繁体   English   中英

无法从iOS中创建的plist文件中获取值

[英]Can't get the values from the created plist file in iOS

我是ios开发中的新手,我无法从使用先前方法创建的plist中读取值。 读取方法返回(null)(null)。 谁能帮我?

在这里,我创建一个plist:

- (void)createAppPlist {

    plistPath = [self getDataFileDir];
    // Create the data structure

    rootElement = [NSMutableDictionary dictionaryWithCapacity:3];
    NSError *err;
    name = @"North America";
    country = @"United States";

    continentElement = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:name, country, nil] forKeys:[NSArray arrayWithObjects:@"Name", @"Country", nil]];

    [rootElement setObject:continentElement forKey:@"Continent"];

    //Create plist file and serialize XML

    data = [NSPropertyListSerialization dataWithPropertyList:rootElement format:NSPropertyListXMLFormat_v1_0 options:0 error:&err];
    if(data)
    {
        [data writeToFile:plistPath atomically:YES];
    } else {
        NSLog(@"An error has occures %@", err);
    }

    NSLog(@"%@", rootElement);
    NSLog(@"%@", data);

}

在这里,我试图获取价值。

-(void)readAppPlist
{
    plistPath = [self getDataFileDir];
    NSMutableDictionary *propertyDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    name = [propertyDict objectForKey:@"Name"];
    country = [propertyDict objectForKey:@"Country"];

    NSLog(@"%@     %@", name, country);
}

使用以下代码创建和读取文档目录中的Plist,可以根据需要更改存储路径

- (void)createAppPlist {

//    plistPath = [self getDataFileDir];
    // Create the data structure

    NSMutableDictionary *rootElement = [NSMutableDictionary dictionaryWithCapacity:3];

    NSString *name = @"North America";
    NSString *country = @"United States";


    NSMutableDictionary *continentElement = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:name, country, nil] forKeys:[NSArray arrayWithObjects:@"Name", @"Country", nil]];

    [rootElement setObject:continentElement forKey:@"Continent"];

    //Create plist file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *filePath = [docDir stringByAppendingPathComponent:@"myPlist.plist"];
    [rootElement writeToFile:filePath atomically:YES];

    NSLog(@"%@", rootElement);
}

-(void)readAppPlist
{
    // Get pList file path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *filePath = [docDir stringByAppendingPathComponent:@"myPlist.plist"];

    NSMutableDictionary *propertyDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    NSDictionary *rootDict = [propertyDict valueForKey:@"Continent"];

    // now read all elements inside, it will print key an value
    [rootDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
        NSLog(@"[%@]->[%@]",key,obj);
    }];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM