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

时间:2016-08-31 20:30:49

标签: ios objective-c nsarray plist nscollectionview

我有一个NSCollectionView,可以像这段代码一样读取数据:

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

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

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

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

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

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

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

                  ];

我的问题是,如何将此代码转换为plist文件,因此我可以使用以下代码将plist文件的数据导入NSCollectionView:

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

3 个答案:

答案 0 :(得分:0)

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

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

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

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

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

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

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

                          @{@"itemTitle":@"Product 7",
                            @"itemPrice":@"$632.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];
}

答案 1 :(得分:0)

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

enter image description here

答案 2 :(得分:0)

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]。