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.

有帮助吗?

解决方案

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];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top