1. 支持数据格式:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL。
2.保存方式
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:myInteger forKey:@"myInteger"]; [userDefaults setFloat:myFloat forKey:@"myFloat"]; [userDefaults setDouble:myDouble forKey:@"myDouble"]; [userDefaults setObject:myString forKey:@"myString"]; [userDefaults setObject:myDate forKey:@"myDate"]; [userDefaults setObject:myArray forKey:@"myArray"]; [userDefaults setObject:myDictionary forKey:@"myDictionary"]; //这里建议同步存储到磁盘中,但是不是必须的 [userDefaults synchronize];
3.数据读取
NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
NSInteger myInteger = [userDefaultes integerForKey:@"myInteger"]; //读取浮点型float类型的数据 float myFloat = [userDefaultes floatForKey:@"myFloat"]; //读取double类型的数据 double myDouble = [userDefaultes doubleForKey:@"myDouble"]; //读取NSString类型的数据 NSString *myString = [userDefaultes stringForKey:@"myString"]; //读取NSDate日期类型的数据 NSDate *myDate = [userDefaultes valueForKey:@"myDate"]; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //读取数组NSArray类型的数据 NSArray *myArray = [userDefaultes arrayForKey:@"myArray"]; //读取字典类型NSDictionary类型的数据 NSDictionary *myDictionary = [userDefaultes dictionaryForKey:@"myDictionary"];