2012-04-04 49 views
0

我有這樣的plist我創建增加值代入我的plist

enter image description here

我已經寫了我的大部分控制器類得到這個plist中,並將其裝入文檔目錄因此它可以讀取/寫入是。

目前我的閱讀工作正常,而且我以前也有寫作的工作,但是我最近剛剛將其中一個對象(緩存值)更改爲與此值相關的字典。現在,當我嘗試寫入這個plist時,我的應用程序崩潰了。

這是我得到的錯誤。

2012-04-05 09:26:18.600 mycodeTest [874:F803] *終止應用程序由於 未捕獲的異常 'NSInvalidArgumentException',原因:「* - [NSDictionary的initWithObjects:forKeys:]:物體(4)的計數從密鑰計數不同(5)」 ***第一擲調用堆棧:(0x12cc022 0x1884cd6 0x1248417 0x12685e2 0x19844 0x17e86 0x17669 0x13b67 0xe53a49 0xe51e84 0xe52ea7 0xe51e3f 0xe51fc5 0xd96f5a 0x1d2aa39 0x1df7596 0x1d21120 0x1df7117 0x1d20fbf 0x12a094f 0x1203b43 0x1203424 0x1202d84 0x1202c9b 0x21aa7d8 0x21aa88a 0x450626 0x77ed 0x1e35 0x1)終止呼叫投擲 exceptionCurrent語言:auto;目前的目標-c

考慮到所有這一切,我現在將向您展示我的方法,當它具有可以保存的值時從另一個類中調用該方法。

//This method gets called from another class when it has new values that need to be saved 
- (void) saveData:(NSString *)methodName protocolSignature:(NSString *)pSignature protocolVersion:(NSNumber *)pVersion requestNumber:(NSNumber *)rNumber dataVersionReturned:(NSNumber *)dvReturned cacheValue:(NSMutableDictionary *)cValue 
{ 
    // get paths from root direcory 
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
    // get documents path 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    // get the path to our Data/plist file 
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"EngineProperties.plist"]; 

    // set the variables to the values in the text fields that will be passed into the plist dictionary 
    self.protocol = pSignature; 
    self.Version = pVersion; 
    self.request = rNumber; 
    self.dataVersion = dvReturned; 

    //if statment for the different types of cacheValues 
    if (methodName == @"GetMan") 
    { 
     //cache value only returns the one cachevalue depending on which method name was used 
     [self.cacheValue setValue:cValue forKey:@"Man"]; //do I need to have the other values of cacheValue dictionary in here? if so how do I do that. 
     c 
    } 
    else if (methodName == @"GetMod") 
    { 
     [self.cacheValue setValue:cValue forKey:@"Mod"]; 
    } 
    else if (methodName == @"GetSubs") 
    { 
     [self.cacheValue setValue:cValue forKey:@"Subs"]; 
    } 


    // This is where my app is falling over and giving the error message 
    // create dictionary with values in UITextFields 
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: protocol, pVersion, rNumber, dvReturned, cacheValue, nil] forKeys:[NSArray arrayWithObjects: @"Signature", @"Version", @"Request", @"Data Version", @"Cache Value", nil]]; 

    NSString *error = nil; 
    // create NSData from dictionary 
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; 

    // check is plistData exists 
    if(plistData) 
    { 
     // write plistData to our Data.plist file 
     [plistData writeToFile:plistPath atomically:YES]; 

     NSString *myString = [[NSString alloc] initWithData:plistData encoding:NSUTF8StringEncoding]; 
     NSLog(@"%@", myString); 
    } 
    else 
    { 
     NSLog(@"Error in saveData: %@", error); 
//  [error release]; 
    } 
} 

當錯誤是說,4個鍵5時有所不同,據我可以告訴我申請的5個值到字典中的任何幫助,將不勝感激,我升技丟失。

編輯**另一件事調試我的問題是什麼時候貌似我沒有得到我我cacheValue字典正確設置它的顯示0鍵valuepairs事實上,我注意到???這是對還是錯? enter image description here

這是當我登錄我在Xcode的plist的建議之下,當我使用[NSDictionary dictionaryWithObjectsAndKeys:..etc

Check setup is everything there? 
Temp Dic output = { 
    Root =  { 
     "Cache Value" =   { 
      Manu = 0; 
      Mod = 0; 
      Sub = 0; 
     }; 
     "Data Version returned" = 0; 
     "Signature" = null; 
     "Version" = 0; 
     "Request Number" = 0; 
    }; 

Run Man cache check results 
Temp Dic output = { 
    "Version returned" = 5; 
    "Signature" = Happy; 
    "Version" = 1; 
    "Request Number" = 4; 

,你可以看到緩存的價值是完全丟失我已經運行了請求之後會發生什麼。

回答

0

我猜測cacheValue在碰撞發生時爲零,導致values陣列中只有4個對象,而keys中只有5個對象。

請嘗試使用[NSDictionary dictionaryWithObjectsAndKeys:]代替。

+0

我已經嘗試過這種按照你的建議,你可以看看我的上面更新的問題我已經添加了輸出...它就像我失去了我的緩存值。 – 2012-04-04 23:18:53

0

在這樣的情況下,分解你的代碼。用臨時變量在單獨的行上執行每個部分。

把你的鑰匙和你的價值觀到臨時數組。

地段一切的價值觀,或者在調試器中設置斷點並檢查所有你的價值觀。 Eli幾乎可以肯定cacheValue是零。 arrayWithObjects方法在第一個零上停止。

此代碼:

NSString *string1 = @"string 1"; 
NSString *string2 = @"string 2"; 
NSString *string3 = @"string 3"; 
NSString *string4 = nil; 
NSString *string5 = @"string 5"; 


NSArray *anArray = [NSArray arrayWithObjects: 
    string1, 
    string2, 
    string3, 
    string4, 
    string5, 
    nil]; 

NSLog(@"anArray has %d elements", [anArray count]); 

將只顯示數組中的3個元素,即使arrayWithObjects線出現加5元

+0

好的謝謝..我想我在代碼atm迷路了。 – 2012-04-05 01:54:21