Search code examples
objective-cmacoscocoaappkitproperty-list

NSString Propertylist issue


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.


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];