Iphone 为什么';我的NSMUTABLEARRY不能写入plist吗?
我有以下代码块:Iphone 为什么';我的NSMUTABLEARRY不能写入plist吗?,iphone,objective-c,cocoa-touch,nsmutablearray,plist,Iphone,Objective C,Cocoa Touch,Nsmutablearray,Plist,我有以下代码块: NSMutableArray* mergedSymbolsArray = [NSMutableArray array]; for (NSDictionary* aSymbol in localSet) { NSLog(@"Symbol:%@",[aSymbol valueForKey:@"symbol"]); [mergedSymbolsAr
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];
}
[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];
由于某种原因,mergedSymbolsArray
不会写入plist。我不知道为什么?
我能够将以下内容写入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];
}
[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";
}
{
“图表计数”=2;
“创建于”=“2010-04-12T16:37:32Z”;
交易所=纳斯达克;
“追随者数量”=259;
id=8404;
行业=”;
“邮件数量”=1436;
ric=“GRPN.O”;
部门=”;
符号=GRPN;
标题=Groupon;
“更新时间”=“2011-09-05T04:17:56Z”;
}
我猜writeToFile:atomically方法失败,因为无法写入?检查
[myData writeToFile:path atomically:YES]的返回值代码>请。如果为否,则在写入时发生了一些错误。请尝试alloc init NSMutableArray,而不是创建一个自动删除的数组。检查[myData WriteFile:path Atomicaly:YES]的返回值代码>请。如果不是,则在写入时发生了一些错误。请尝试alloc init NSMutableArray,而不是创建自动删除的NSMutableArray。尝试在文件名中添加正斜杠,即:
NSString *path = [documentsDirectory stringByAppendingString:@"/mobile-watchlist.plist"];
尝试在文件名中添加正斜杠,即:
NSString *path = [documentsDirectory stringByAppendingString:@"/mobile-watchlist.plist"];
确保数组中保留的所有对象都是属性列表对象(NSString、NSData、NSArray或NSDictionary)。确保数组中保留的所有对象都是属性列表对象(NSString、NSData、NSArray或NSDictionary)。由于dta包含扇区=“”其值被解释为NSNull,无法写入plist
在序列化之前,我将NSArray或NSDictionary转换为NSData。以下是nsarray上用于序列化和反序列化的类别。此ComfortabyBy可处理某些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];
}
它失败是因为dta包含扇区=“其值被解释为NSNull,并且无法写入plist
在序列化之前,我将NSArray或NSDictionary转换为NSData。以下是nsarray上用于序列化和反序列化的类别。此ComfortabyBy可处理某些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];
}
我检查了writeToFile:是否以原子方式正确返回。不幸的是,对于我的情况,它没有返回。如何进一步诊断?@Sheehan调用writeToFile:时path的值是多少?目录是否存在?目录是否存在。path的值是/Users/syalam/Library/Application Support/iPhone Simulator/4.3.2/Applications/2C2B2FDF-B376-43FA-AA95-49921B3DC3B4/Documents/mobile watchlist.plist
I检查writeToFile:atomically返回是否正确。不幸的是,对于我的情况,它没有返回。如何进一步诊断?@Sheehan调用writeToFile:时path的值是多少?目录是否存在?目录是否存在。path的值是/Users/syalam/Library/Application Support/iPhone Simulator/4.3.2/Applications/2C2B2FDF-B376-43FA-AA95-49921B3DC3B4/Documents/mobile watchlist.plist
stringByAppendingPathComponent
它会自动运行吗幸运的是,这并不能解决我的问题。仍然无法通过追加PathComponent来编写pliststringByAppendingPathComponent
是否自动执行。幸运的是,这并不能解决我的问题。仍然无法写入plist+(void)writeObjectToPList:(id)myData。尝试将(id)更改为NSMutableArray。。。。type@booleanboy我将其更改为NSMutableArray,令人遗憾的是,在将数据添加到MergedSymbolArray后,使用NSLog检查数据是否确实存储了…并尝试使用alloc而不是autorelease array…writeToFile实际返回否。我更改为alloc而不是autorelease,同样的结果。为什么writeToFile返回NO?请尝试将原子设置为NO+(void)WriteObject TopList:(id)myData。尝试将(id)更改为NSMutableArray。。。。type@booleanboy我将其更改为NSMutableArray,令人遗憾的是,在将数据添加到MergedSymbolArray后,使用NSLog检查数据是否确实存储了…并尝试使用alloc而不是autorelease array…writeToFile实际返回否。我更改为alloc而不是autorelease,同样的结果。为什么writeToFile返回NO?尝试将Atomicaly设置为NotAnks来执行此操作。它确实解决了我的问题:)我错过了NSKeyedArchiver,我想我很快会读到更多关于它的内容isA:)再次感谢您的帮助:)非常感谢它确实解决了我的问题:)我错过了NSKeyedArchiver,我想我很快就会读到更多关于它的信息(伊莎:)再次感谢你的帮助:)