读出Plist问题

发布于 2024-12-25 04:01:48 字数 1573 浏览 3 评论 0原文

为什么 naam 输出(NULL)? (抱歉,我知道这是基本的东西,但我是 plist 新手)

这是 plist: 在此处输入图像描述

方法如下:

- (id) init {

self = [super init];
if (self) {
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    NSLog(@"path: %@",plistPath);
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];

    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
                                          propertyListFromData:plistXML
                                          mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                          format:&format
                                          errorDescription:&errorDesc];
      NSLog(@"temp: %@",temp);
    if (!temp) {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
    }
    self.personName = [temp objectForKey:@"Name"];
    NSLog(@"NAAM:%@",[temp objectForKey:@"Name"]);
    self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:@"Phones"]];


}
return self;}

Why is the naam output (NULL)? (sorry In know this is basic stuff, but I am new with plists)

This is the plist:
enter image description here

Here's the method:

- (id) init {

self = [super init];
if (self) {
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    NSLog(@"path: %@",plistPath);
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];

    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
                                          propertyListFromData:plistXML
                                          mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                          format:&format
                                          errorDescription:&errorDesc];
      NSLog(@"temp: %@",temp);
    if (!temp) {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
    }
    self.personName = [temp objectForKey:@"Name"];
    NSLog(@"NAAM:%@",[temp objectForKey:@"Name"]);
    self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:@"Phones"]];


}
return self;}

the output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

财迷小姐 2025-01-01 04:01:48

因为temp是一个字典,只包含一个键:@“Root”。您正在内部字典中查找对象:[[temp objectForKey:@"Root"] objectForKey:@"Name"]

Because temp is a dictionary that contains only one key: @"Root". You are looking for an object in the inner dictionary: [[temp objectForKey:@"Root"] objectForKey:@"Name"]

往日 2025-01-01 04:01:48

先尝试解压ROOT。

NSDictionary* root=[temp objectForKey:@"Root"];
NSLog(@"NAAM:%@",[root objectForKey:@"Name"]);

Try extracting ROOT first.

NSDictionary* root=[temp objectForKey:@"Root"];
NSLog(@"NAAM:%@",[root objectForKey:@"Name"]);
情愿 2025-01-01 04:01:48

尝试

NSLog(@"NAAM: %@", [temp valueForKeyPath:@"Root.Name"]);

Phones 启动第一部手机,执行以下操作:

NSDictionary *root = [temp valueForKey:@"Root"];
[[root valueForKey:@"Phones"] objectAtIndex:0];

try

NSLog(@"NAAM: %@", [temp valueForKeyPath:@"Root.Name"]);

To tread first phone from Phones do this:

NSDictionary *root = [temp valueForKey:@"Root"];
[[root valueForKey:@"Phones"] objectAtIndex:0];
挖鼻大婶 2025-01-01 04:01:48

我的“Words.plist”

<dict>
    <key>Root</key>
    <array>
        <string>sunday</string>
        <string>monday</string>
        <integer>44</integer>
    </array>
</dict>


NSString *StringsFromPList = [[NSBundle mainBundle] bundlePath];
NSString *itemPositionPlistLocation = [StringsFromPList stringByAppendingPathComponent:@"Words.plist"];
 _myDictionary= [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation];
NSArray * items = [_myDictionary objectForKey:@"Root"];
NSLog(@"%@", items);

希望它能有所帮助:)

my "Words.plist"

<dict>
    <key>Root</key>
    <array>
        <string>sunday</string>
        <string>monday</string>
        <integer>44</integer>
    </array>
</dict>


NSString *StringsFromPList = [[NSBundle mainBundle] bundlePath];
NSString *itemPositionPlistLocation = [StringsFromPList stringByAppendingPathComponent:@"Words.plist"];
 _myDictionary= [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation];
NSArray * items = [_myDictionary objectForKey:@"Root"];
NSLog(@"%@", items);

Hope it helps a bit :)

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