cellForRowAtIndexPath Crash [duplicate] - iphone

This question already has answers here:
cellForRowAtIndexPath: crashes when trying to access arrays objectAtIndex:indexPath.row
(2 answers)
Closed 8 years ago.
I have a NSMutable array with data.
I want in the method "cellForRowAtIndexPath" fill the individual rows.
The table view keeps crashing and I do not know why.
The problem is in this line "cell.ergebnissLabel.text = [result objectAtIndex:indexPath.row]".
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = #"ErgebnisseCell";
ErgebnisseCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (indexPath.section == 0) {
NSDictionary*filtertNew = [self.nummeriertesArray objectAtIndex:0];
NSArray *result = [[filtertNew allKeys] objectAtIndex:1]
cell.ergebnissLabel.text = [result objectAtIndex:indexPath.row];
}
return cell;
}
The error message looks like this.
2014-02-28 17:32:11.208 Info[2451:70b] -[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x10950c110
2014-02-28 17:32:11.210 Info[2451:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x10950c110'
*** First throw call stack:
(
0 CoreFoundation 0x0000000101caf295 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000101a0d99e objc_exception_throw + 43
2 CoreFoundation 0x0000000101d4045d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000101ca0b8d ___forwarding___ + 973
4 CoreFoundation 0x0000000101ca0738 _CF_forwarding_prep_0 + 120
5 Info 0x000000010001f0da -[ErgebnisseTableViewController tableView:cellForRowAtIndexPath:] + 394
6 UIKit 0x000000010069574a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348
7 UIKit 0x000000010067bb1b -[UITableView _updateVisibleCellsNow:] + 2337
8 UIKit 0x000000010068cfc1 -[UITableView layoutSubviews] + 207
9 UIKit 0x0000000100621943 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
10 QuartzCore 0x000000010021cdf2 -[CALayer layoutSublayers] + 151
11 QuartzCore 0x0000000100211959 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
12 QuartzCore 0x00000001002117da _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
13 QuartzCore 0x00000001001856e4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 252
14 QuartzCore 0x000000010018675c _ZN2CA11Transaction6commitEv + 394
15 UIKit 0x00000001005c01f1 _UIApplicationHandleEventQueue + 10863
16 CoreFoundation 0x0000000101c3eb21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
17 CoreFoundation 0x0000000101c3e3f2 __CFRunLoopDoSources0 + 242
18 CoreFoundation 0x0000000101c5a26f __CFRunLoopRun + 767
19 CoreFoundation 0x0000000101c59b83 CFRunLoopRunSpecific + 467
20 GraphicsServices 0x0000000103587df5 GSEventRunModal + 161
21 UIKit 0x00000001005c2003 UIApplicationMain + 1010
22 Info 0x0000000100036143 main + 115
23 libdyld.dylib 0x00000001025a35fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Thanks.

This two line gives you an error:
NSArray *result = [[filtertNew allKeys] objectAtIndex:1]
[result objectAtIndex:indexPath.row];
// This gives you array of string
[filtertNew allKeys]
// This gives you string (second in array)
[[filtertNew allKeys] objectAtIndex:1]
//When you call that line you call
[result objectAtIndex:indexPath.row]
//You call objectAtIndex: method on NSString object but there is not method like that on NSString
I believe you want to do:
cell.ergebnissLabel.text = [[filtertNew allKeys] objectAtIndex:row]

Here's the problem. First, your code obtains an instance of NSDictionary from an array:
NSDictionary *filtertNew = [self.nummeriertesArray objectAtIndex:0];
On the next line, you obtain an array of the dictionary's keys, which in all likelihood are instances of NSString. You then access the second key in the array and store it in a variable named result.
NSArray *result = [[filtertNew allKeys] objectAtIndex:1]
Unfortunately, although the result variable is typed NSArray *, the value you're assigning is a string, not an array. Next, you send objectAtIndex: to that string, which crashes the app because NSString doesn't have an objectAtIndex: method.
cell.ergebnissLabel.text = [result objectAtIndex:indexPath.row];
That's why you're seeing the following error message on the console:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x10950c110'
This tells you that an instance of NSString was sent an objectAtIndex: message, and that since the instance didn't recognize the selector (i.e., the method name), it threw an exception.
However, you haven't included any information on what it is you're trying to do, so i would be difficult to tell you exactly what you'd need to do to get it working correctly.

Related

objective c - getting null values

I have been stuck on this for some time now but whats happening is that when the request comes back from mysql there is a null value but how do i check that a null value has hit my condition so that i dont store this value in the array. The code is below
The error i keep getting is -[NSNull isEqualToString:]: unrecognized and im trying to bind the array inside a tableview
- (void)viewDidLoad {
JobAddSiteAppDelegate *ja = (JobAddSiteAppDelegate *)[[UIApplication sharedApplication] delegate];
tempArray = [[NSMutableArray alloc] init];
NSString *strURL = [NSString stringWithFormat:#"http://www.bestitjobs.co.uk/appresults3.php", ""];
NSData *nsData = [NSData dataWithContentsOfURL:[NSURL URLWithString: strURL]];
//NSLog(#"JSON: %#", nsData);
//NSString *strResult = [[[NSString alloc] initWithData:nsData encoding:NSUTF8StringEncoding] autorelease];
//NSLog(#"Result: %#",strResult);
//NSError *e = nil;
JSONDecoder* decoder = [[JSONDecoder alloc] init];
NSDictionary* listDictionary = [nsData objectFromJSONData];
NSArray* people =[listDictionary objectForKey:#"jobs"];
for (NSDictionary *person in people) {
NSLog(#"people %#",people);
if([person objectForKey:#"position"] != [NSNull class])
{
NSString *position = [person objectForKey:#"position"];
NSLog(#"position %#",position);
[tempArray addObject: position];
}
}
self.listData = tempArray;
[tempArray release];
[listData release];
[super viewDidLoad];
}
Error
2012-12-06 12:22:11.314 JobAddSite[7718:207] position Senior Network and Systems Consultant
2012-12-06 12:22:11.316 JobAddSite[7718:207] position SAP Business One / SAP B1 Consultant London
2012-12-06 12:22:11.317 JobAddSite[7718:207] position Business Development Manager - Market Leading IT Company
2012-12-06 12:22:11.318 JobAddSite[7718:207] -[NSNull isEqualToString:]: unrecognized selector sent to instance 0xe565e8
2012-12-06 12:22:11.321 JobAddSite[7718:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull isEqualToString:]: unrecognized selector sent to instance 0xe565e8'
* Call stack at first throw:
(
0 CoreFoundation 0x00de95a9 exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f3d313 objc_exception_throw + 44
2 CoreFoundation 0x00deb0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d5a966 __forwarding + 966
4 CoreFoundation 0x00d5a522 _CF_forwarding_prep_0 + 50
5 JobAddSite 0x000024cd -[ResultViewController viewDidLoad] + 604
6 UIKit 0x00389089 -[UIViewController view] + 179
7 UIKit 0x0038aa3d -[UIViewController viewControllerForRotation] + 63
8 UIKit 0x00386988 -[UIViewController _visibleView] + 90
9 UIKit 0x0062893c -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 354
10 UIKit 0x0030081e -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 954
11 UIKit 0x00588619 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 1381
12 UIKit 0x0038d65d -[UIViewController presentModalViewController:withTransition:] + 3478
13 JobAddSite 0x0000213f -[JobAddSiteViewController search] + 270
14 UIKit 0x002d94fd -[UIApplication sendAction:to:from:forEvent:] + 119
15 UIKit 0x00369799 -[UIControl sendAction:to:forEvent:] + 67
16 UIKit 0x0036bc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
17 UIKit 0x0036a7d8 -[UIControl touchesEnded:withEvent:] + 458
18 UIKit 0x002fdded -[UIWindow _sendTouchesForEvent:] + 567
19 UIKit 0x002dec37 -[UIApplication sendEvent:] + 447
20 UIKit 0x002e3f2e _UIApplicationHandleEvent + 7576
21 GraphicsServices 0x01741992 PurpleEventCallback + 1550
22 CoreFoundation 0x00dca944 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52
23 CoreFoundation 0x00d2acf7 __CFRunLoopDoSource1 + 215
24 CoreFoundation 0x00d27f83 __CFRunLoopRun + 979
25 CoreFoundation 0x00d27840 CFRunLoopRunSpecific + 208
26 CoreFoundation 0x00d27761 CFRunLoopRunInMode + 97
27 GraphicsServices 0x017401c4 GSEventRunModal + 217
28 GraphicsServices 0x01740289 GSEventRun + 115
29 UIKit 0x002e7c93 UIApplicationMain + 1160
30 JobAddSite 0x00001dec main + 102
31 JobAddSite 0x00001d7d start + 53
)
terminate called after throwing an instance of 'NSException'
Just try this one:
if ([person objectForKey:#"position"]) {..}
This one is the same but maybe more readable:
if ([person objectForKey:#"position"] != nil) {..}
Hi Use the following code.
It is working Well.
Download Supporting Files
NSMutableArray *empArray = [[NSMutableArray alloc] init];
SBJSON *parser = [[SBJSON alloc] init];
NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.bestitjobs.co.uk/appresults3.php"]]];
NSLog(#"GGGG:%#",request1);
NSData *response1 = [NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response1 encoding:NSUTF8StringEncoding];
NSDictionary *statuses = [parser objectWithString:json_string error:nil];
NSLog(#"List Dictionary:%#",statuses);
NSArray* people =[statuses objectForKey:#"jobs"];
for(NSDictionary *test in people)
{
[empArray addObject:[test valueForKey:#"position"]];
}
NSLog(#"EMP ARRAY CATEGORY:%#",empArray);
Note: Before you Use SBJSon supporting files and import JSON.h
And check your console.
Check condition like this and add value to Array
for (NSDictionary *person in people) {
NSLog(#"people %#",people);
NSString *position = [person objectForKey:#"position"];
if(position) {
[tempArray addObject: position];
}
}

uncaught exception with dictionnary and label.text

I try to search in the dictionnary (passed trough an observer):
NSDictionary *myDictionnary = [NSDictionary dictionaryWithObjectsAndKeys:#"details",#"details de l'installation",#"recapitulatif",#"recapitulatif de l'installe",nil];
... to set the text in a label :
NSLog(#"key: %#, value: %#", recapitulatif, [dictionnary objectForKey:#"recapitulatif"]);
details.text = [dictionnary objectForKey:#"recapitulatif"];
But it make me an uncaught exception :
-[ThirdViewController objectForKey:]: unrecognized selector sent to instance 0x4b59c30
2012-01-09 15:07:13.179 emars[8185:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ThirdViewController objectForKey:]: unrecognized selector sent to instance 0x4b59c30'
*** Call stack at first throw:
(
0 CoreFoundation 0x00fb45a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01108313 objc_exception_throw + 44
2 CoreFoundation 0x00fb60bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00f25966 ___forwarding___ + 966
4 CoreFoundation 0x00f25522 _CF_forwarding_prep_0 + 50
5 emars 0x0000439d -[FirstViewController updateLabel:] + 134
6 Foundation 0x0002f669 _nsnote_callback + 145
7 CoreFoundation 0x00f8c9f9 __CFXNotificationPost_old + 745
8 CoreFoundation 0x00f0b93a _CFXNotificationPostNotification + 186
9 Foundation 0x0002520e -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
10 emars 0x0000880b -[ThirdViewController connectionDidFinishLoading:] + 541
Thanks to help me, I don't understand my mistake
EDIT:
Here is the whole method :
- (void)updateLabel:(NSNotification*)notification
{
NSDictionary *dictionnary = (NSDictionary*)[notification object];
NSLog(#"key: %#, value: %#", recapitulatif, [dictionnary objectForKey:#"recapitulatif"]);
}
And the Observer is send from "ThirdViewController" class :
NSDictionary *myDictionnary = [NSDictionary dictionaryWithObjectsAndKeys:#"details",#"details de l'installation",#"recapitulatif",#"recapitulatif de l'installe",nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"LABELUPDATENOTIFICATION" object:self userInfo:myDictionnary];
Thanks again !!!
EDIT 2:
- (void)updateLabel:(NSNotification*)notification
{
NSDictionary *dictionnary = (NSDictionary*)[notification userInfo];
NSLog(#"key: %#, value: %#", recapitulatif, [dictionnary objectForKey:#"recapitulatif"]);
NSLog(#"text: ", [dictionnary objectForKey:#"recapitulatif"]);
}
Give me
key: <UILabel: 0x4b2ff80; frame = (20 245; 280 133); text = 'Index du Compteur: 1 572,...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x4b30040>>, value: (null)
and
text:
This should give me #"recapitulatif de l'installe", isn't it ?
From the error you posted (but not seeing more of your code, which you should also post there), it appears that you are trying to do the objectForKey method on your ThirdViewController object (which is a view controller), not the actual NSDictionary you wanted to get your object from.
Make sure dictionnary is set properly when you are calling objectForKey on it.
Do this:
NSDictionary *dictionnary = (NSDictionary*)[notification userInfo];
instead of this:
NSDictionary *dictionnary = (NSDictionary*)[notification object];
I've linked Apple's NSDictionary userInfo documentation for you.

tableView crashes when going back from detail view iphone

in my application i have a table view and a detail view. The table view is populated from a database query. I have this database methods in the viewDidLoad method (i don't know where else to put it) Then i have the [self.tableView reload] method in the viewWillAppear method. The problem is that when i go back from the detail view to the table view and click the same element (in the table view) the application crashes. I guess it is because the database methods are still running when i come back from the detail view and i click on a cell. So the data in the table view is never reloaded. Do you guys have any idea on how to fix this?
*EDIT:
*Here's the code from the database, i use it to populate the table view:*
- (void)viewDidLoad
{
//Convert the course id to string
NSString *selectedCategoryIdConverted = [NSString stringWithFormat:#"%d", self.selectedCategory._id];
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
// Set up sqlite statement
sqlite3_stmt *dbStatement = nil;
NSString *sqlQuery = [NSString stringWithFormat:#"SELECT co.name, co.id, o.location FROM course as co JOIN categories as ca JOIN occasions as o WHERE co.catID = ca.id AND co.catID = %i AND o.courseID = co.id", self.selectedCategory._id];
//Convert the query string to const char
const char *sqlQueryConverted =[sqlQuery UTF8String];
int prepareSqlQuery = sqlite3_prepare_v2( [[DatabaseController sharedDatabaseController] getDb], sqlQueryConverted, -1, &dbStatement, NULL);
//Run the query
while ( sqlite3_step(dbStatement) == SQLITE_ROW )
{
const char *name = (const char *)sqlite3_column_text(dbStatement, 0);
int courseId = sqlite3_column_int(dbStatement, 1);
const char *location = (const char *)sqlite3_column_text(dbStatement, 2);
//Convert the returnedElement char to string
NSString *nameConverted = [[[NSString alloc] initWithUTF8String:name] autorelease];
NSString *locationConverted = [[[NSString alloc] initWithUTF8String:location] autorelease];
Course *course = [[Course alloc] initWithName:nameConverted _id:courseId location:locationConverted];
[tempArray addObject:course];
}
self.courses = tempArray;
[super viewDidLoad];
}
EDIT: I noticed that when i go back to the table view, the app won't crash if i click on the next element, but if i click on the previous element (row) in the table view the app crashes.
EDIT: I changed the code to the viewDidAppear method and fixed memory leaking. self.courses is (nonatomic, retain) here's the code:
- (void)viewWillAppear:(BOOL)animated
{
//Convert the course id to string
//NSString *selectedCategoryIdConverted = [NSString stringWithFormat:#"%d", self.selectedCategory._id];
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
// Set up sqlite statement
sqlite3_stmt *dbStatement = nil;
NSString *sqlQuery = [NSString stringWithFormat:#"SELECT co.name, co.id, o.location FROM course as co JOIN categories as ca JOIN occasions as o WHERE co.catID = ca.id AND co.catID = %i AND o.courseID = co.id", self.selectedCategory._id];
//Convert the query string to const char
const char *sqlQueryConverted =[sqlQuery UTF8String];
int prepareSqlQuery = sqlite3_prepare_v2( [[DatabaseController sharedDatabaseController] getDb], sqlQueryConverted, -1, &dbStatement, NULL);
//Run the query
while ( sqlite3_step(dbStatement) == SQLITE_ROW )
{
const char *name = (const char *)sqlite3_column_text(dbStatement, 0);
int courseId = sqlite3_column_int(dbStatement, 1);
const char *location = (const char *)sqlite3_column_text(dbStatement, 2);
//Convert the returnedElement char to string
NSString *nameConverted = [[[NSString alloc] initWithUTF8String:name] autorelease];
NSString *locationConverted = [[[NSString alloc] initWithUTF8String:location] autorelease];
Course *course = [[Course alloc] initWithName:nameConverted _id:courseId location:locationConverted];
[tempArray addObject:course];
[course release];
course = nil;
}
self.courses = tempArray;
[tempArray release];
[self.tableView reloadData];
[super viewWillAppear:animated];
}
EDIT: Here's is the error log:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceWhiteColor _id]: unrecognized selector sent to instance 0x4d2e2a0'
Call stack at first throw:
(
0 CoreFoundation 0x010435a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01197313 objc_exception_throw + 44
2 CoreFoundation 0x010450bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00fb4966 ___forwarding___ + 966
4 CoreFoundation 0x00fb4522 _CF_forwarding_prep_0 + 50
5 BookApp 0x000079e8 -[CoursesCategoriesViewController viewWillAppear:] + 184
6 UIKit 0x0017c9be -[UINavigationController _startTransition:fromViewController:toViewController:] + 858
7 UIKit 0x0017732a -[UINavigationController _startDeferredTransitionIfNeeded] + 266
8 UIKit 0x0017e562 -[UINavigationController pushViewController:transition:forceImmediate:] + 932
9 UIKit 0x001771c4 -[UINavigationController pushViewController:animated:] + 62
10 BookApp 0x000056d6 -[CoursesViewController tableView:didSelectRowAtIndexPath:] + 374
11 UIKit 0x00135b68 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
12 UIKit 0x0012bb05 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
13 Foundation 0x0084579e __NSFireDelayedPerform + 441
14 CoreFoundation 0x010248c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
15 CoreFoundation 0x01025e74 __CFRunLoopDoTimer + 1220
16 CoreFoundation 0x00f822c9 __CFRunLoopRun + 1817
17 CoreFoundation 0x00f81840 CFRunLoopRunSpecific + 208
18 CoreFoundation 0x00f81761 CFRunLoopRunInMode + 97
19 GraphicsServices 0x012d81c4 GSEventRunModal + 217
20 GraphicsServices 0x012d8289 GSEventRun + 115
21 UIKit 0x000ccc93 UIApplicationMain + 1160
22 BookApp 0x00001ff9 main + 121
23 BookApp 0x00001f75 start + 53
)
terminate called after throwing an instance of 'NSException'
From your stack trace, the interesting parts are :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceWhiteColor _id]: unrecognized selector sent to instance 0x4d2e2a0'
and
5 BookApp 0x000079e8 -[CoursesCategoriesViewController viewWillAppear:] + 184
which corresponds to :
- (void)viewWillAppear:(BOOL)animated {
//...
NSString *sqlQuery =
[NSString stringWithFormat:
#"SELECT co.name, co.id, o.location FROM course as co JOIN categories as ca JOIN occasions as o WHERE co.catID = ca.id AND co.catID = %i AND o.courseID = co.id",
self.selectedCategory._id]; // << The culprit
//...
You get this because self.selectedCategory._id tries to send an _id message to self.selectedCategory and fails, self.selectedCategory not being the good type.
How could this be possible?
Since I guess you are sure that self.selectedCategory is the expected type...
Simply because self.selectedCategory has been released, and its address has been reused for another variable... of another type, in your trace a UIColor instance... Could be anything else.
Conclusion
Double check your retain/release of that self.selectedCategory property :)
Side note
+1 for the FMDB suggestion... A must!
Your code is leaking memory. You are not putting release after assigning temp array to self.courses. Also you are not releasing course object after adding it to temp array. For SQLite operations I suggest you to use FMDB. It will be very easier to manage then putting SQL queries in each view controller to fetch the data from database. Also It's a bad practice to follow this kind of pattern. Use FMDB for all your sqlite purpose. Create a seperate class for all the sqlite communication.

NSDictionary parsing Exception in iphone

I am using this code and it is giving exception
NSMutableArray *streams = (NSMutableArray *)[feed valueForKey:#"comments"];
NSMutableArray *streams1 = (NSMutableArray *)[streams valueForKey:#"data"];
//NSMutableArray *streams2 = (NSMutableArray *)[streams1 valueForKey:#"message"];
// loop over all the stream objects and print their titles
int index;
NSMutableDictionary *stream;
for (index = 0; index < [feed count];index++) {
stream = (NSMutableDictionary *)[streams1 objectAtIndex:index];
NSLog(#"Name of sender is: %#", [stream valueForKey:#"message"]);
}
FaceBookTable *detailViewController = [[FaceBookTable alloc] initWithNibName:#"FaceBookTable" bundle:nil];
// ...
// Pass the selected object to the new view controller.
detailViewController.fbGraph = fbGraph;
detailViewController.dummyArray = [ feed valueForKey:#"message"];
detailViewController.dict = stream;
}
Exception is
-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5dae960
2011-06-15 16:14:07.835 MultiSocial[8042:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5dae960'
while my other code is working fine
NSMutableArray *streams = (NSMutableArray *)[feed valueForKey:#"from"];
lueForKey:#"message"];
// loop over all the stream objects and print their titles
int index;
NSMutableDictionary *stream;
for (index = 0; index < [feed count];index++) {
stream = (NSMutableDictionary *)[streams objectAtIndex:index];
NSLog(#"Name of sender is: %#", [stream valueForKey:#"message"]);
}
Please help
here is crash log
2011-06-15 17:05:42.148 MultiSocial[8583:207] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5d05f50
2011-06-15 17:05:42.156 MultiSocial[8583:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5d05f50'
*** Call stack at first throw:
(
0 CoreFoundation 0x013cabe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0151f5c2 objc_exception_throw + 47
2 CoreFoundation 0x013cc6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x0133c366 ___forwarding___ + 966
4 CoreFoundation 0x0133bf22 _CF_forwarding_prep_0 + 50
5 UIKit 0x00649405 -[UITableViewLabel setText:] + 84
6 MultiSocial 0x00046147 -[FaceBookTable tableView:cellForRowAtIndexPath:] + 467
7 UIKit 0x0045f7fa -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
8 UIKit 0x0045577f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
9 UIKit 0x0046a450 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
10 UIKit 0x00462538 -[UITableView layoutSubviews] + 242
11 QuartzCore 0x01e98451 -[CALayer layoutSublayers] + 181
12 QuartzCore 0x01e9817c CALayerLayoutIfNeeded + 220
13 QuartzCore 0x01e9137c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
14 QuartzCore 0x01e910d0 _ZN2CA11Transaction6commitEv + 292
15 QuartzCore 0x01ec17d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
16 CoreFoundation 0x013abfbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
17 CoreFoundation 0x013410e7 __CFRunLoopDoObservers + 295
18 CoreFoundation 0x01309bd7 __CFRunLoopRun + 1575
19 CoreFoundation 0x01309240 CFRunLoopRunSpecific + 208
20 CoreFoundation 0x01309161 CFRunLoopRunInMode + 97
21 GraphicsServices 0x0190f268 GSEventRunModal + 217
22 GraphicsServices 0x0190f32d GSEventRun + 115
23 UIKit 0x003fa42e UIApplicationMain + 1160
24 MultiSocial 0x00002740 main + 102
25 MultiSocial 0x000026d1 start + 53
)
Well as per the crash log -[__NSArrayI isEqualToString:] there is some mistake in your code. It is clearly visible in the crash report that isEqualToString method is for NSString object but in your code it is called on NSArray.
Just debug your app and you will find the solution.
UPDATE
//Make sure that feed return an array for the key comments
NSArray *streams = (NSArray *)[feed valueForKey:#"comments"];
NSArray *streams1 = (NSArray *)[streams valueForKey:#"data"];
NSLog(#"comments : %#",streams);
//When you are running loop only last index value will be copied into your dictionary named stream so remove the loop.
FaceBookTable *detailViewController = [[FaceBookTable alloc] initWithNibName:#"FaceBookTable" bundle:nil];
// ...
// Pass the selected object to the new view controller.
detailViewController.fbGraph = fbGraph;
detailViewController.dummyArray = [ feed valueForKey:#"message"];
detailViewController.dict = streams;
}
//Your tableView cellForRowAtIndexPath method should look like this
//First get the dictionary
if(dict){
NSDictionary *stream = (NSDictionary *)[dict objectAtIndex:indexPath.row];
if(stream){
NSDictionary * messages = (NSDictionary *[stream valueForKey:#"Message"];
NSString *comment = [messages valueForKey:#"comment"];
cell.textLabel.text = comment;
}
}
u have been assigning the NSString object to NSArray.so do debug with breakpoints
NSMutableArray *arr = [[[feed valueForKey:#"comments"]valueForKey:#"data" ]valueForKey:#"id"];
for (NSString* cid in [arr objectAtIndex:0]) {
NSLog(#"cid is : %# \n\n",cid);
}

AddressBook Crash, only with some contacts

My app crashes, it is a problem that arises from the AddressBook API, it only happens with some contacts.
Here is the log:
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x00000102, 0x316ebd38
Crashed Thread: 0
Thread 0 Crashed:
0 CoreFoundation 0x00001cfe CFRetain + 90
1 AddressBook 0x00004b2c ABCMultiValueCopyValueAtIndex + 28
2 AddressBook 0x0001066a ABMultiValueCopyValueAtIndex + 2
3 Call Monitor 0x00003824 -[CallAppDelegate peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:] (AppDelegate.m:408)
4 AddressBookUI 0x00032cfc -[ABPeoplePickerNavigationController personViewController:shouldPerformDefaultActionForPerson:property:identifier:withMemberCell:] + 152
5 AddressBookUI 0x0003b8ce -[ABPersonViewControllerHelper personTableViewDataSource:selectedPropertyAtIndex:inPropertyGroup:withMemberCell:forEditing:] + 222
6 AddressBookUI 0x0004a17c -[ABPersonTableViewDataSource valueAtIndex:selectedForPropertyGroup:withMemberCell:forEditing:] + 40
7 AddressBookUI 0x00048c00 -[ABPersonTableViewDataSource tableView:didSelectRowAtIndexPath:] + 316
8 UIKit 0x00091f40 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 656
9 UIKit 0x0009db40 -[UITableView _userSelectRowAtIndexPath:] + 124
10 Foundation 0x00086c86 __NSFireDelayedPerform + 362
11 CoreFoundation 0x00071a54 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 8
12 CoreFoundation 0x00073ede __CFRunLoopDoTimer + 854
13 CoreFoundation 0x0007485e __CFRunLoopRun + 1082
14 CoreFoundation 0x0001d8e4 CFRunLoopRunSpecific + 224
15 CoreFoundation 0x0001d7ec CFRunLoopRunInMode + 52
16 GraphicsServices 0x000036e8 GSEventRunModal + 108
17 GraphicsServices 0x00003794 GSEventRun + 56
18 UIKit 0x000062a0 -[UIApplication _run] + 396
19 UIKit 0x00004e10 UIApplicationMain + 664
20 Call Monitor 0x000028e8 main (main.m:14)
21 Call Monitor 0x000028b8 start + 32
This is driving me crazy, as it only happens with an isolated number of contacts.
Any help would be greatly appreciated.
Here is the code that was requested, thank you very very much for your help:
(It is an extract from the method where I think the error happens)
The weird thing is that it only happens with certain contacts, and deleting the contact, then creating a new one solves the problem...
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
NSString* firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString* lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSNumber* record = [NSNumber numberWithInt:ABRecordGetRecordID(person)];
if(lastName!=nil){
name=[NSString stringWithFormat:#"%# %#",firstName,lastName];
}
else {
name=firstName;
}
ABMultiValueRef phone = ABRecordCopyValue(person, property);
NSString *value =(NSString *)ABMultiValueCopyValueAtIndex(phone, identifier);
NSString *label =(NSString *)ABMultiValueCopyLabelAtIndex(phone, identifier);
NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"MainArray"]];
NSDictionary *stringDictionary = [NSDictionary dictionaryWithObjectsAndKeys:name, kLabelKey, value, kNumberKey,label, kNumberLabelKey,record, kReferenceKey, nil];
[tempArray addObject:stringDictionary];
NSArray *mainArray = [NSArray arrayWithArray:tempArray];
[[NSUserDefaults standardUserDefaults] setObject:mainArray forKey:#"MainArray"];
To anyone experiencing a similar problem:
My error came from the fact that I was using:
ABMultiValueCopyValueAtIndex with an identifier instead of an index.
You have to call ABMultiValueGetIndexForIdentifier and then use that index on the ABMultiValueCopyValueAtIndex.
The bottom line is Identifier!=index.
Instead of using
ABMultiValueCopyValueAtIndex(multiValue, identifier);
Which Zebs pointed out above, use...
ABMultiValueCopyValueAtIndex(multiValue, ABMultiValueGetIndexForIdentifier(multiValue, identifier));
I would make sure you're really dealing with valid multi value record type.
if (ABMultiValueGetPropertyType(phone) != kABInvalidPropertyType) {
// Do work here.
}
Actually, it would probably be safer to make sure it's really a string.
if (ABMultiValueGetPropertyType(phone) == kABMultiStringPropertyType) {
// Do work here.
}
I don't know why this wouldn't be the case. Maybe you have corrupted contacts entries? Or maybe sometimes the phone number isn't a multi value type?