将键字符串的对象设置为iOS 9

时间:2015-11-11 21:01:48

标签: ios objective-c

大家好,我一直遇到麻烦,在网上查了几天,仍然无法获得一段代码来实现我的想法。我试图得到一个#34; alertTextField.text"并将其设置为密钥@" kUsername"在plist_path中。我一直在尝试不同的方法但是当我检查NSLog时它没有设置并且只显示kUsername,即使在plist文件中有一个名为kUsername的密钥。有人可以帮助我,告诉我我做错了什么,这样我可以多学一点吗?

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
 if (buttonIndex == 0){
       UITextField *alertTextField = [alertView textFieldAtIndex:0];
        NSLog(@"alerttextfiled - %@",alertTextField.text);
        NSMutableDictionary *userlist= [[NSMutableDictionary alloc] initWithContentsOfFile:PLIST_PATH];
        NSString *username = [userlist objectForKey:@"kUsername"];
        [userlist setObject:alertTextField.text forKey:@"kUsername"];
        NSLog(@"alerttextfiled - %@",username);
    }}
@end

1 个答案:

答案 0 :(得分:0)

自己想出来,需要使用writetofile。

[userlist writeToFile:PLIST_PATH atomically:YES];