2012-08-29 38 views
0

可能重複:
iPhone - Writing NSMutableDictionary to file在Cocoa應用程序的主束創建屬性列表文件編程

如何以編程方式創建一個屬性列表文件可可應用的MainBundle內。我知道如何讀取和寫入plist文件的內容。

+0

您無法修改MainBundle中的內容。 – 0xDE4E15B

+0

可能的重複 - http://stackoverflow.com/questions/3984722/how-to-write-nsmutabledictionary-into-plist,triplicate - http://stackoverflow.com/questions/5361630/iphone-writing-nsmutabledictionary-to-文件 – Jeremy1026

+0

這不是重複的,所有其他比較是iOS,OP想知道關於可可。雖然相似,但它們並不相同 – TigerCoding

回答

0

我將數組寫入plist並使用類似於下面的代碼將其讀出。可能有用。

- (NSString *)offlineCachePath { 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *cache = [paths objectAtIndex:0]; 
    NSString *path = [cache stringByAppendingPathComponent:@"NameOfApp"]; 

    // Check if the path exists, otherwise create it 
    if (![fileManager fileExistsAtPath:path]) { 
     [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; 
    } 

    return path; 
} 

- (NSString *)cachedStuffPath { 
    NSString *cachedStuffPath = [[self offlineCachePath] stringByAppendingPathComponent:@"NameOfFile.plist"]; 
    return cachedStuffPath; 
} 

- (NSMutableArray *)getCachedStuff { 
    return [[NSArray arrayWithContentsOfFile:[self cachedStuffPath]] mutableCopy]; 
} 

- (void)cacheStuff:(NSMutableArray *)stuff { 
    [stuff writeToFile:[self cachedStuffPath] atomically:YES]; 
} 
相關問題