问题描述
我正在制作游戏,当我关闭应用程序时(关闭多任务管理器),我的所有数据都消失了!所以,我的问题很简单:如何保存数据?
让我们说你想要保存分数和级别,这两个属性都是名为dataHolder的对象的属性。
DataHolder可以创建为单例,所以你不必太担心关于你从哪里访问它(实际上是 sharedInstance
):
它的代码看起来有点像这样: / p>
DataHolder.h
#import <基金会/ Foundation.h>
@interface DataHolder:NSObject
+(DataHolder *)sharedInstance;
@property(assign)int level;
@property(assign)int score;
- (void)saveData;
- (void)loadData;
@end
DataHolder.m
NSString * const kLevel = @kLevel;
NSString * const kScore = @kScore;
@implementation DataHolder
- (id)init
{
self = [super init];
if(self)
{
_level = 0;
_score = 0;
}
返回自我;
}
+(DataHolder *)sharedInstance
{
静态MDataHolder * _sharedInstance = nil;
static dispatch_once_t onceSecurePredicate;
dispatch_once(& onceSecurePredicate,^
{
_sharedInstance = [[self alloc] init];
});
返回_sharedInstance;
}
//在这个例子中,你将数据保存到NSUserDefault的
//你可以将它保存到一个文件或一些更复杂的
//数据结构:取决于你需要什么,真的是
- (void)saveData
{
[[NSUserDefaults standardUserDefaults]
setObject:[NSNumber numberWithInt:self.score] forKey:kScore];
[[NSUserDefaults standardUserDefaults]
setObject:[NSNumber numberWithInt:self.level] forKey:kLevel];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)loadData
{
if([[NSUserDefaults standardUserDefaults] objectForKey:kScore])
{
self。得分= [(NSNumber *)[[NSUserDefaults standardUserDefaults]
objectForKey:kScore] intValue];
self.level = [(NSNumber *)[[NSUserDefaults standardUserDefaults]
objectForKey:kLevel] intValue];
}
其他
{
self.level = 0;
self.score = 0;
}
}
@end
Don不要忘记#importDataHolder.h你需要的地方,或者只是把它放在 ...- Prefix.pch
。
您可以在 appDelegate
方法中执行实际加载和保存:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[DataHolder sharedInstance] saveData];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[DataHolder sharedInstance] loadData];
}
您可以使用 [DataHolder sharedInstance] .score 和 [DataHolder sharedInstance] .level
。
对于一个简单的任务来说,这似乎有些过分,但它确实有助于保持整洁,它可以帮助您避免将所有数据保存在 appDelegate
(这通常是快速的&肮脏的解决方案)。
I'm making a game and when I close the app (close at multitask manager), all my data is gone! So, My question is very simple: How do I save the data?
Let's say you want to save score and level, which are both properties of an object called dataHolder.
DataHolder can be created as a singleton, so you don't have to worry too much about from where you access it (its sharedInstance
actually):
It's code would look a bit like this:
DataHolder.h
#import <Foundation/Foundation.h>
@interface DataHolder : NSObject
+ (DataHolder *)sharedInstance;
@property (assign) int level;
@property (assign) int score;
-(void) saveData;
-(void) loadData;
@end
DataHolder.m
NSString * const kLevel = @"kLevel";
NSString * const kScore = @"kScore";
@implementation DataHolder
- (id) init
{
self = [super init];
if (self)
{
_level = 0;
_score = 0;
}
return self;
}
+ (DataHolder *)sharedInstance
{
static MDataHolder *_sharedInstance = nil;
static dispatch_once_t onceSecurePredicate;
dispatch_once(&onceSecurePredicate,^
{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
//in this example you are saving data to NSUserDefault's
//you could save it also to a file or to some more complex
//data structure: depends on what you need, really
-(void)saveData
{
[[NSUserDefaults standardUserDefaults]
setObject:[NSNumber numberWithInt:self.score] forKey:kScore];
[[NSUserDefaults standardUserDefaults]
setObject:[NSNumber numberWithInt:self.level] forKey:kLevel];
[[NSUserDefaults standardUserDefaults] synchronize];
}
-(void)loadData
{
if ([[NSUserDefaults standardUserDefaults] objectForKey:kScore])
{
self.score = [(NSNumber *)[[NSUserDefaults standardUserDefaults]
objectForKey:kScore] intValue];
self.level = [(NSNumber *)[[NSUserDefaults standardUserDefaults]
objectForKey:kLevel] intValue];
}
else
{
self.level = 0;
self.score = 0;
}
}
@end
Don't forget to #import "DataHolder.h" where you need it, or simply put it in ...-Prefix.pch
.
You could perform actual loading and saving in appDelegate
methods:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[DataHolder sharedInstance] saveData];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[DataHolder sharedInstance] loadData];
}
You can access your score and level data from anywhere with [DataHolder sharedInstance].score
and [DataHolder sharedInstance].level
.
This might seem like an overkill for a simple task but it sure helps to keep things tidy and it can help you to avoid keeping all the data in appDelegate
(which is usually the quick & dirty path to solution).
这篇关于如何在iOS中保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!