Question

What is the best way to validate whether a NSString is a propertyList or not? If I call NSString's -propertyList method it will throw an exception if it cannot parse the string.

Was it helpful?

Solution

Use +propertyListWithData:options:format:error: on NSPropertyListSerialization to attempt to parse the data, it can pass you back an NSError object with some diagnostics if it can't. For example:

NSString *plist = ...;
NSError *e = nil;
NSPropertyListFormat format;
id obj = [NSPropertyListSerialization 
    propertyListWithData:[plist dataUsingEncoding:NSUnicodeStringEncoding]
                 options:NSPropertyListImmutable
                  format:&format
                   error:&e];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top