0
嗨我實現了這個對象PlistManager來寫/讀plist在我的項目中。問題是plist被填充好會運行(我希望),當進入後臺(I使用通知和NSlog [Plistmanager readPlist:@「Database」])我仍然可以讀取之前輸入的數據。寫給plist但是當我退出模擬時它是空的
問題是當我停止對xcode的模擬並再次運行時,plist現在是空的。爲什麼?
@interface ReadWritePlist : NSObject
+(void)writeToPlist:(NSString*)filePlist dic:(NSDictionary*)dic;
+ (NSMutableDictionary *)readPlist:(NSString*)filePlist;
@end
@implementation ReadWritePlist
+ (void)writeToPlist:(NSString*)filePlist dic:(NSDictionary*)dic
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[dic writeToFile:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",filePlist]] atomically:YES];
}
+ (NSMutableDictionary *)readPlist:(NSString*)filePlist {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [[documentsDirectory stringByAppendingPathComponent:filePlist] stringByAppendingPathExtension:@"plist"];
NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
return dic;
}
解決
我剛剛發現這些新的方法進行讀/寫plist中,它是working.I必須添加里面的appdelegate的 「initializeFileDic」 方法(didFinishLaunchingWithOptions):
@implementation PlistManager
+ (NSMutableDictionary *)readPlist:(NSString*)filePlist {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [[documentsDirectory stringByAppendingPathComponent:filePlist] stringByAppendingPathExtension:@"plist"];
NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
return dic;
}
+ (void)writeToPlist:(NSString*)filePlist dic:(NSDictionary*)dic
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if ([dic writeToFile:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",filePlist]] atomically:YES]) {
NSLog(@"Ok done!");
}
else{
NSLog(@"Noo not done!");
}
}
+ (void)initializeFileDic:(NSString*)FileNameExt
{
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *startPath = [[path stringByAppendingPathComponent:FileNameExt]stringByAppendingPathExtension:@"plist"] ;
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [[documentsDirectoryPath stringByAppendingPathComponent:FileNameExt]stringByAppendingPathExtension:@"plist"];
NSLog(@"%@ \n,\n %@ ",startPath,filePath);
if([NSDictionary dictionaryWithContentsOfFile: startPath]){
NSLog(@"File %@ exists",filePath);
}
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
if([[NSDictionary dictionaryWithContentsOfFile:startPath] writeToFile:filePath atomically:NO])
NSLog(@"File copied inside App DocumentsDirectory\n\n");
else
NSLog(@"Error while trying to copy into DocumentsDirectory\n\n");
}
}
@end
有人可以解釋什麼是錯? 謝謝
您是否知道NSDictionary的[writeToFile方法](http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#// apple_ref/occ/instm/NSDictionary/writeToFile:atomically :)返回成功結果?檢查是否爲YES或NO。另外,你在你的字典中記錄什麼類型的東西?這些項目中的任何一個是否定製了Objective C對象? –
你使用ARC嗎? – CarlJ
positive ..爲什麼你問我? – luca