NSMutable Array returning unrecognised values - ios

I am using an NSMutable Array as a datasource to UITableView. I am fetching the values from database and adding it to mutable array. When the view loads I populate the table view with the mutable array and it loads fine. Later when I click a cell it throws an error saying unrecognised selector sent to instance
Inside array I am storing a person object with a name property. I am loading all cells with person.name Later when I try to disolay person.name as the user clicks each cell it shows error.
This is how I do
Person *per = (Person*)[my arr objectAtIndex:indexPath.row];
NSlog(#"%#",per.name);
Here it crashes. But I am using the same code inside cellforRowAtIndexPath for displaying the person name.
Sometimes the error says Unrecognised selector name sent to NSArray
again sometime it says Unrecognised selector name sent to
UIDeviceRGBColor
So it means I am not getting correct object back from my NSMutable Array
How can I do this properly?
Thanks

This is due to the array is dealloacated. Make sure that the array is retained properly. In ARC have a strong reference. In non-ARC make sure you retain the array (I recommend to use property).

Related

-[__NSCFDictionary Title]: unrecognized selector sent to instance

I have created an application that populates a table from JSON data I pull from my local host. I am able to search through this data using a search bar and have added a button to add the rows to another table which I am using a library for a users favourite objects. So now that Ive this all working I'm trying to create a detail view that when a user clicks on a row in the table they are brought to a new view controller and then they can view the full details of the object as the rows in the initial table only have the title and author listed in each row.
I have made the Segue and created the view controller ect and when I try to populate a few UILabels on the detaiViewController I get a error:
Maybe papers dictionary store others NSDictionary, not Paper object. When you parse a JSON, it will parse to iOS SDK class, such as [] parse to NSArray, "" parse to NSString, {} parse to NSDictionary.
So, I guess your JSON will parse to an array of dictionary. It cannot return a Paper for you. You have to assign values in each dictionary to one your Paper, and put it in a NSArray of Paper.
Change:
self.Title.text = self.paper.Title;
to:
self.Title.text = self.paper[#"Title"];
Use awakeFromNib instead of viewDidLoad
NSMutableArray *locations = [[NSMutableArray alloc] init];
_papers = [json objectForKey:#"Papers"];
You are populating the locations array from retrieving elements from _papers. Actually _papers holds list of NSDictionaries and when in prepareForsegue you just get an element from _papers which is a dictionary so thats why it is giving error. You have to store this locations array and retrieve elements from it because you are parsing the json into paper in the loop and putting it in locations hope it helps.

How to check for valid object in Objective-C?

I have inherited code that parses from a web service and fills in a NSDictionary. Under some circumstances, which I am going to explore, the parser returns a NSDictionary with count of 5, but the objects are invalid. Any message sent to them fails with unrecognized selector, even isKindOfClass: fails.
When I po the object in console, I see this:
error: Execution was interrupted, reason: Attempted to dereference an invalid
ObjC Object or send it an unrecognized selector.
The process has been returned to the state before expression evaluation.
How can I check that the object is invalid, if isKindOfClass: does not work here?
Here is the output from the console during the crash. You see dictionary with 5 empty objects, po on object 9 returns the invalid message.
Here is the po of the big dictionary:
I hope I will be able to check the issue in the parser, but I am also interested in how can I check for that invalid object to prevent the crash.
The problem is that you are trying to access an element of dictionary using an integer, if the object is a dictionary, the key should be any obj-c object that comforms to copy protocol. You are using 0 as an integer index, primitive types aren't obj-c objects, you should wrap it into an NSNumber.
The other issue is that the NSJSONSerializer returns an id type object, basing on the structure of the parsed JSON this could be a dictionary or an array, you should always inspect the returned object to check the type. I usually always expect an arrays of dictionaries, if the system returns just a dictionary, I create an array on the fly with just that object.
your po wants an array - but it is a dictionary.
this wont work
a dictionary can not be indexed with integers as it doesnt have objectForSubscriptIndex

NSArray containsObject method inconsistent results

I have an NSMutableArray called selectedUsers to which I am adding objects using a method called addUser. The objects being added are most often of type PLManagedUser (a core data managed object) although sometimes the object could be a string. In any case, if the array already contains the object, I do not want to add it to the array. Here is the code:
- (void)addUser:(id)user withTitle:(NSString *)title {
if (![_selectedUsers containsObject:user]) {
[_selectedUsers addObject:user];
}
}
I have noticed that if I try to add the same user back to back using the above method, the containsObject catches it, and duplicates are not added. However, if I add the same user (with the same memory address) after having added other objects in between, the duplicate will be added.
I am printing the contents of the array each time I add something to confirm that the duplicate objects are in the array.
My question is, is there any obvious reason why containsObject isn't consistently working here?
You need to provide the ability for an object to identify itself as equal to another object of the same class, and to do this you implement the isEqual: and hash methods.
Having said that, the explanation in your question is the opposite of what I would have expected.

NSMutableArray remove Object is cloning another object in the Array

I have an strange error after removing an object in a NSMutableArray.
If i remove an object with
[Array removeObjectAtIndex:2];
the array changed to this:
why are there now 2 ContactViewControllers??
lldb lies sometimes. It gets confused and will display the incorrect information. Try doing
po Array
you should see the correct array contents then.

Core Data object fault - NSManagedObject attributes returns nil

Recently I had a problem with such scenario:
In My NSManagedObject I stored longName attribute, called save on NSManagedObjectContext and after a while I close the application. Save ended without an error.
I restarted the application, I fetched the object and I tried to get [object longName]. The value which was returned was nil. When I stopped there with breakpoint and when I tried to display the value by po [object longName] proper value was returned and printed. ALl next calles to [object longName] in sources where correct. Whenever I don't use breakpoints there the value was always nil.
I think I hit the same problem recently in other application. I fetched the user object which contains NSSet of flights objects. When I tried to use [[flight validFrom] intValue] it returns number 0, but when I tried to print it with #"%#" format I've got (null).
The strange thing is that problem occurred not on every object.

Resources