开发者

Why doesn't my NSMutableArray write to plist?

I have the following block of code:

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

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

                    }

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

Here is my code to read/write to a plist:

+ (void)writeObjectToPList:(id)myData {  
    NSArray *paths = N开发者_StackOverflow中文版SSearchPathForDirectoriesInDomains(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];
}

For some reason mergedSymbolsArray will not write to a plist. I am not sure why?

I am able to write the following into a 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];

Why doesn't the first block of code write to a plist?

ADDITIONAL NOTES: Here is an example of a dictionary the mergedSymbolsArray contains:

  {
    "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";
}

I am guessing the writeToFile:atomically method is failing because cannot be written?


Make sure all the objects you keep in the array are the property list objects (NSString, NSData, NSArray, or NSDictionary).


It fails because dta contains sector = "<null>" whose value is interpreted as NSNull and cannot be written to a plist.

I convert NSArray or NSDictionary to NSData before serializing. Following is a category on nsarray for serializing and deserializing. This comfortableby handles some data being 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];
}


Check the return value of [myData writeToFile:path atomically:YES]; please. If it's NO, some errors have occurred while writing.Try to alloc-init the NSMutableArray instead of creating an autoreleased one.


Try adding a forward slash to your file name Ie:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜