1夜焦、創(chuàng)建一個(gè)moder (繼承NSObject的類(lèi)拄显,我們可以在一個(gè)類(lèi)里面寫(xiě)多個(gè)moder)? 繼承<NSCoding>
@interface storageModer : NSObject
@property(nonatomic,strong)NSMutableArray * storage_moder;
@end
2、在moder中實(shí)現(xiàn)兩個(gè)方法
@implementation storageModer
// 告訴需要 需要?dú)w檔哪些屬性
- (void)encodeWithCoder:(NSCoder*)aCoder
{
[aCoder encodeObject:_storage_moder forKey:@"storage"];
}
// 告訴系統(tǒng)解檔哪些屬性
- (instancetype)initWithCoder:(NSCoder*)aDecoder
{
if (self = [super init]) {
_storage_moder = [aDecoder decodeObjectForKey:@"storage"];
}
return self;
}
@end
//存儲(chǔ)路徑的宏
#define kFilePath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"storage2.data"]
3咽弦、調(diào)用存儲(chǔ)數(shù)據(jù)
//讀存數(shù)據(jù)
storageModer *? storage_out = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath];
storage_out.storage_moder[self.Then_int][Transformation_int+1]=@"1";
storage_out.storage_moder[4][0] =[NSString stringWithFormat:@"%d",[storage_out.storage_moder[4][0] intValue]+pass_Coin] ;
//存儲(chǔ)數(shù)據(jù)
storageModer *? storage_in = [[storageModer alloc]init];
storage_in.storage_moder = storage_out.storage_moder;
[NSKeyedArchiver archiveRootObject:storage_in toFile:kFilePath];
4蛋哭、App首次啟動(dòng)時(shí)初始化數(shù)據(jù)
//判斷app是否首次啟動(dòng)
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions{
NSArray * name = [[NSArray alloc]initWithObjects:@"Sium",@"Tom",@"YonG",@"QuAn",@"Greent", nil];
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
NSLog(@"第一次啟動(dòng)");
//獲取路徑
storageModer * storage = [[storageModer alloc]init];
NSString * name_str =name[arc4random()%4];
NSMutableArray * one_check = [NSMutableArray arrayWithObjects:@"1",@"0",@"0",@"0", nil];
NSMutableArray * two_check = [NSMutableArray arrayWithObjects:@"1",@"0",@"0",@"0", nil];
NSMutableArray * three_check = [NSMutableArray arrayWithObjects:@"1",@"0",@"0",@"0", nil];
NSMutableArray * four_check = [NSMutableArray arrayWithObjects:@"1",@"0",@"0",@"0", nil];
NSMutableArray * five = [NSMutableArray arrayWithObjects:@"0",name_str, nil];
storage.storage_moder = [NSMutableArray arrayWithObjects:one_check,two_check,three_check ,four_check,five, nil];
[NSKeyedArchiver archiveRootObject:storage toFile:kFilePath];
}
return YES;
}
數(shù)據(jù)每次存儲(chǔ)需要全部將數(shù)據(jù)全部賦值,沒(méi)賦值的就清零掘而。所以以建NSMutableArray或字典最佳