Reputation: 10245
I have passed an NSArray value back to a parent view using a delegate.
it is received in the parent view like so
- (void) setManufactureSearchFields:(NSArray *)arrayValues withIndexPath:(NSIndexPath *)myIndexPath
{
manufactureSearchObjectString = [arrayValues valueForKey:@"MANUFACTURER"];
NSLog(@"%@",[arrayValues valueForKey:@"MANUFACTURER"]);
manufactureResultIndexPath = myIndexPath;
[self.tableView reloadData]; //reloads the tabels so you can see the value in the tableViewCell.
}
If i was to execute this code and select the tablecell of the childview it obviously executes the delegate but then freezes up and fires this error to the Log
2011-10-31 14:06:16.670 code[12610:207] (
"Alfa Romeo"
)
2011-10-31 14:06:16.673 code[12610:207] *** -[__NSArrayI isEqualToString:]: message sent to deallocated instance 0x6859280
However if I comment out the following line in my delegate method
- (void) setManufactureSearchFields:(NSArray *)arrayValues withIndexPath:(NSIndexPath *)myIndexPath
{
//manufactureSearchObjectString = [arrayValues valueForKey:@"MANUFACTURER"];
NSLog(@"%@",[arrayValues valueForKey:@"MANUFACTURER"]);
manufactureResultIndexPath = myIndexPath;
[self.tableView reloadData]; //reloads the tabels so you can see the value in the tableViewCell.
}
it dose not crash and I end up with the NSLog with the correct details in my log like so.
2011-10-31 14:09:28.200 code[12737:207] (
Sony
)
I am hoping someone has experianced such a problem before, this is what my array object looks like, its a dictionary of values.
ISELECT = F;
ISALIVE = T;
MANUFACTURER = Sony;
MANUFACTURERID = 3;
Upvotes: 1
Views: 2661
Reputation: 7973
please convert it to NSString.
NSString *manufact=[NSString stringWithFormat:@"%@",[arrayValues valueForKey:@"MANUFACTURER"];
It will not get crash.
Upvotes: 0
Reputation: 9162
valueForKey:
returns an autoreleased instance. Unless you retain it, you can expect it will be deallocated sometime after you return from the current method. It looks like you are assigning it to an instance variable. If you do that, you need to retain it.
manufactureSearchObjectString = [arrayValues valueForKey:@"MANUFACTURER"];
[manufactureSearchObjectString retain];
But it looks like you have another problem.
[arrayValues valueForKey:@"MANUFACTURER"];
That returns an array.
manufactureSearchObjectString = [arrayValues valueForKey:@"MANUFACTURER"];
By the name of your variable, it looks as though you're assigning it to a string variable. So if you fix the retain issue, then you will have another error. You will get an unrecognized selector when you try to call isEqualToString on it.
You need to assign a string value to manufactureSearchObjectString. You need to figure out what string value you want that to be. In this case you only have one string in your array, so I guess you want that one. In that case
manufactureSearchObjectString = [[arrayValues valueForKey:@"MANUFACTURER"] objectAtIndex:0];
[manufactureSearchObjectString retain];
But in general you need to check if there is more than one value in the array, and decide which one you want, and also check if there are no values in the array, and do something correct to handle that.
Upvotes: 1
Reputation: 7663
Try changing your code into this:
- (void) setManufactureSearchFields:(NSDictionary *)arrayValues withIndexPath:(NSIndexPath *)myIndexPath
{
manufactureSearchObjectString = [[arrayValues valueForKey:@"MANUFACTURER"] copy];
NSLog(@"%@",[arrayValues valueForKey:@"MANUFACTURER"]);
manufactureResultIndexPath = myIndexPath;
[self.tableView reloadData]; //reloads the tabels so you can see the value in the tableViewCell.
}
Please note that I've also changed the variable type of arrayValues
to NSDictionary
. Let me know if that works for you.
Upvotes: 0