Plist 读写 iPhone
我的类在读取数据并将数据写入 plist 时遇到问题。这是一些代码:
第一个块来自我的自定义类,其中包含我的所有 plist 读取和写入方法。
-(NSString *) dataFilePath{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"userInformation.plist"];
}
-(bool)readUserIsMale{
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSDictionary *boolDict = [[NSDictionary alloc] initWithContentsOfFile:[self dataFilePath]];
return [[boolDict objectForKey:@"boolUserIsMale"] boolValue];
}
return nil;
}
-(void)writeUserIsMale:(bool)boolValue{
NSDictionary *boolDict = [[NSDictionary alloc] init];
[boolDict setValue:[NSNumber numberWithBool:boolValue] forKey:@"boolUserIsMale"];
[boolDict writeToFile:[self dataFilePath] atomically:YES];
}
然后,我在另一个需要导入的类中,创建并使用类方法:
#import "plistReadWrite.h"
plistReadWrite *readWrite;
如果我尝试在控制台中查看它的值,我会得到(null)返回。
NSLog(@"%@",[readWrite readUserIsMale]);
这当然是在我编写了一些数据之后,如下所示:
[readWrite writeUserIsMale:isUserMale];
isUserMale
是一个 bool 值。
任何帮助将不胜感激,如果您需要更多信息,请告诉我。谢谢。
I am having troubles with my class which reads and writes data to a plist. Here is some code:
This first chunk is from my custom class with all my plist read and write methods.
-(NSString *) dataFilePath{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"userInformation.plist"];
}
-(bool)readUserIsMale{
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSDictionary *boolDict = [[NSDictionary alloc] initWithContentsOfFile:[self dataFilePath]];
return [[boolDict objectForKey:@"boolUserIsMale"] boolValue];
}
return nil;
}
-(void)writeUserIsMale:(bool)boolValue{
NSDictionary *boolDict = [[NSDictionary alloc] init];
[boolDict setValue:[NSNumber numberWithBool:boolValue] forKey:@"boolUserIsMale"];
[boolDict writeToFile:[self dataFilePath] atomically:YES];
}
I then in another class where desired import, create and use the class methods:
#import "plistReadWrite.h"
plistReadWrite *readWrite;
If I try and see its value in the console I get (null) return.
NSLog(@"%@",[readWrite readUserIsMale]);
This is of course after I have written some data like so:
[readWrite writeUserIsMale:isUserMale];
isUserMale
being a bool value.
Any help would be massively appreciated, if you need anymore info let me know. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这基本上是正确的。在您的 writeUserIsMale: 方法中,您需要一个可变字典,因此您实际上可以设置该键(这应该按原样为您崩溃,所以我猜是复制/粘贴问题?)
然后当您记录该值时,请记住bool(或 BOOL)是原语,而不是对象,所以:
最后,由于这是 Objective-C,我可能会使用 BOOL 而不是 bool。
我假设这只是一个简单的例子,并且您了解 NSUserDefaults 这类事情。
希望有帮助。
I think this is mostly correct. In your writeUserIsMale: method you want a mutable dictionary, so you can actually set that key (this should have crashed for you as is, so I'm guessing a copy/paste problem?)
And then when you log the value, remember that bool (or BOOL) are primitives, not objects so:
Lastly, since this is objective-c, I would probably use BOOL instead of bool.
I'm assuming this is just a simple example, and that you know about NSUserDefaults for this sort of thing.
Hope that helps.