Plist 读写 iPhone

发布于 2024-12-15 01:08:19 字数 1379 浏览 3 评论 0原文

我的类在读取数据并将数据写入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

前事休说 2024-12-22 01:08:19

我认为这基本上是正确的。在您的 writeUserIsMale: 方法中,您需要一个可变字典,因此您实际上可以设置该键(这应该按原样为您崩溃,所以我猜是复制/粘贴问题?)

//NSDictionary *boolDict = [[NSDictionary alloc] init];
//should be:

NSMutableDictionary *boolDict = [[NSMutableDictionary alloc] init];

然后当您记录该值时,请记住bool(或 BOOL)是原语,而不是对象,所以:

NSLog (@"%d",[readWrite readUserIsMale]); // Will print 0 or 1
// or if you prefer:
NSLog (@"%@", ([readWrite readUserIsMale]? @"YES":@"NO")); // print YES or NO

最后,由于这是 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?)

//NSDictionary *boolDict = [[NSDictionary alloc] init];
//should be:

NSMutableDictionary *boolDict = [[NSMutableDictionary alloc] init];

And then when you log the value, remember that bool (or BOOL) are primitives, not objects so:

NSLog (@"%d",[readWrite readUserIsMale]); // Will print 0 or 1
// or if you prefer:
NSLog (@"%@", ([readWrite readUserIsMale]? @"YES":@"NO")); // print YES or NO

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文