简体   繁体   中英

How to read and parse a plist

I like to read and parse plist files, but It wont work.

This is my code: NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:self.plistPath]; dict always is (null)

I tryed to get the content as string, here is my code:

NSString *string = [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:self.plistPath] encoding:NSUTF8StringEncoding];

self.plistPath is /tmp/com.apple.audio.hogmode.plist

And this is the content of the plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>

What am I doing wrong?

Figured it out!

NSString *error;
NSPropertyListFormat format;
NSDictionary *dict = [NSPropertyListSerialization propertyListFromData:[NSData dataWithContentsOfFile:self.plistPath] mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM