されているオブジェクトにもかかわらず、「不変オブジェクトに送られたメソッドを変異さ」:は、私が<code>NSMutableDictionary</code>を使用して、このエラーを打ってるNSMutableDictionary
// Turn the JSON strings/data into objects
NSError *error;
NSMutableDictionary *invoiceDictFromReq = [[NSMutableDictionary alloc] init];
// invoiceDictFromReq = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:[request responseData] options:kNilOptions error:&error];
invoiceDictFromReq = [NSMutableDictionary dictionaryWithDictionary:[NSJSONSerialization JSONObjectWithData:[request responseData] options:kNilOptions error:&error]];
NSLog(@"invoiceDictFromReq count: %i, key: %@, value: %@", [invoiceDictFromReq count], [invoiceDictFromReq allKeys], [invoiceDictFromReq allValues]);
// Get values and keys from JSON response
self.invoiceDict = [invoiceDictFromReq objectForKey:@"invoice"];
NSNumber *invoiceAmount = [self.invoiceDict objectForKey:@"amount"];
NSNumber *invoiceId = [self.invoiceDict objectForKey:@"id"];
NSNumber *invoiceNumber = [self.invoiceDict objectForKey:@"number"];
NSNumber *checkoutStarted = [self.invoiceDict objectForKey:@"checkoutStarted"];
NSNumber *checkoutCompleted = [self.invoiceDict objectForKey:@"checkoutCompleted"];
NSLog(@"amount: %@, id: %@, number: %@, started: %@, completed: %@", invoiceAmount, invoiceId, invoiceNumber, checkoutStarted, checkoutCompleted);
すべてのコンソールログがあることを示す:
'NSInternalInconsistencyException', reason: '-[__NSCFDictionary removeObjectForKey:]: mutating method sent to immutable object'
ここではコードですデータは問題ありません。これは物事が崩壊し始めるところです。 Iは、次のビューコントローラにinvoiceDict
プロパティを渡す:
// Pass the invoice to checkoutViewController
[checkoutViewController setInvoiceDict:self.invoiceDict];
CheckoutViewController.mで:
// Change invoice checkoutCompleted to true
// [self.invoiceDict removeObjectForKey:@"checkoutCompleted"];
[self.invoiceDict setObject:[NSNumber numberWithBool:YES] forKey:@"checkoutCompleted"];
エラーが[self.invoiceDict setObject...]
です。私が使用している辞書はすべてNSMutableDictionary
であることを確認しました。私は試したことを示すコードにコメントアウトされた行を残し、レンガの壁に当たった。私はいつも新しい辞書を作ることができると思います。それを行うのが好ましい方法ですか?
おかげで、それ以下はパーサから可変辞書を取得する方法です同じエラーが表示されます。今、私はinvoiceDictFromReqを.hのプロパティとして使用し、.mでsyntehsizedし、self.invoiceDictFromReq = [NSMutableDictionary dictionary ...]としてデータに設定します。 – okysabeni
申し訳ありませんが、私は自分の答えを変更しました。それでも問題があるかどうかを確認してください。 invoiceDictFromReqはローカル変数なので、保持する必要はありませんが、そこにリークがあるので初めてinvoiceDictFromReqを作成する行は削除してください。 –
:-)。私は "あなたの問題はNSMutableDictionaryを作成しているのですが、mutableDictionary内の辞書をself.invoiceDictに設定していることです。これは必ずしもmutableDictionaryではありません。だから私のことは愚かです。それを指摘してくれてありがとう。ネイティブjsonパーサがNSMutableDictionaryを返すと仮定してはいけません。本当にありがとう! – okysabeni