我如何从这些数组创建一个 plist 文件?

How can i create a plist file from these arrays?

我有一个 NSCollectionView 可以像这样读取数据:

    self.contents = @[
                  @{@"itemTitle":@"Product 1",
                    @"itemPrice":@".90",
                    @"itemImage":@"PastedGraphic-1.png"},

                  @{@"itemTitle":@"Product 2",
                    @"itemPrice":@".90",
                    @"itemImage":@"PastedGraphic-3.png"},

                  @{@"itemTitle":@"Product 3",
                    @"itemPrice":@"10.90",
                    @"itemImage":@"PastedGraphic-4.png"},

                  @{@"itemTitle":@"Product 4",
                    @"itemPrice":@"0.90",
                    @"itemImage":@"PastedGraphic-5.png"},

                  @{@"itemTitle":@"Product 5",
                    @"itemPrice":@"9.19",
                    @"itemImage":@"PastedGraphic-1.png"},

                  @{@"itemTitle":@"Product 6",
                    @"itemPrice":@"0.90",
                    @"itemImage":@"PastedGraphic-6.png"},

                  @{@"itemTitle":@"Product 7",
                    @"itemPrice":@"2.90",
                    @"itemImage":@"PastedGraphic-7.png"},

                  ];

我的问题是,如何将此代码转换为 plist 文件,以便使用此代码将 plist 文件的数据获取到 NSCollectionView 中:

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"legue" ofType:@"plist"];
self.contents = [[NSArray alloc] initWithContentsOfFile:plistPath];

您可以使用以下函数 (objective-C) 创建 plist 文件:

+ (void) writeToPlist {
    NSArray* contents = @[
                          @{@"itemTitle":@"Product 1",
                            @"itemPrice":@".90",
                            @"itemImage":@"PastedGraphic-1.png"},

                          @{@"itemTitle":@"Product 2",
                            @"itemPrice":@".90",
                            @"itemImage":@"PastedGraphic-3.png"},

                          @{@"itemTitle":@"Product 3",
                            @"itemPrice":@"10.90",
                            @"itemImage":@"PastedGraphic-4.png"},

                          @{@"itemTitle":@"Product 4",
                            @"itemPrice":@"0.90",
                            @"itemImage":@"PastedGraphic-5.png"},

                          @{@"itemTitle":@"Product 5",
                            @"itemPrice":@"9.19",
                            @"itemImage":@"PastedGraphic-1.png"},

                          @{@"itemTitle":@"Product 6",
                            @"itemPrice":@"0.90",
                            @"itemImage":@"PastedGraphic-6.png"},

                          @{@"itemTitle":@"Product 7",
                            @"itemPrice":@"2.90",
                            @"itemImage":@"PastedGraphic-7.png"},

                          ];

    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,

                                                         NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectoryPath
                          stringByAppendingPathComponent:@"legue.plist"];

    [contents writeToFile:filePath atomically: YES];
}

您可以创建一个新的 plist 文件并设计您的 plist 如下图:

NSString *filename = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
// your plist path
NSString *path = [filename stringByAppendingPathComponent:@"legue.plist"];
[contents writeToFile:path atomically:YES];
// test it
NSString *str = [[NSBundle mainBundle]pathForResource:@"legue.plist" ofType:nil];
NSLog(@"%@",str); // str = nil; 

所以你应该创建一个新的plist文件,然后你可以使用[NSBundle mainBundle]。