Dict is coming from notification, taking out the NSData from dict and adding it to NSMutableArray is crashing the application.
Once in a while this crash is happening not always.
Dict is coming from notification, taking out the NSData from dict and adding it to NSMutableArray is crashing the application.
Once in a while this crash is happening not always.
You can directly get the data to array there is no need to cast.
if(self.RFTagData != nil){
self.RFTagData = [dict objectForKey:@"obj"];
}
NSLog(@"array %@", RFTagData);
This will add all data to array under the obj
key.
Update:
As user rmaddy & danh suggested, so here needs to take concern over this point regarding use of valueForKey
and objectForKey
methods and nil check on the array
.
objectForKey
: This is an NSDictionary
method. An NSDictionary is a collection class similar to an NSArray
(collections), except instead of using indexes like NSArray
, it uses keys to differentiate between items. A key is an arbitrary string you provide. No two objects can have the same key (just as no two objects in an NSArray
can have the same index).
valueForKey
: This is a KVC method. It works with ANY class. valueForKey: allows you to access a property using a string for its name.
Here both returns the value associated with a given key, so here using valueForKey
method provides workaround solution to you. But using objectForKey
is the more preferred way to use in such cases.
To check for the null
values inside array which are identically appears like literals @"<null>"
rather then NSNull
objects typically used to represent nil
s in Cocoa collections. You can filter them out by using NSArray's
filteredArrayUsingPredicate
method:
NSPredicate *pred = [NSPredicate predicateWithBlock:^BOOL(id value, NSDictionary *unused) {
return ![str isEqualToString:@"<null>"];
}];
NSArray *filteredAry = [self.RFTagData filteredArrayUsingPredicate:pred];
NSLog(@"array with non null vals %@", filteredAry);
NSData *data=[dict objectForKey:@"obj"]; [self.RFTagData addObject:data];
You can directly add data object by doing this.Instead of converting to string.
Don't type cast NSData to NSString when adding objects into array.You should first convert NSData into NSString then add it to array.So better way to use this NSData into NSString and add NSString into array.
NSData *data=[dict objectForKey:@"obj"];
NSString *strData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(data != nil self.RFTagData != nil)
{
[self.RFTagData addObject:strData];
.....
}