Can't read from plist

前端 未结 2 2004
别跟我提以往
别跟我提以往 2021-01-16 20:42

I\'m trying to do something that I already know how to do - but it isn\'t working. Looking for ideas to fix this. I think it\'s something to do with the way Xcode 4.1 create

相关标签:
2条回答
  • 2021-01-16 21:00

    The root object of you .plist is a dict, so you need to load it in memory as a NSDictionary.

    myDictionary = [[NSDictionary alloc] initWithContentsOfFile: myFile];
    
    0 讨论(0)
  • 2021-01-16 21:06

    If you just want the plist loaded, you need to use property list serialization to first load the plist as NSData

    [[NSData alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"File1"]]
    

    then serialize it into a dict (or array, depending on what your root item is) using

    NSDictionary *aDict = [NSPropertyListSerialization propertyListWithData: options: format: error:]
    

    When you view the plist in Xcode, the top level element is the type to make aDict.

    See here for more info: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/SerializePlist/SerializePlist.html

    If you want the entire plist loaded as strings, you probably need to do something with an NSString method, something like [NSString stringFromData].

    0 讨论(0)
提交回复
热议问题