本文介绍了保存Array对的plist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一些项目存储到的plist 。这里是阵列环

 为(以项目ID OBJ)
的NSLog(@OBJ:%@,OBJ);

输出的NSLog:

  2013年3月27日13:00:40.072我的code [47436:C07] OBJ:红
2013年3月27日13:00:40.073我的code [47436:C07] OBJ:蓝
2013年3月27日13:00:40.073我的code [47436:C07] OBJ:绿

//该arrayWithObjects的作品。但我不知道如何(循环?)通过我的物品保存到plist文件...

 的NSArray *路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        的NSString * documentsDirectory = [路径objectAtIndex:0];
        * NSString的路径= [documentsDirectory stringByAppendingPathComponent:@data.plist];
    *的NSFileManager filemgr;
    filemgr =的NSFileManager defaultManager]    如果([filemgr fileExistsAtPath:路径]){
        的NSLog(@%@,路径);    *的NSMutableDictionary =的plist [[NSDictionary的dictionaryWithContentsOfFile:路径] mutableCopy]
    NSMutableArray里* newArray = [[[NSMutableArray里的alloc]初始化]自动释放];
    newArray = [NSArray的arrayWithObjects:@红,@绿色,@蓝无]。 //< --- WORKS!
    newArray = [NSArray的arrayWithObjects:项目,无] //<? -
    [plist中的setObject:newArray forKey:@你好];
    [的plist将writeToFile:路径原子:YES];
    [plist中释放];
    }其他{
        的NSLog(@找不到文件);
    }    [filemgr发布]


解决方案

也许这块code的帮助你。

 的NSMutableArray * newArray = [[[NSMutableArray里的alloc]初始化]自动释放];
NSMutableArray里* targetArray = [[[NSMutableArray里的alloc]初始化]自动释放];newArray = [NSArray的arrayWithObjects:@红,@绿色,@蓝无]。
的for(int i = 0; I< newArray.count;我++)
{
 [targetArray ADDOBJECT:[newArray objectAtIndex:我]];}
[plist中的setObject:targetArray forKey:@你好];
[的plist将writeToFile:路径原子:YES];

或另一种方法

 的NSMutableArray * targetArray = [[[NSMutableArray里的alloc]初始化]自动释放];为(以项目ID OBJ)
{
 [targetArray ADDOBJECT:项目];
 }
[plist中的setObject:targetArray forKey:@你好];
[的plist将writeToFile:路径原子:YES];

希望这有助于!

I'm trying to store some items into a pList. Here is the array loop:

for (id obj in items)
NSLog(@"obj: %@", obj);

Output NSLog:

2013-03-27 13:00:40.072 mycode[47436:c07] obj: Red
2013-03-27 13:00:40.073 mycode[47436:c07] obj: Blue
2013-03-27 13:00:40.073 mycode[47436:c07] obj: Green

// The arrayWithObjects works. But I don't know how to (loop?) through my items for saving into the plist file...

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];


    NSFileManager *filemgr;
    filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: path]) {
        NSLog(@"%@", path);

    NSMutableDictionary *plist = [[NSDictionary dictionaryWithContentsOfFile:path] mutableCopy];
    NSMutableArray *newArray = [[[NSMutableArray alloc] init] autorelease];
    newArray = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue" nil]; // <--- WORKS!
    newArray = [NSArray arrayWithObjects:items, nil]; // <-- ?
    [plist setObject:newArray forKey:@"Hi"];
    [plist writeToFile:path atomically:YES];
    [plist release];


    } else {
        NSLog(@"File not found");
    }

    [filemgr release];
解决方案

Maybe this piece of code helps you.

NSMutableArray *newArray = [[[NSMutableArray alloc] init] autorelease];
NSMutableArray *targetArray = [[[NSMutableArray alloc] init] autorelease];

newArray = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue" nil];
for(int i=0;i<newArray.count;i++)
{
 [targetArray addObject:[newArray objectAtIndex:i]];

}
[plist setObject:targetArray forKey:@"Hi"];
[plist writeToFile:path atomically:YES];

or another method

NSMutableArray *targetArray = [[[NSMutableArray alloc] init] autorelease];

for (id obj in items)
{
 [targetArray addObject:items];
 }
[plist setObject:targetArray forKey:@"Hi"];
[plist writeToFile:path atomically:YES];

Hope this helps !!!

这篇关于保存Array对的plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:43