在添加字符串plist文件时遇到问题

时间:2016-05-04 10:40:47

标签: ios objective-c iphone plist

我的应用程序中有一个plist文件,表视图加载了它的对象。当用户在textfield中键入文本并点击保存按钮时,我需要保存一个字符串,这是我的代码,日志显示正确的字符串,但表格没有重新加载和plist没有'添加任何东西!

     NSString *rootPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath=[rootPath stringByAppendingPathComponent:@"users.plist"];
NSMutableDictionary *plist_dict=[[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];

if(plist_dict==nil)
{
    plist_dict =[[NSMutableDictionary alloc]init];
}
[plist_dict setObject:_username.text forKey:@"username"];

[plist_dict writeToFile:plistPath atomically:YES];


[_tableView reloadData];

这是我的默认plist文件:

<plist version="1.0">
<dict>
    <key>username</key>
    <array>
        <string>Instagram</string>
    </array>
</dict>
</plist>

1 个答案:

答案 0 :(得分:0)

 NSString *rootPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath=[rootPath stringByAppendingPathComponent:@"appSetting.plist"];
NSMutableDictionary *plist_dict=[[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];

if(plist_dict==nil)
{
    plist_dict =[[NSMutableDictionary alloc]init];
}
[plist_dict setObject:[NSNumber numberWithBool:status] forKey:@"isLoggedIn"];

[plist_dict writeToFile:plistPath atomically:YES];

使用此代码。它为我工作。希望它会对你有所帮助。