Cocoa / CocoaTouchで.plistファイルを操作する

すべてに幸運を!



今日、OS XやiOSで設定やその他のプログラムデータを保存するいくつかの側面についてお話ししたいと思います。 いつものように、いくつかのオプションがあります:Core Data、裸のSQLite、バイナリ形式、テキスト形式、NSUserDefaults、そして聞いたことがあると思いますが、 PLISTのようなファイル 、つまりXML P roperty Listです。



要するに、plistファイルは単純なXMLですが、いくつか注意点があります。 たとえば、タグ内のタグの順序はいくつかのルールによって決定されます。キーと値のペアになりますが、キータイプのタグと値タイプのタグは同じレベルにあります。 典型的な例:



<key>identifier</key> <string>j3qq4-h7h2v</string>
      
      





Plistは、NSString、NSNumber(int、float、BOOL)、NSDate、NSArray、NSDictionary、NSDataの主要なCocoaデータ型を保存できます。 次のタグは、これらのタイプに対応しています: , , , <true/>, <false/>, , , , . , plist , .



- , , API .



plist' : " ?", " ?", " ?" . , .



, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .



. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist . , plutil



JSON, - , .



NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization



, Cocoa.



, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.



: ( ) , .



- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }





, , , :



<?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> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>





, ? ! XML . :



{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }





.



:



- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }





? ! JSON-, ! , , : NSDictionary . .



, "mutable" , NSPropertyListMutableContainersAndLeaves



. NSPropertyListImmutable



, NSMutableDictionary, NSDictionary, .



, PLIST Cocoa. , , .



!



UPD: mejedi , plain-XML .



XML « », ( , «hello world» , ).



, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).












, , , <true/>, <false/>, , , , . , plist , .



- , , API .



plist' : " ?", " ?", " ?" . , .



, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .



. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist




. , plutil



JSON, - , .



NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization



, Cocoa.



, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.



: ( ) , .



- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }





, , , :



<?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> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>





, ? ! XML . :



{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }





.



:



- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }





? ! JSON-, ! , , : NSDictionary . .



, "mutable" , NSPropertyListMutableContainersAndLeaves



. NSPropertyListImmutable



, NSMutableDictionary, NSDictionary, .



, PLIST Cocoa. , , .



!



UPD: mejedi , plain-XML .



XML « », ( , «hello world» , ).



, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).








 , , , <true/>, <false/>, , , , . , plist    ,       . 
      



- , , API .



plist' : " ?", " ?", " ?" . , .



, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .



. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist




. , plutil



JSON, - , .



NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization



, Cocoa.



, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.



: ( ) , .



- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }





, , , :



<?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> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>





, ? ! XML . :



{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }





.



:



- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }





? ! JSON-, ! , , : NSDictionary . .



, "mutable" , NSPropertyListMutableContainersAndLeaves



. NSPropertyListImmutable



, NSMutableDictionary, NSDictionary, .



, PLIST Cocoa. , , .



!



UPD: mejedi , plain-XML .



XML « », ( , «hello world» , ).



, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).








, , , <true/>, <false/>, , , , . , plist , .



- , , API .



plist' : " ?", " ?", " ?" . , .



, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .



. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist




. , plutil



JSON, - , .



NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization



, Cocoa.



, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.



: ( ) , .



- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }





, , , :



<?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> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>





, ? ! XML . :



{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }





.



:



- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }





? ! JSON-, ! , , : NSDictionary . .



, "mutable" , NSPropertyListMutableContainersAndLeaves



. NSPropertyListImmutable



, NSMutableDictionary, NSDictionary, .



, PLIST Cocoa. , , .



!



UPD: mejedi , plain-XML .



XML « », ( , «hello world» , ).



, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).








 , , , <true/>, <false/>, , , , . , plist    ,       . 
      



- , , API .



plist' : " ?", " ?", " ?" . , .



, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .



. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist




. , plutil



JSON, - , .



NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization



, Cocoa.



, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.



: ( ) , .



- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }





, , , :



<?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> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>





, ? ! XML . :



{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }





.



:



- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }





? ! JSON-, ! , , : NSDictionary . .



, "mutable" , NSPropertyListMutableContainersAndLeaves



. NSPropertyListImmutable



, NSMutableDictionary, NSDictionary, .



, PLIST Cocoa. , , .



!



UPD: mejedi , plain-XML .



XML « », ( , «hello world» , ).



, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).








, , , <true/>, <false/>, , , , . , plist , .



- , , API .



plist' : " ?", " ?", " ?" . , .



, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .



. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist




. , plutil



JSON, - , .



NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization



, Cocoa.



, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.



: ( ) , .



- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }





, , , :



<?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> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>





, ? ! XML . :



{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }





.



:



- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }





? ! JSON-, ! , , : NSDictionary . .



, "mutable" , NSPropertyListMutableContainersAndLeaves



. NSPropertyListImmutable



, NSMutableDictionary, NSDictionary, .



, PLIST Cocoa. , , .



!



UPD: mejedi , plain-XML .



XML « », ( , «hello world» , ).



, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).








 , , , <true/>, <false/>, , , , . , plist    ,       . 
      



- , , API .



plist' : " ?", " ?", " ?" . , .



, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .



. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist




. , plutil



JSON, - , .



NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization



, Cocoa.



, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.



: ( ) , .



- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }





, , , :



<?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> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>





, ? ! XML . :



{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }





.



:



- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }





? ! JSON-, ! , , : NSDictionary . .



, "mutable" , NSPropertyListMutableContainersAndLeaves



. NSPropertyListImmutable



, NSMutableDictionary, NSDictionary, .



, PLIST Cocoa. , , .



!



UPD: mejedi , plain-XML .



XML « », ( , «hello world» , ).



, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).








, , , <true/>, <false/>, , , , . , plist , .



- , , API .



plist' : " ?", " ?", " ?" . , .



, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .



. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist




. , plutil



JSON, - , .



NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization



, Cocoa.



, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.



: ( ) , .



- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }





, , , :



<?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> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>





, ? ! XML . :



{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }





.



:



- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }





? ! JSON-, ! , , : NSDictionary . .



, "mutable" , NSPropertyListMutableContainersAndLeaves



. NSPropertyListImmutable



, NSMutableDictionary, NSDictionary, .



, PLIST Cocoa. , , .



!



UPD: mejedi , plain-XML .



XML « », ( , «hello world» , ).



, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).








 , , , <true/>, <false/>, , , , . , plist    ,       . 
      



- , , API .



plist' : " ?", " ?", " ?" . , .



, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .



. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist




. , plutil



JSON, - , .



NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization



, Cocoa.



, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.



: ( ) , .



- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }





, , , :



<?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> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>





, ? ! XML . :



{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }





.



:



- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }





? ! JSON-, ! , , : NSDictionary . .



, "mutable" , NSPropertyListMutableContainersAndLeaves



. NSPropertyListImmutable



, NSMutableDictionary, NSDictionary, .



, PLIST Cocoa. , , .



!



UPD: mejedi , plain-XML .



XML « », ( , «hello world» , ).



, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).








, , , <true/>, <false/>, , , , . , plist , .



- , , API .



plist' : " ?", " ?", " ?" . , .



, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .



. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist




. , plutil



JSON, - , .



NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization



, Cocoa.



, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.



: ( ) , .



- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }





, , , :



<?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> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>





, ? ! XML . :



{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }





.



:



- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }





? ! JSON-, ! , , : NSDictionary . .



, "mutable" , NSPropertyListMutableContainersAndLeaves



. NSPropertyListImmutable



, NSMutableDictionary, NSDictionary, .



, PLIST Cocoa. , , .



!



UPD: mejedi , plain-XML .



XML « », ( , «hello world» , ).



, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).








, , , <true/>, <false/>, , , , . , plist , .



- , , API .



plist' : " ?", " ?", " ?" . , .



, , , . : , . , , . : NSCoding NSData NSKeyedArchiver. NSData . .



. , bplist ( B inary Plist ), . : Xcode , XML , : plutil -convert xml1 MyFile.plist




. , plutil



JSON, - , .



NSUserDefaults, . , ~/Library/Preferences/com.yourcompany.yourapp.plist (, , , , bplist), . , ? NSPropertyListSerialization



, Cocoa.



, ? , NSDictionary NSArray NSData, plist. , , : NSData NSDictionary NSArray.



: ( ) , .



- (IBAction)savePlist:(id)sender { NSMutableDictionary *root = [NSMutableDictionary dictionary]; [root setObject:@YES forKey:@"autosave"]; [root setObject:@"hello" forKey:@"greet-text"]; [root setObject:@"4F4@@" forKey:@"identifier"]; NSMutableArray *elements = [NSMutableArray array]; [elements addObject:@"one"]; [elements addObject:@"two"]; [elements addObject:@"thee"]; [root setObject:elements forKey:@"elements"]; NSMutableArray *subs = [NSMutableArray array]; for (NSInteger i = 0; i < 10; i++) { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:[NSString stringWithFormat:@"John %ld", i] forKey:@"name"]; [dict setObject:[NSString stringWithFormat:@"Moscow %ld", i] forKey:@"city"]; [dict setObject:[NSNumber numberWithInteger:i] forKey:@"id"]; [subs addObject:dict]; } [root setObject:subs forKey:@"subs"]; NSLog(@"saving data:\n%@", root); NSError *error = nil; NSData *representation = [NSPropertyListSerialization dataWithPropertyList:root format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; if (!error) { BOOL ok = [representation writeToFile:self.plistFileName atomically:YES]; if (ok) { NSLog(@"ok!"); } else { NSLog(@"error writing to file: %@", self.plistFileName); } } else { NSLog(@"error: %@", error); } }





, , , :



<?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> <key>autosave</key> <true/> <key>elements</key> <array> <string>one</string> <string>two</string> <string>thee</string> </array> <key>greet-text</key> <string>hello</string> <key>identifier</key> <string>4F4@@</string> <key>subs</key> <array> <dict> <key>city</key> <string>Moscow 0</string> <key>id</key> <integer>0</integer> <key>name</key> <string>John 0</string> </dict> <dict> <key>city</key> <string>Moscow 1</string> <key>id</key> <integer>1</integer> <key>name</key> <string>John 1</string> </dict> <!-- --> </array> </dict> </plist>





, ? ! XML . :



{ autosave = 1; elements = ( one, two, thee ); "greet-text" = hello; identifier = "4F4@@"; subs = ( { city = "Moscow 0"; id = 0; name = "John 0"; }, { city = "Moscow 1"; id = 1; name = "John 1"; } ); }





.



:



- (IBAction)loadPlist:(id)sender { NSData *plistData = [NSData dataWithContentsOfFile:self.plistFileName]; if (!plistData) { NSLog(@"error reading from file: %@", self.plistFileName); return; } NSPropertyListFormat format; NSError *error = nil; id plist = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListMutableContainersAndLeaves format:&format error:&error]; if (!error) { NSMutableDictionary *root = plist; NSLog(@"loaded data:\n%@", root); } else { NSLog(@"error: %@", error); } }





? ! JSON-, ! , , : NSDictionary . .



, "mutable" , NSPropertyListMutableContainersAndLeaves



. NSPropertyListImmutable



, NSMutableDictionary, NSDictionary, .



, PLIST Cocoa. , , .



!



UPD: mejedi , plain-XML .



XML « », ( , «hello world» , ).



, — 10.6 , , 10.8 , - ( __CFBinaryPlistWrite).











All Articles