为什么我的NSMutableArray不写入plist?

时间:2011-09-05 04:49:51

标签: iphone objective-c cocoa-touch nsmutablearray plist

我有以下代码块:

NSMutableArray* mergedSymbolsArray = [NSMutableArray array];
                    for (NSDictionary* aSymbol in localSet) {

                        NSLog(@"Symbol:%@",[aSymbol valueForKey:@"symbol"]);
                        [mergedSymbolsArray addObject:aSymbol];

                    }

                    [Utils writeObjectToPList:mergedSymbolsArray]; 
                    tickers = [Utils getDataFromPList]; 

这是我读取/写入plist的代码:

+ (void)writeObjectToPList:(id)myData {  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];
    [myData writeToFile:path atomically:YES];
}  

+(NSMutableArray*)getDataFromPList
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mobile-watchlist.plist"];

    NSMutableArray* myArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
    NSLog(@"READING -- PList Data Count: %d", [myArray count]);
    return [[NSMutableArray alloc] initWithContentsOfFile:path];
}

由于某种原因,mergedSymbolsArray不会写入plist。我不确定为什么?

我可以将以下内容写入plist:

[tickers addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"GOOG", @"symbol", @"2044", @"id", nil]];
        [tickers addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"AAPL", @"symbol", @"686", @"id", nil]];
        [tickers addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"YHOO", @"symbol", @"4177", @"id", nil]];

        [Utils writeObjectToPList:tickers];

为什么第一个代码块没有写入plist?

其他说明: 以下是mergedSymbolsArray包含的字典示例:

  {
    "charts_count" = 2;
    "created_at" = "2010-04-12T16:37:32Z";
    exchange = NASDAQ;
    "followers_count" = 259;
    id = 8404;
    industry = "<null>";
    "messages_count" = 1436;
    ric = "GRPN.O";
    sector = "<null>";
    symbol = GRPN;
    title = Groupon;
    "updated_at" = "2011-09-05T04:17:56Z";
}

我猜是writeToFile:atomically方法失败因为无法写入?

4 个答案:

答案 0 :(得分:4)

确保您保存在数组中的所有对象都是属性列表对象(NSString,NSData,NSArray或NSDictionary)。

答案 1 :(得分:2)

失败,因为dta包含sector =“&lt; null&gt;”其值被解释为NSNull,不能写入plist。

在序列化之前,我将NSArray或NSDictionary转换为NSData。以下是nsarray上用于序列化和反序列化的类别。这舒适地处理一些数据是nsnull

-(BOOL)writeToPlistFile:(NSString*)filename{
    NSData * data = [NSKeyedArchiver archivedDataWithRootObject:self];
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
    BOOL didWriteSuccessfull = [data writeToFile:path atomically:YES];
    return didWriteSuccessfull;
}

+(NSArray*)readFromPlistFile:(NSString*)filename{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
    NSData * data = [NSData dataWithContentsOfFile:path];
    return  [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

答案 2 :(得分:0)

请检查[myData writeToFile:path atomically:YES];的返回值。如果它为NO,则在写入时会发生一些错误。尝试alloc-init NSMutableArray而不是创建自动释放的。

答案 3 :(得分:0)

尝试在文件名中添加正斜杠:

NSString *path = [documentsDirectory stringByAppendingString:@"/mobile-watchlist.plist"];