1

I have an NSString that already contains a pList.

How do I turn it into an NSArray? (WITHOUT saving it to disk, only to reload 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];
Patricia
  • 309
  • 3
  • 11
  • What works for some people... doesn't work for others. Other answers are "50%" correct. Others have 5 correct answers. Sorry... I'm only here to GET help... and to GIVE help. Don't want to play the "who's right, who's wrong" arguing game. – Patricia Oct 29 '10 at 20:58
  • Does anyone know how I can block vikingosegundo so none of his useless "answers" appear on my screen? – Patricia Oct 29 '10 at 20:59
  • Does anyone know, why Patricia is so angry? – vikingosegundo Nov 30 '10 at 05:21

1 Answers1

5

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;
}
Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
  • I'll give that a try. I guess the BIG question is: How would I have figured that out on my own? I would have NEVER have thought to look deep inside NSPropertyListSerialization for this solution. – Patricia Oct 29 '10 at 21:00
  • You want to do something with property lists. So, in the Xcode documentation window, you would start typing "property". Then you might find that class, or you would find and read Apple's [Property List Programming Guide](http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/PropertyLists/Introduction/Introduction.html). Apple's documentation is really very good, and they provide a lot of sample code and projects, too. – Jeremy W. Sherman Oct 29 '10 at 21:16
  • 1
    The docs have always been useless to me. I would have searched for NSARRAY (not property). But either way... I would get 100s of matches and spend about an hour reading them... and 0 of them would answer my question. – Patricia Nov 04 '10 at 16:52
  • This is all I get when I try Jermemy's code: (Even though I've not tried to send it any 'selector' at all.... ..... .... *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSPropertyListSerialization propertyListWithData:options:format:error:]: unrecognized selector sent to class 0x38483aa4' – Patricia Nov 04 '10 at 16:56
  • 1
    "Selector" is another way of saying "method name." This error says that `NSPropertyListSerialization` does not have a `propertyListWithData:options:format:error:` method. This method was introduced with iOS 4.0, so you're likely targeting an earlier version of the platform. I'll add code for the older version to the answer. – Jeremy W. Sherman Nov 04 '10 at 18:59
  • I thought "unrecognized selector" would mean I'm trying to do something like: [NSTest makeThis:@selector(missing:)]; Why can't the error say anything even remotely specific like: "options can't be used with NSPropertyListSerialization"? – Patricia Nov 04 '10 at 19:28