0

我知道它很愚蠢,但我如何将此数组重新创建为 plist 列表

    candyArray = [NSArray arrayWithObjects:
              [Candy candyOfCategory:@"chocolate" name:@"chocolate bar"],
              [Candy candyOfCategory:@"chocolate" name:@"chocolate chip"],
              [Candy candyOfCategory:@"chocolate" name:@"dark chocolate"],
              [Candy candyOfCategory:@"hard" name:@"lollipop"],
              [Candy candyOfCategory:@"hard" name:@"candy cane"],
              [Candy candyOfCategory:@"hard" name:@"jaw breaker"],
              [Candy candyOfCategory:@"other" name:@"caramel"],
              [Candy candyOfCategory:@"other" name:@"sour chew"],
              [Candy candyOfCategory:@"other" name:@"peanut butter cup"],
              [Candy candyOfCategory:@"other" name:@"gummi bear"], nil];

感谢任何人的帮助!

4

2 回答 2

0

Try like this but before that include your array and then follow below:-

NSFileManager *fileManager =[NSFileManager defaultManager];
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask,YES);
        NSString *desktopDirectory = [paths objectAtIndex:0];
          NSString *path = [desktopDirectory stringByAppendingPathComponent:@"yourfile.plist"];
         NSMutableDictionary *mut=[NSMutableDictionary dictionary];

     [mut setObject:candyArray forKey:@"yourKey"];
      If ([mut writeToFile:path automatically:YES])
       {
       NSLog(@"file created");
        }
于 2013-10-22T15:49:28.717 回答
0

安迪,

侯赛因的答案很接近,但在归档自定义类型时会失败。

简单的回答。由于您使用自定义类(Candy),因此您必须采用 NSCoding 协议。实现以下任务 initWithCoder、encodeWithCoder 和默认存档器将负责其余的工作。如果您想以其他格式(二进制等)存档,请参考 NSData 和 NSCoder (NSKeyedArchiver)。JSON 也是另一个不错的精益选择。

如果这太麻烦了,请放弃自定义对象并将原语嵌套在数组或字典中。这对于复杂的自定义类型是不鼓励的,并且会产生技术债务,但对于简单的结构是鼓励的。想分享 Apple 的最佳实践。

支持的 plist 类型 (iOS)

NSString、NSNumber、NSDate、NSData、NSArray、NSDictionary

简单示例(NSArray、NSDictionary) https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/Archiving/Articles/serializing.html#//apple_ref/doc/uid/20000952-BABBEJEE

自定义对象的完整参考文档 (NSCoding) https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/Archiving/Archiving.html#//apple_ref/doc/uid/10000047i

于 2013-10-22T15:55:04.083 回答