Forte Zhu
Forte Zhu

Reputation: 762

setvalue:forkey: not working, app is crashing

I am working on an app. I tried to set value for particular key of NSDictionary using the following code,

NSMutableArray *testArray=[NSMutableArray arrayWithObjects:@“Jan Month",@“Feb Month",@“Mar Month",@“Apr Month",@“May Month",@“June Month",@“July Month",@“Aug Month",@“Sep Month",@“Oct Month", nil];

NSMutableArray *testResult=[NSMutableArray arrayWithObjects:@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass", nil];

NSDictionary *testDetails=[NSDictionary dictionaryWithObjects:testArray forKeys:testResult];

[testDetails setValue:@"Fail" forKey:@“Feb Month"];

My app is crashing at fourth line giving error,

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0x1741a2f40> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Feb Month.'

I tried solving this by checking,

po [self.testDetails valueForKey:"Feb Month"]
<extracting data from value failed>

Please help me in solving this.

Upvotes: 2

Views: 1659

Answers (4)

user3182143
user3182143

Reputation: 9609

Did you set object and key correctly? also Did you use NSDictionary and NSMutableDictionary correctly?

I checked your below code first.

NSMutableArray *testArray=[NSMutableArray arrayWithObjects:@"Jan Month",@"Feb Month",@"Mar Month",@"Apr Month",@"May Month",@"June Month",@"July Month",@"Aug Month",@"Sep Month",@"Oct Month", nil];
NSMutableArray *testResult=[NSMutableArray arrayWithObjects:@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass", nil];
NSDictionary *testDetails=[NSDictionary dictionaryWithObjects:testArray forKeys:testResult];
[testDetails setValue:@"Fail" forKey:@"Feb Month"];

Now see the result.There is only 1 key value pair

enter image description here

What your mistake here is you wrongly set the key and object as well as you need to set the value to NSMutableDictionary.

If you want to edit the values in dictionary you must use NSMutableDictionary as it is mutable.Using NSDictionary you cant edit because it is immutable.You can't change anything.

NSMutableArray *testArray=[NSMutableArray arrayWithObjects:@"Jan Month",@"Feb Month",@"Mar Month",@"Apr Month",@"May Month",@"June Month",@"July Month",@"Aug Month",@"Sep Month",@"Oct Month", nil];
NSMutableArray *testResult=[NSMutableArray arrayWithObjects:@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass",@"Pass", nil];
NSMutableDictionary *testDetails=[NSMutableDictionary dictionaryWithObjects:testResult forKeys:testArray];
[testDetails setValue:@"Fail" forKey:@"Feb Month"];

Now see the printed result

Printing description of testDetails:
{
  "Apr Month" = Pass;
  "Aug Month" = Pass;
  "Feb Month" = Fail;
  "Jan Month" = Pass;
  "July Month" = Pass;
  "June Month" = Pass;
  "Mar Month" = Pass;
  "May Month" = Pass;
  "Oct Month" = Pass;
  "Sep Month" = Pass;
}

Also see the below screenshot

enter image description here

You should know to use the dictionary first.

Apple NSDictionary API reference says

The NSDictionary class declares the programmatic interface to objects that manage immutable associations of keys and values. Use this class or its subclass NSMutableDictionary when you need a convenient and efficient way to retrieve data associated with an arbitrary key. NSDictionary creates static dictionaries, and NSMutableDictionary creates dynamic dictionaries. (For convenience, the term dictionary refers to any instance of one of these classes without specifying its exact class membership.)

Apple NSMutableDictionary API reference says

The NSMutableDictionary class declares the programmatic interface to objects that manage mutable associations of keys and values. It adds modification operations to the basic operations it inherits from NSDictionary.

NSDictionary vs NSMutableDictionary

Upvotes: 1

Niharika
Niharika

Reputation: 1198

Because from your code your NSDictionary testDetails will have only one value.

 testDetails:
{
    Pass = "Jan Month";
}

you need to add your array to dictionary key and value like this:

 NSMutableDictionary *testDetails = [NSMutableDictionary dictionaryWithObjects:testResult forKeys:testArray];

then replace the key value using this:

[testDetails setValue:@"Fail" forKey:@"Feb Month"];

Upvotes: 0

ios
ios

Reputation: 31

try this:

 NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:testResult,testArray, nil];

    [dict setObject:@"Fail" forKey:@"Feb Month"];

Upvotes: 1

Nirav D
Nirav D

Reputation: 72460

For of all you need to set testResult for objects and testArray for keys. Now if youIf you want to change the value of it you need to declare it as NSMutableDictionary.

NSMutableDictionary *testDetails = [NSMutableDictionary dictionaryWithObjects:testResult forKeys:testArray];
[testDetails setValue:@"Fail" forKey:@“Feb Month"];

Upvotes: 2

Related Questions