-1

enter image description here

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.

Mridul Gupta
  • 599
  • 5
  • 18
  • 10
    why dont you convert the NSdata into string in a btter way ? Casting Nsdata into Nsstring is not the right way to do ! – Teja Nandamuri Feb 07 '17 at 14:31
  • @TejaNandamuri is right you should use `NSString *myString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];` – Ludovic Feb 07 '17 at 14:39
  • @Mridul add your original code here instead of image, just edit. – vaibhav Feb 07 '17 at 14:45
  • The reason of your crash is `self.RFTagData` make sure it is `strong`. – iphonic Feb 07 '17 at 15:08
  • The code doesn't appear able to crash on the line highlighted, but there it is in the image. I think better guess is along the lines of threading. Certainly, preforming the table update on a notification (which may not have been posted on the main thread is crash-ably dangerous). – danh Feb 07 '17 at 15:23
  • @Mridul Gupta why aren't you adding the object as NSData? – Ahmed Sahib Feb 08 '17 at 07:20

3 Answers3

0

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 nils 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);
vaibhav
  • 4,038
  • 1
  • 21
  • 51
  • Why are you using `valueForKey:` instead of `objectForKey:`? Don't use KVC unless there is a clear, specific reason to use it. – rmaddy Feb 07 '17 at 15:01
  • Also, you're suggesting that the OP *assign* the array, not add to it? Also, the nil check on the array is superfluous. – danh Feb 07 '17 at 15:20
  • Thanks for the answer , but it is not solving crash – Mridul Gupta Feb 07 '17 at 17:37
  • @rmaddy regards, for your useful concern kindly check the updated ans thanks. – vaibhav Feb 08 '17 at 07:41
  • @danh regards, for your useful concern kindly check the updated ans thanks. – vaibhav Feb 08 '17 at 07:41
  • @MridulGupta i assume you are getting crash just because of getting null, now please check my updated ans and as i already pointed this please post original code not as image. – vaibhav Feb 08 '17 at 07:43
0

NSData *data=[dict objectForKey:@"obj"]; [self.RFTagData addObject:data];

You can directly add data object by doing this.Instead of converting to string.

Ammu
  • 41
  • 7
0

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];
   .....
}

Example for Converting Data into String

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39