开发者

Objective-C arrayWithPlist (that's already in an NSString)

I have an NSString that already contains a pList.

How do I turn it into an NSArray? (WITHOUT saving it to disk, only to reloa开发者_如何学编程d it back with arrayWithContentsOfFile, and then have to delete it.)

Where is the make arrayWithPlist or arrayWithString method? (Or how would I make my own?)

 NSArray *anArray = [NSArray arrayWithPlist:myPlistString];


You want to use NSPropertyListSerialization:

NSData *data = [plistString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSArray *plist = [NSPropertyListSerialization
                  propertyListWithData:plistData
                  options:/*unused*/0
                  format:NULL
                  error:&error];
if (!plist) {
    NSLog(@"%s: Failed to create plist: %@",
          __func__, error ?: @"(unknown error)");
}

That particular method was introduced with iOS 4.0/Mac OS X 10.6. Prior to those releases, you would use:

NSData *data = [plistString dataUsingEncoding:NSUTF8StringEncoding];
NSString *errorText = nil;
NSArray *plist = [NSPropertyListSerialization
                  propertyListFromData:plistData
                  mutabilityOption:NSPropertyListImmutable
                  format:NULL
                  errorDescription:&errorText];
if (!plist) {
    NSLog(@"%s: Failed to create plist: %@",
          __func__, errorText ?: @"(unknown error)");

    /* Part of the reason this method was replaced:
     * It is the caller's responsibility to release the error description
     * if any is returned. This is completely counter-intuitive.
     */
    [errorText release], errorText = nil;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜