2013-04-01 68 views
0

我有一個步進器,在它的值更改期間,我需要將值存儲在plist中。在plist文件中存儲值時出錯

所以根據saveAppPlist方法我需要通過的NSMutableDictionary與改變的文件 - > dictData

NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:dictData format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; 
if(plistData) 
{ 
    [plistData writeToFile:plistPath atomically:YES]; 
} 

這裏,它是步進的方法:

-(void)changeItemQuantityAtRow:(int)row 
         toValue:(double)value 
{ 
    NSMutableDictionary *item = [[items objectAtIndex:row] mutableCopy]; 
    [item setObject:[NSNumber numberWithDouble:value] forKey:@"Quantity"]; 
    [items setObject:item atIndexedSubscript:row]; 
    NSLog(@"%@", items); 
    NSMutableDictionary *plistDict = [NSDictionary dictionaryWithObjects:items forKeys:[NSArray arrayWithObjects:@"Name", @"Price", @"Quantity", nil]]; 
    NSLog(@"%@", plistDict); 
    [self saveAppPlist:plistDict]; 
} 

和的plist外觀像這樣(這只是4件中的一件):

<dict> 
    <key>Items</key> 
    <array> 
     <dict> 
      <key>Name</key> 
      <string>The zero item</string> 
      <key>Price</key> 
      <integer>133</integer> 
      <key>Quantity</key> 
      <integer>17</integer> 
     </dict> 

步進器正在改變曲線該項目的反義詞。但我有一個這樣的錯誤,當我試圖改變量的值:

'NSInvalidArgumentException', reason: '*** -[NSDictionary initWithObjects:forKeys:]: count of objects (4) differs from count of keys (3)' 

我理解錯誤的來源,但不知道如何解決這個問題。誰能幫我?還有一點 - 可能是在步進器每次敲擊後存儲數值都不太好。

+1

您的物品數組有4個物體,並且您給出了三個鍵,這些問題一旦顯示出您的物品數組數據。 – Balu

+0

@Sunny我看到了,但是邏輯上有些問題..我有4個Items對象,每個對象都有3個值..我只是想改變1個項目的數量..我該怎麼做? – ShurupuS

+0

解釋你想要的結構? – Balu

回答

2

忘記你想存儲一個數組,想象一下這個對象爲id實例。當你想存儲id items用鑰匙@"Items",你想做的事就是用

//[NSDictionary dictionaryWithObject:<#(id)#> forKey:<#(id<NSCopying>)#>] 
NSMutableDictionary *plistDict = [NSDictionary dictionaryWithObject:items forKey:@"Items"]; 
+0

它的工作原理,謝謝你!太好了! – ShurupuS

1

下面是簡單的方式,如果我想要寫在plist文件的一些數據

plist文件名是「 Contacts.plist「 存放值

  1. 名稱
  2. 地址
  3. 電話號碼

你可以找到編譯如何與plist文件上運行下面的鏈接

How to use PList files in iPhone Tutorial

下面的教程是代碼示例

- (void)saveDataInPlist { 

    //get the plist document path 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"Contacts.plist"];  

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSMutableDictionary *data = [[NSMutableDictionary alloc]init]; 
    NSMutableArray *contentArray= [[NSMutableArray alloc]init]; 

    if (![fileManager fileExistsAtPath: plistFilePath]) 
    { 
     NSLog(@"File does not exist"); 

     // If the file doesn’t exist, create an empty plist file   
     plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"Contacts.plist"]; 
     //NSLog(@"path is %@",plistFilePath); 

    } 
    else{ 
     NSLog(@"File exists, Get data if anything stored"); 

     contentArray = [[NSMutableArray alloc] initWithContentsOfFile:plistFilePath];  
    }  


    NSString *name = nameTextField.text; 
    NSString *address = addressTextField.text; 
    NSString *phone = phoneTextField.text; 


    //add values to dictionary 
    [data setValue:name forKey:@"Name"]; 
    [data setValue:address forKey:@"Address"]; 
    [data setValue:phone forKey:@"PhoneNo"]; 

    //add dictionary to array 
    [contentArray addObject:data]; 

    //write array to plist file 
    if([contentArray writeToFile:plistFilePath atomically:YES]){ 

     NSLog(@"saved"); 
    } 
    else { 
     NSLog(@"Couldn't saved"); 
    } 
    } 

} 

希望這會幫助你。

+0

謝謝你的鏈接 - 這非常有幫助! – ShurupuS

+0

@ShurupuS真的很高興知道,我付出的努力是值得的:) – swiftBoy

1

從我所看到的問題看,字典構造。 您提供items作爲值(每個項目可能包含Name,PriceQuantity鍵)以及您提供item鍵的鍵。 您可能需要將構造更改爲: [NSDictionary dictionaryWithObjects:@[items] forKeys:@[@"Items"]]

+0

我認爲這是一個決定,但現在我有這個..'NSInvalidArgumentException',原因:'*** - [NSDictionary initWithObjects:forKeys:]:對象(4)的數量與密鑰(1)的數量不同' – ShurupuS

+1

如果您使用了[[NSArray arrayWithObjects:items,nil]'作爲第一個參數,它將會起作用。 @ShurupuS請花幾分鐘時間閱讀所有這些方法的描述(如果您選擇了自動完成選項之一,然後點擊alt + click方法,則可以使用簡短描述),這將在未來幫助您很多。 –