将字典保存到 iPhone 不起作用?

Posted

技术标签:

【中文标题】将字典保存到 iPhone 不起作用?【英文标题】:Saving a dictionary to the iPhone is not working? 【发布时间】:2010-09-08 20:22:03 【问题描述】:

这是艰难的一周,我会尽量把这件事说清楚。感谢您花时间阅读。

您好,我正在向服务器发送注册请求。返回 jSon 中的用户对象,我解析 jSon 并将 jSon 字典电话保存为我的 User 类的方法。但是,每当我从手机加载用户时——通过读取保存的字典,然后将其转换为用户对象——我都会得到一个旧的用户对象,这是我上周保存的。

每当我收到用户的 json 时,我都会为他们的 SAT(单一访问令牌)创建几个日志语句。前两个单一访问令牌日志显示由服务器生成的新的。第三条日志语句显示以前用户的旧 SAT。

由此我可以得出结论,保存到手机的工作不正确。在我的 userFromDictionary 方法中,我调用了 saveUserToPhone。这里的逻辑是服务器的json提供了一个新的字典,所以我们应该继续把这个新的字典保存到手机中以供将来加载用户。

User loading/saving logic:
        self.user = [User userFromDictionary:userProperties];   
    NSLog(@"RegistrationScreenOne: User SAT: %@",self.user.singleAccessToken);
    [User saveUserToPhone:userProperties];
    NSLog(@"RegistrationScreenOne: User SAT: %@",self.user.singleAccessToken);      
    self.user = [User userFromPhone];
    NSLog(@"RegistrationScreenOne: User SAT: %@",self.user.singleAccessToken);

--前2个日志显示新SAT,第三个日志显示旧SAT。

所有这些都在模拟器中。每当我运行时,我都会关闭 iPhone 模拟器并从 Xcode 重新启动。

用户实现文件:

+(NSDictionary *)jSonMapping
return [NSDictionary dictionaryWithObjectsAndKeys:
        @"username", @"username",
        @"password", @"password",
   ////
   ////ETC, spared for length
   ////     
 

+(NSString *)userFilePath

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilestats];


   ////
   ////kFileStats is declared in the header as @"user-file.plist"
   ////


+(User*)userFromDictionary:(NSDictionary *)dictionary

NSDictionary *mapping = [self jSonMapping];

User *user = [[User alloc] init];

for (NSString *attribute in [mapping allKeys])
    NSString *classProperty = [mapping objectForKey:attribute];
    NSString *attributeValue = [dictionary objectForKey:attribute];
    [user setValue:attributeValue forKeyPath:classProperty];
    NSLog(@"Class Property: %@ Value: %@",classProperty,attributeValue);
   

[self saveUserToPhone:dictionary];

return [user autorelease];


+(BOOL)doesUserExistOnPhone
NSString *dataPath = [self userFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    return YES;


return NO;


+(User*)userFromPhone
User *user = [[User alloc] init];

NSString *dataPath = [self userFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath])

    NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:dataPath];      
    user = [self userFromDictionary:dictionary];


return user;


+(void)saveUserToPhone:(NSDictionary *)dictionary
[dictionary writeToFile:[self userFilePath] atomically:YES];    
 

这 3 个日志语句的日志输出,包括查看属性:

First my new user, I used random number strings for their data:
2010-09-08 14:29:05.274 AppNameWindowedApp[6873:207] Class Property: lastName Value: 2309320923
2010-09-08 14:29:05.274 AppNameWindowedApp[6873:207] Class Property: firstName Value: 309209239032
2010-09-08 14:29:05.275 AppNameWindowedApp[6873:207] Class Property: schoolName Value: ##################
2010-09-08 14:29:05.275 AppNameWindowedApp[6873:207] Class Property: mobile Value: 23902390
2010-09-08 14:29:05.276 AppNameWindowedApp[6873:207] Class Property: singleAccessToken Value: xUd3-OfcUrEaHf3FIKdl
2010-09-08 14:29:05.277 AppNameWindowedApp[6873:207] Class Property: password Value: (null)
2010-09-08 14:29:05.278 AppNameWindowedApp[6873:207] Class Property: email Value: 209239023@arst.com
2010-09-08 14:29:05.278 AppNameWindowedApp[6873:207] Class Property: schoolID Value: 4c871745a1e7490b4d000008
2010-09-08 14:29:05.279 AppNameWindowedApp[6873:207] Class Property: role Value: admin
2010-09-08 14:29:05.279 AppNameWindowedApp[6873:207] Class Property: username Value: 23903902

从json字典设置用户后的单一访问令牌:

2010-09-08 14:29:05.280 AppNameWindowedApp[6873:207] RegistrationScreenOne:用户 SAT:xUd3-OfcUrEaHf3FIKdl

Single Access Token的第二条Log语句,保存后:

2010-09-08 14:29:05.280 AppNameWindowedApp[6873:207] RegistrationScreenOne:用户 SAT:xUd3-OfcUrEaHf3FIKdl

从手机读取时的用户类属性:

2010-09-08 14:29:05.281 AppNameWindowedApp[6873:207] Class Property: lastName Value: Admin
2010-09-08 14:29:05.282 AppNameWindowedApp[6873:207] Class Property: firstName Value: Admin
2010-09-08 14:29:05.282 AppNameWindowedApp[6873:207] Class Property: schoolName Value: #############
2010-09-08 14:29:05.283 AppNameWindowedApp[6873:207] Class Property: mobile Value: 
2010-09-08 14:29:05.283 AppNameWindowedApp[6873:207] Class Property: singleAccessToken Value: RL01Mv1-yJacB_FAxHG1
2010-09-08 14:29:05.283 AppNameWindowedApp[6873:207] Class Property: password Value: (null)
2010-09-08 14:29:05.284 AppNameWindowedApp[6873:207] Class Property: email Value: admin@admin.com
2010-09-08 14:29:05.286 AppNameWindowedApp[6873:207] Class Property: schoolID Value: 4c86f53ff7c9ff494f000001
2010-09-08 14:29:05.286 AppNameWindowedApp[6873:207] Class Property: role Value: AppName
2010-09-08 14:29:05.286 AppNameWindowedApp[6873:207] Class Property: username Value: admin

读取 UserFromPhone 后的 Single Access Token:

2010-09-08 14:29:05.287 AppNameWindowedApp[6873:207] RegistrationScreenOne:用户 SAT:RL01Mv1-yJacB_FAxHG1

【问题讨论】:

【参考方案1】:

文件可能没有被覆盖,这可以解释为什么您会得到旧记录。

【讨论】:

它试图保存的字典中有一个空值。谢谢!

以上是关于将字典保存到 iPhone 不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

localStorage UIWebView iPhone / iPad 保存或读取不起作用.. 有时?

核心数据保存到数据库不起作用

PHP干预orientate()在iPhone上不起作用

将 iphone 前置摄像头广播到 HTML 元素不起作用 iOS 11.2

将 skspritenode 位置保存到 plist 不起作用

Android:将arrayList保存到文件不起作用