2013-10-22 29 views
0

我知道它的愚蠢的,但我要如何重建這個數組到的plist列表重新arraywithobjects是plist

candyArray = [NSArray arrayWithObjects: 
       [Candy candyOfCategory:@"chocolate" name:@"chocolate bar"], 
       [Candy candyOfCategory:@"chocolate" name:@"chocolate chip"], 
       [Candy candyOfCategory:@"chocolate" name:@"dark chocolate"], 
       [Candy candyOfCategory:@"hard" name:@"lollipop"], 
       [Candy candyOfCategory:@"hard" name:@"candy cane"], 
       [Candy candyOfCategory:@"hard" name:@"jaw breaker"], 
       [Candy candyOfCategory:@"other" name:@"caramel"], 
       [Candy candyOfCategory:@"other" name:@"sour chew"], 
       [Candy candyOfCategory:@"other" name:@"peanut butter cup"], 
       [Candy candyOfCategory:@"other" name:@"gummi bear"], nil]; 

感謝任何人的幫助!

+0

你想寫在plist相同嗎? –

+0

是的,請這將是非常感謝 –

+0

看看這個:http://stackoverflow.com/questions/9356929/how-to-save-nsmutable-array-into-plist-in-iphone – Alex

回答

0

試試這樣但在此之前包括你的陣列,然後在下面如下: -

NSFileManager *fileManager =[NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask,YES); 
     NSString *desktopDirectory = [paths objectAtIndex:0]; 
      NSString *path = [desktopDirectory stringByAppendingPathComponent:@"yourfile.plist"]; 
     NSMutableDictionary *mut=[NSMutableDictionary dictionary]; 

    [mut setObject:candyArray forKey:@"yourKey"]; 
     If ([mut writeToFile:path automatically:YES]) 
     { 
     NSLog(@"file created"); 
     } 
0

安迪,

侯賽因的答案是接近,但歸檔自定義類型時會失敗。

簡單的答案。自從使用自定義類(Candy)後,您必須採用NSCoding協議。執行以下任務initWithCoder,encodeWithCoder和默認存檔器將負責其餘部分。如果你想以其他格式存檔(二進制等),請參考NSData和NSCoder(NSKeyedArchiver)。 JSON也是另一個很好的精益選擇。

如果放棄自定義對象和在數組或字典中嵌套基元太麻煩了。對於複雜的自定義類型,這是不鼓勵的,並且會建立技術債務,但鼓勵用於簡單結構。希望分享Apple最佳實踐。

支持的plist類型(IOS)

的NSString, 的NSNumber, 的NSDate, NSData的, 的NSArray, 的NSDictionary

簡單的例子(NSArray中,NSDictionary的) https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/Archiving/Articles/serializing.html#//apple_ref/doc/uid/20000952-BABBEJEE

全參考文件用於定製對象(NSCoding) https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/Archiving/Archiving.html#//apple_ref/doc/uid/10000047i

+0

非常感謝您的幫助我會看看 –