I have parsed JSON in tableview and I want to implement search bar to filter huge data. I have tried implementing search bar from here.
but it crashes.
-(void)parsingmethod{
NSString *string = [NSString stringWithFormat:#"HEREISMYJSONURL"];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.json=[responseObject valueForKey:#"name"];
self.filteredCandyArray = [NSMutableArray arrayWithCapacity:[candyArray count]];
self.filteredArray=[NSMutableArray arrayWithArray:self.json];
NSLog(#"self.flteredarray=%#",self.filteredArray);
self.current_address = [responseObject valueForKey:#"current_address"];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(#"Oops, something went wrong: %#", [error localizedDescription]);
}];
[operation start];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text =[self.filteredArray objectAtIndex:indexPath.row];
} else {
cell.textLabel.text =[self.json objectAtIndex:indexPath.row];
}
cell.detailTextLabel.text=[self.current_address objectAtIndex:indexPath.row];
return cell;
}
And content filtering and delegate method also done from here
Below my Crash Logs
***** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0xcc515f0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(
0 CoreFoundation 0x021af1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0199c8e5 objc_exception_throw + 44
2 CoreFoundation 0x0223efe1 -[NSException raise] + 17
3 Foundation 0x0165cc7a -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 282
4 Foundation 0x015c9dfd _NSGetUsingKeyValueGetter + 81
5 Foundation 0x015c9437 -[NSObject(NSKeyValueCoding) valueForKey:] + 260
6 Foundation 0x0159a465 -[NSFunctionExpression expressionValueWithObject:context:] + 1260
7 Foundation 0x016202c1 -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 248
8 Foundation 0x016201c1 -[NSPredicate evaluateWithObject:] + 48
9 Foundation 0x01620141 _filterObjectsUsingPredicate + 418
10 Foundation 0x0161fefa -[NSArray(NSPredicateSupport) filteredArrayUsingPredicate:] + 328
11 LuckyHR 0x00043995 -[NameListTableViewController filterContentForSearchText:scope:] + 245
12 LuckyHR 0x00043bf2 -[NameListTableViewController searchDisplayController:shouldReloadTableForSearchString:] + 386
13 UIKit 0x00a1eb37 -[UISearchDisplayController searchBar:textDidChange:] + 128
14 UIKit 0x0093dc56 -[UISearchBar(UISearchBarStatic) _searchFieldEditingChanged] + 178
15 libobjc.A.dylib 0x019ae82b -[NSObject performSelector:withObject:] + 70
16 UIKit 0x0065e3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
17 UIKit 0x0065e345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
18 UIKit 0x0075fbd1 -[UIControl sendAction:to:forEvent:] + 66
19 UIKit 0x0075ffc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
20 UIKit 0x00d8529d -[UITextField fieldEditorDidChange:] + 221
21 UIKit 0x00765db4 -[UIFieldEditor textInputDidChange:] + 58
22 UIKit 0x00d931b3 -[UITextInputController _sendDelegateChangeNotificationsForText:selection:] + 118
23 UIKit 0x00d95090 -[UITextInputController _insertText:fromKeyboard:] + 768
24 UIKit 0x00d95954 -[UITextInputController insertText:] + 372
25 UIKit 0x00768a64 -[UIFieldEditor insertText:] + 1086
26 UIKit 0x00d88ffa -[UITextField insertText:] + 59
27 UIKit 0x00848760 -[UIKeyboardImpl insertText:] + 87
28 UIKit 0x00859ea4 -[TIKeyboardOperationInsertText(UIKeyboardImpl) main] + 83
29 Foundation 0x01670c79 -[__NSOperationInternal _start:] + 671
30 Foundation 0x015ed9c8 -[NSOperation start] + 83
31 UIKit 0x00846d4d -[UIKeyboardImpl performOperations:] + 153
32 UIKit 0x00844f8e -[UIKeyboardImpl continueHandleKeyboardInputWithOperations:] + 75
33 UIKit 0x00844ddc __73-[UIKeyboardImpl replyHandlerForHandleKeyboardInputWithExecutionContext:]_block_invoke_2 + 44
34 UIKit 0x00dae978 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 402
35 libobjc.A.dylib 0x019ae82b -[NSObject performSelector:withObject:] + 70
36 Foundation 0x015f0e48 __NSThreadPerformPerform + 285
37 CoreFoundation 0x0213877f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
38 CoreFoundation 0x0213810b __CFRunLoopDoSources0 + 235
39 CoreFoundation 0x021551ae __CFRunLoopRun + 910
40 CoreFoundation 0x021549d3 CFRunLoopRunSpecific + 467
41 CoreFoundation 0x021547eb CFRunLoopRunInMode + 123
42 GraphicsServices 0x031d45ee GSEventRunModal + 192
43 GraphicsServices 0x031d442b GSEventRun + 104
44 UIKit 0x0065cf9b UIApplicationMain + 1225
45 LuckyHR 0x000499fd main + 141
46 libdyld.dylib 0x0392a70d start + 1
47 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) *
I went to similar questions posted here but still not figured it out.
It's clearly written :
3 Foundation 0x0165cc7a -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 282
4 Foundation 0x015c9dfd _NSGetUsingKeyValueGetter + 81
5 Foundation 0x015c9437 -[NSObject(NSKeyValueCoding) valueForKey:] + 260
You ask for a key that doesn't exist so it's not working.
Related
I am trying to set up a UITableView inside of a UIViewController. I am using storyboard. but when running it on the simulator, the tableview does not display. Here is the simple view in Xcode:
#implementation ForthViewController{
NSArray *menuItems;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSArray *o = [prefs mutableArrayValueForKey:#"option"];
NSLog(#"%#",o);
menuItems =[NSArray arrayWithArray:o];
[self viewrequest];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [menuItems objectAtIndex:indexPath.row];
return cell;
}
I am getting following error:
2016-11-29 11:57:44.987 Wellness_24x7[1400:46606] -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x7a67d160
2016-11-29 11:57:45.010 Wellness_24x7[1400:46606] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x7a67d160'
*** First throw call stack:
(
0 CoreFoundation 0x00b4da14 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0060ee02 objc_exception_throw + 50
2 CoreFoundation 0x00b56d63 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x00a946bd ___forwarding___ + 1037
4 CoreFoundation 0x00a9428e _CF_forwarding_prep_0 + 14
5 UIKit 0x01884c1c -[UITableViewLabel setText:] + 120
6 Wellness_24x7 0x000f16c4 -[ForthViewController tableView:cellForRowAtIndexPath:] + 372
7 UIKit 0x014df398 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 822
8 UIKit 0x014df4e1 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 90
9 UIKit 0x014af948 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3347
10 UIKit 0x014ce0d6 __29-[UITableView layoutSubviews]_block_invoke + 52
11 UIKit 0x014e919e -[UITableView _performWithCachedTraitCollection:] + 88
12 UIKit 0x014cdfab -[UITableView layoutSubviews] + 214
13 UIKit 0x01424008 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 810
14 libobjc.A.dylib 0x00623059 -[NSObject performSelector:withObject:] + 70
15 QuartzCore 0x0569280a -[CALayer layoutSublayers] + 144
16 QuartzCore 0x056864ee _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
17 QuartzCore 0x05694d2a -[CALayer(CALayerPrivate) layoutBelowIfNeeded] + 44
18 UIKit 0x014117c0 -[UIView(Hierarchy) layoutBelowIfNeeded] + 1258
19 UIKit 0x014112c2 -[UIView(Hierarchy) layoutIfNeeded] + 82
20 UIKit 0x01585420 -[UITabBarController _layoutViewController:] + 845
21 UIKit 0x0158557a -[UITabBarController _wrapperViewForViewController:] + 262
22 UIKit 0x01591ba6 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 787
23 UIKit 0x01590a0e -[UITabBarController transitionFromViewController:toViewController:] + 76
24 UIKit 0x0158c0dd -[UITabBarController _setSelectedViewController:] + 453
25 UIKit 0x0158bee4 -[UITabBarController setSelectedViewController:] + 252
26 UIKit 0x01590839 -[UITabBarController _tabBarItemClicked:] + 587
27 libobjc.A.dylib 0x006230b5 -[NSObject performSelector:withObject:withObject:] + 84
28 UIKit 0x01359b79 -[UIApplication sendAction:to:from:forEvent:] + 118
29 UIKit 0x01359af8 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
30 UIKit 0x017a70fb -[UITabBar _sendAction:withEvent:] + 525
31 libobjc.A.dylib 0x006230b5 -[NSObject performSelector:withObject:withObject:] + 84
32 UIKit 0x01359b79 -[UIApplication sendAction:to:from:forEvent:] + 118
33 UIKit 0x01359af8 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
34 UIKit 0x014f98f8 -[UIControl sendAction:to:forEvent:] + 79
35 UIKit 0x014f9c78 -[UIControl _sendActionsForEvents:withEvent:] + 408
36 UIKit 0x014f9938 -[UIControl sendActionsForControlEvents:] + 48
37 UIKit 0x017ace41 -[UITabBar(Static) _buttonUp:] + 123
38 libobjc.A.dylib 0x006230b5 -[NSObject performSelector:withObject:withObject:] + 84
39 UIKit 0x01359b79 -[UIApplication sendAction:to:from:forEvent:] + 118
40 UIKit 0x01359af8 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
41 UIKit 0x014f98f8 -[UIControl sendAction:to:forEvent:] + 79
42 UIKit 0x014f9c78 -[UIControl _sendActionsForEvents:withEvent:] + 408
43 UIKit 0x014f8c7e -[UIControl touchesEnded:withEvent:] + 714
44 UIKit 0x013d6182 -[UIWindow _sendTouchesForEvent:] + 1095
45 UIKit 0x013d7220 -[UIWindow sendEvent:] + 1159
46 UIKit 0x0137bf93 -[UIApplication sendEvent:] + 266
47 UIKit 0x01351668 _UIApplicationHandleEventQueue + 7802
48 CoreFoundation 0x00a676ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
49 CoreFoundation 0x00a5d38b __CFRunLoopDoSources0 + 523
50 CoreFoundation 0x00a5c7a8 __CFRunLoopRun + 1032
51 CoreFoundation 0x00a5c0e6 CFRunLoopRunSpecific + 470
52 CoreFoundation 0x00a5befb CFRunLoopRunInMode + 123
53 GraphicsServices 0x05133664 GSEventRunModal + 192
54 GraphicsServices 0x051334a1 GSEventRun + 104
55 UIKit 0x01357bfa UIApplicationMain + 160
56 Wellness_24x7 0x000cba6a main + 138
57 libdyld.dylib 0x033c6a21 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Change this line
cell.textLabel.text = [menuItems objectAtIndex:indexPath.row];
with this
cell.textLabel.text = [NSString stringWithFormat:#"%#",[menuItems objectAtIndex:indexPath.row]];
Problem - The reason of the crash is cell.textLabel.text is expecting a NSString value and you are passing NSCFNumber in it so you need to convert NSCFNumber to NSString
Below is my sample error
2016-02-10 13:02:15.335 redminete[5804:56271]
-[NSArrayI length]: unrecognized selector sent to instance 0x7fd0e3483470 2016-02-10 13:02:15.361 redminete[5804:56271] ***
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSArrayI length]:
unrecognized selector sent to instance 0x7fd0e3483470'
*** First throw call stack: ( 0 CoreFoundation 0x000000010ac89c65 __exceptionPreprocess + 165 1 libobjc.A.dylib
0x000000010a922bb7 objc_exception_throw + 45 2 CoreFoundation
0x000000010ac910ad -[NSObject(NSObject) doesNotRecognizeSelector:] +
205 3 CoreFoundation 0x000000010abe713c
___forwarding_ + 988 4 CoreFoundation 0x000000010abe6cd8 _CF_forwarding_prep_0 + 120 5 UIKit
0x000000010b258e9c -[UILabel _ensureBaselineMetricsReturningBounds] +
138 6 UIKit 0x000000010b259082
-[UILabel _firstBaselineOffsetFromTop] + 36 7 UIKit 0x000000010b70003d -[UIView(AdditionalLayoutSupport)
nsli_lowerAttribute:intoExpression:withCoefficient:forConstraint:] +
1287 8 Foundation 0x000000010a4be9ae
lower_1_attribute + 158 9 Foundation
0x000000010a4be4c4 -[NSLayoutConstraint
_lowerIntoExpression:reportingConstantIsRounded:] + 102 10 Foundation 0x000000010a4b441e
-[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 96 11 UIKit 0x000000010b6fd8d4 57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 474 12 Foundation 0x000000010a4c21be -[NSISEngine withBehaviors:performModifications:] +
155 13 UIKit 0x000000010b6fd6da
__57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452 14 UIKit 0x000000010b6fd4ed -[UIView(AdditionalLayoutSupport)
_switchToLayoutEngine:] + 197 15 UIKit 0x000000010b6fd7d3 __57-[UIView(AdditionalLayoutSupport)
_switchToLayoutEngine:]_block_invoke_2 + 217 16 Foundation 0x000000010a4c21be -[NSISEngine withBehaviors:performModifications:] +
155 17 UIKit 0x000000010b6fd6da
__57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452 18 UIKit 0x000000010b6fd4ed -[UIView(AdditionalLayoutSupport)
_switchToLayoutEngine:] + 197 19 UIKit 0x000000010b10b7c5 -[UIScrollView _switchToLayoutEngine:] + 69 20
UIKit 0x000000010b6fd7d3
__57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 217 21 Foundation 0x000000010a4c21be -[NSISEngine withBehaviors:performModifications:] +
155 22 UIKit 0x000000010b6fd6da
__57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452 23 UIKit 0x000000010b6fd4ed -[UIView(AdditionalLayoutSupport)
_switchToLayoutEngine:] + 197 24 UIKit 0x000000010b6fd7d3 __57-[UIView(AdditionalLayoutSupport)
_switchToLayoutEngine:]_block_invoke_2 + 217 25 Foundation 0x000000010a4c21be -[NSISEngine withBehaviors:performModifications:] +
155 26 UIKit 0x000000010b6fd6da
__57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452 27 UIKit 0x000000010b6fd4ed -[UIView(AdditionalLayoutSupport)
_switchToLayoutEngine:] + 197 28 UIKit 0x000000010b6fd7d3 __57-[UIView(AdditionalLayoutSupport)
_switchToLayoutEngine:]_block_invoke_2 + 217 29 Foundation 0x000000010a4c21be -[NSISEngine withBehaviors:performModifications:] +
155 30 UIKit 0x000000010b6fd6da
__57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452 31 UIKit 0x000000010b6fd4ed -[UIView(AdditionalLayoutSupport)
_switchToLayoutEngine:] + 197 32 UIKit 0x000000010b6fd7d3 __57-[UIView(AdditionalLayoutSupport)
_switchToLayoutEngine:]_block_invoke_2 + 217 33 Foundation 0x000000010a4c21be -[NSISEngine withBehaviors:performModifications:] +
155 34 UIKit 0x000000010b6fd6da
__57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452 35 UIKit 0x000000010b6fd4ed -[UIView(AdditionalLayoutSupport)
_switchToLayoutEngine:] + 197 36 UIKit 0x000000010b0f66d7 __40-[UIView(Hierarchy)
layoutBelowIfNeeded]_block_invoke + 39 37 Foundation
0x000000010a4c21be -[NSISEngine withBehaviors:performModifications:] +
155 38 UIKit 0x000000010b0f6516
-[UIView(Hierarchy) layoutBelowIfNeeded] + 320 39 UIKit 0x000000010b48b234 -[_UIAlertControllerAnimatedTransitioning
animateTransition:] + 470 40 UIKit
0x000000010b190a0e __56-[UIPresentationController
runTransitionForCurrentState]_block_invoke + 1867 41 UIKit
0x000000010b0a65ec _applyBlockToCFArrayCopiedToStack + 314 42 UIKit
0x000000010b0a6466 _afterCACommitHandler + 533 43 CoreFoundation
0x000000010abbcca7
__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 44 CoreFoundation 0x000000010abbcc00
__CFRunLoopDoObservers + 368 45 CoreFoundation 0x000000010abb2a33 __CFRunLoopRun + 1123 46 CoreFoundation
0x000000010abb2366 CFRunLoopRunSpecific + 470 47 GraphicsServices
0x000000010e1afa3e GSEventRunModal + 161 48 UIKit
0x000000010b0828c0 UIApplicationMain + 1282 49 redminete
0x000000010a3dbc33 main + 99 50 libdyld.dylib
0x000000010d22d145 start + 1 ) libc++abi.dylib: terminating with
uncaught exception of type NSException
Below is my sample code
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.receivedData appendData:data];
NSString *htmlSTR = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
#try {
if([htmlSTR isEqualToString:#"Successful update."])
{
NSString * storyboardName=#"Main";
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:storyboardName bundle:nil];
UITabBarController *tabBar =[storyboard instantiateViewControllerWithIdentifier:#"MainTabBar"];
[self presentViewController:tabBar animated:YES completion:Nil];
}
else
{
id jsonObjects = [NSJSONSerialization JSONObjectWithData:
data options:NSJSONReadingMutableContainers error:nil];
NSString *errorMsg = [jsonObjects objectForKey:#"errors"];
[self alertView:#"Error Message" andMessage:errorMsg];
NSLog(#"%#" , errorMsg);
}
}
#catch (NSException *exception) {
NSString *errorMsg = [NSString stringWithFormat:#"%#",exception];
[self alertView:#"Error Message" andMessage:errorMsg];
}
}
-(void) alertView:(NSString *) title andMessage:(NSString *) message
{
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
Most likely this line [jsonObjects objectForKey:#"errors"] is returning an array of errors and you're treating it as NSString.
This is my code for loading data from sqlite file
- (void) loadInitialData{
// Form the query.
NSString *query = #"select * from tasklist";
// Get the results.
if (self.toDoItems != nil) {
self.toDoItems = nil;
}
NSArray *dbResultArray = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
//Logging for debug purpose
NSLog(#"Result set starts");
for(NSArray *subArray in dbResultArray) {
NSLog(#"Data in array: %#",subArray);
}
NSLog(#"Result set ends");
self.toDoItems = [[NSMutableArray alloc]initWithArray:dbResultArray];
// Reload the table view.
//[self.tblPeople reloadData];
[self.tableView reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:#"todotaskdb.sql"];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
}
And I am getting this error using NSLog
2014-11-25 18:24:13.862 ToDoList[3213:60b] Result set ends
2014-11-25 18:24:13.865 ToDoList[3213:60b] -[__NSArrayM itemName]: unrecognized selector sent to instance 0x8c996f0
2014-11-25 18:24:13.900 ToDoList[3213:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM itemName]: unrecognized selector sent to instance 0x8c996f0'
*** First throw call stack:
(
0 CoreFoundation 0x018dc1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0165b8e5 objc_exception_throw + 44
2 CoreFoundation 0x01979243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x018cc50b ___forwarding___ + 1019
4 CoreFoundation 0x018cc0ee _CF_forwarding_prep_0 + 14
5 ToDoList 0x00004882 -[XYZToDoListTableViewController tableView:cellForRowAtIndexPath:] + 306
6 UIKit 0x0041411f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
7 UIKit 0x004141f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
8 UIKit 0x003f5ece -[UITableView _updateVisibleCellsNow:] + 2428
9 UIKit 0x0040a6a5 -[UITableView layoutSubviews] + 213
10 UIKit 0x0038a964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
11 libobjc.A.dylib 0x0166d82b -[NSObject performSelector:withObject:] + 70
12 QuartzCore 0x03d4745a -[CALayer layoutSublayers] + 148
13 QuartzCore 0x03d3b244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
14 QuartzCore 0x03d473a5 -[CALayer layoutIfNeeded] + 160
15 UIKit 0x0044cae3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
16 UIKit 0x00362aa7 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
17 UIKit 0x00361646 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
18 UIKit 0x00361518 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
19 UIKit 0x003615a0 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
20 UIKit 0x0036063a __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
21 UIKit 0x0036059c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
22 UIKit 0x003612f3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
23 UIKit 0x003648e6 -[UIWindow setDelegate:] + 449
24 UIKit 0x0043eb77 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
25 UIKit 0x0035a474 -[UIWindow addRootViewControllerViewIfPossible] + 591
26 UIKit 0x0035a5ef -[UIWindow _setHidden:forced:] + 312
27 UIKit 0x0035a86b -[UIWindow _orderFrontWithoutMakingKey] + 49
28 UIKit 0x003653c8 -[UIWindow makeKeyAndVisible] + 65
29 UIKit 0x00315bc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
30 UIKit 0x0031a667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
31 UIKit 0x0032ef92 -[UIApplication handleEvent:withNewEvent:] + 3517
32 UIKit 0x0032f555 -[UIApplication sendEvent:] + 85
33 UIKit 0x0031c250 _UIApplicationHandleEvent + 683
34 GraphicsServices 0x038d1f02 _PurpleEventCallback + 776
35 GraphicsServices 0x038d1a0d PurpleEventCallback + 46
36 CoreFoundation 0x01857ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
37 CoreFoundation 0x018579db __CFRunLoopDoSource1 + 523
38 CoreFoundation 0x0188268c __CFRunLoopRun + 2156
39 CoreFoundation 0x018819d3 CFRunLoopRunSpecific + 467
40 CoreFoundation 0x018817eb CFRunLoopRunInMode + 123
41 UIKit 0x00319d9c -[UIApplication _run] + 840
42 UIKit 0x0031bf9b UIApplicationMain + 1225
43 ToDoList 0x00004e0d main + 141
44 libdyld.dylib 0x01e25701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Ironically, if put below line at the end of viewDidLoad method, then it works.
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:#"todotaskdb.sql"];
Can anyone please tell me what is it that I am doing wrong?
Your toDoItems contains Arrays which contains your items! You're may be getting arrays of the single rows in your database, so you would have to fill them into single toDoItems objects first.
Maybe you could use a factory-like pattern with a method like popToDoItemFromRow:(NSArray *)row which returns your toDoItem for this row.
Then fill an new array in your for (NSArray *subArray in dbResultArray) for these single rows. This new array should you use in you cellForRow...
may be like..
- (void) loadInitialData{
// Form the query.
NSString *query = #"select * from tasklist";
// Get the results.
if (self.toDoItems != nil) {
self.toDoItems = nil;
}
NSArray *dbResultArray = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
if (self.toDoItems) {
[self.toDoItems removeAllObjects];
}
else {
self.toDoItems = [NSMutableArray new];
}
for(NSArray *subArray in dbResultArray) {
[self.toDoItems addObject:[ToDoItem popToDoItemFromRow:subArray]];
}
// Reload the table view.
//[self.tblPeople reloadData];
[self.tableView reloadData];
}
I need to add an empty object to index 0 and index 1 of an array being populated with data from a third party XML feed.
This is my parseXML method, it works.
-(void) parseXML{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"APIKEYHERECANTSHOW YOU"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *xmlString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(#"The string : %#", xmlString);
NSDictionary *xml = [NSDictionary dictionaryWithXMLString:xmlString];
NSLog(#"The dict:%#", xml);
NSMutableDictionary *PageItem = [xml objectForKey:#"TeamLeagueStanding"];
NSLog(#"PageItem: %#", PageItem);
NSMutableArray *items = [xml objectForKey:#"TeamLeagueStanding"];
NSNull *nullValue = [NSNull null];
[items insertObject:nullValue atIndex:0]; <- THIS MAKES MY APP CRASH
NSLog(#"The array: %#", items);
[self setTableData:items];
}
But when i run this i get a crash with the console output:
2014-02-03 21:24:09.063 Liga Zon Sagres Companion[9645:70b] -[NSNull objectForKey:]: unrecognized selector sent to instance 0x101a85b40
2014-02-03 21:24:09.066 Liga Zon Sagres Companion[9645:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKey:]: unrecognized selector sent to instance 0x101a85b40'
*** First throw call stack:
(
0 CoreFoundation 0x000000010192a795 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010168d991 objc_exception_throw + 43
2 CoreFoundation 0x00000001019bbbad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010191c09d ___forwarding___ + 973
4 CoreFoundation 0x000000010191bc48 _CF_forwarding_prep_0 + 120
5 Liga Zon Sagres Companion 0x000000010000ee20 -[StandingsTableViewController tableView:cellForRowAtIndexPath:] + 256
6 UIKit 0x00000001003bbb8a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348
7 UIKit 0x00000001003a3836 -[UITableView _updateVisibleCellsNow:] + 2297
8 UIKit 0x00000001003b4381 -[UITableView layoutSubviews] + 207
9 UIKit 0x000000010034bb27 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
10 QuartzCore 0x0000000102081a22 -[CALayer layoutSublayers] + 151
11 QuartzCore 0x0000000102076589 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
12 QuartzCore 0x0000000102081956 -[CALayer layoutIfNeeded] + 162
13 UIKit 0x00000001003ebfc2 -[UIViewController window:setupWithInterfaceOrientation:] + 264
14 UIKit 0x000000010032ab4d -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 4360
15 UIKit 0x0000000100329a3f -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 36
16 UIKit 0x000000010032998f -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 101
17 UIKit 0x0000000100328c9e -[UIWindow _updateToInterfaceOrientation:duration:force:] + 377
18 UIKit 0x00000001003dfd4a -[UIViewController _tryBecomeRootViewControllerInWindow:] + 147
19 UIKit 0x0000000100323a87 -[UIWindow addRootViewControllerViewIfPossible] + 506
20 UIKit 0x0000000100323bd5 -[UIWindow _setHidden:forced:] + 275
21 UIKit 0x000000010032cca2 -[UIWindow makeKeyAndVisible] + 51
22 UIKit 0x00000001002eb0c8 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1449
23 UIKit 0x00000001002eebe8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
24 UIKit 0x00000001002ffaab -[UIApplication handleEvent:withNewEvent:] + 3092
25 UIKit 0x00000001002fff1e -[UIApplication sendEvent:] + 79
26 UIKit 0x00000001002f02be _UIApplicationHandleEvent + 618
27 GraphicsServices 0x0000000102578bb6 _PurpleEventCallback + 762
28 GraphicsServices 0x000000010257867d PurpleEventCallback + 35
29 CoreFoundation 0x00000001018ac819 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
30 CoreFoundation 0x00000001018ac5ee __CFRunLoopDoSource1 + 478
31 CoreFoundation 0x00000001018d5ab3 __CFRunLoopRun + 1939
32 CoreFoundation 0x00000001018d4f33 CFRunLoopRunSpecific + 467
33 UIKit 0x00000001002ee4bd -[UIApplication _run] + 609
34 UIKit 0x00000001002f0043 UIApplicationMain + 1010
35 Liga Zon Sagres Companion 0x0000000100011f93 main + 115
36 libdyld.dylib 0x0000000102f815fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Any ideas how to solve this? Thank you.
The error is coming from your StandingsTableViewController tableView:cellForRowAtIndexPath: method. Your data is giving you an NSNull instance where you expect an NSDictionary.
Since you explicitly add the NSNull object you need to update your cellForRow... method to check to see if the object is an NSNull instance before assuming it is an NSDictionary.
Something like this:
NSDictionary *data = self.tableData[someIndex];
if ([data isKindOfClass:[NSDictionary class]]) {
// process the data as usual
} else {
// This is probably the NSNull object, ignore it or handle appropriately
}
Am getting a SIGABRT error in main.m:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); // SIGABRT error
}
}
Am getting this in the console, but can't understand it:
2013-10-24 15:21:30.278 Blog_Reader[45888:a0b] -[__NSDictionaryI length]: unrecognized selector sent to instance 0xb779360
2013-10-24 15:21:30.293 Blog_Reader[45888:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI length]: unrecognized selector sent to instance 0xb779360'
*** First throw call stack:
(
0 CoreFoundation 0x017345e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014b78b6 objc_exception_throw + 44
2 CoreFoundation 0x017d1903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0172490b ___forwarding___ + 1019
4 CoreFoundation 0x017244ee _CF_forwarding_prep_0 + 14
5 Foundation 0x010fcb2d -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 39
6 Foundation 0x010fd79a -[NSConcreteMutableAttributedString initWithString:attributes:] + 293
7 UIKit 0x003d7116 -[UILabel _setText:] + 97
8 UIKit 0x003d72d4 -[UILabel setText:] + 40
9 Blog_Reader 0x00002c3d -[TableViewController tableView:cellForRowAtIndexPath:] + 301
10 UIKit 0x00318d2f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
11 UIKit 0x00318e03 -[UITableView _createPreparedCellForGlobalRow:] + 69
12 UIKit 0x002fd124 -[UITableView _updateVisibleCellsNow:] + 2378
13 UIKit 0x003105a5 -[UITableView layoutSubviews] + 213
14 UIKit 0x00294dd7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
15 libobjc.A.dylib 0x014c981f -[NSObject performSelector:withObject:] + 70
16 QuartzCore 0x03aee72a -[CALayer layoutSublayers] + 148
17 QuartzCore 0x03ae2514 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
18 QuartzCore 0x03aee675 -[CALayer layoutIfNeeded] + 160
19 UIKit 0x0034fca3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
20 UIKit 0x0026ed27 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
21 UIKit 0x0026d8c6 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
22 UIKit 0x0026d798 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
23 UIKit 0x0026d820 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
24 UIKit 0x0026c8ba __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
25 UIKit 0x0026c81c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
26 UIKit 0x0026d573 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
27 UIKit 0x00270b66 -[UIWindow setDelegate:] + 449
28 UIKit 0x00341dc7 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
29 UIKit 0x002667cc -[UIWindow addRootViewControllerViewIfPossible] + 609
30 UIKit 0x00266947 -[UIWindow _setHidden:forced:] + 312
31 UIKit 0x00266bdd -[UIWindow _orderFrontWithoutMakingKey] + 49
32 UIKit 0x0027144a -[UIWindow makeKeyAndVisible] + 65
33 Blog_Reader 0x000023bb -[AppDelegate application:didFinishLaunchingWithOptions:] + 267
34 UIKit 0x00223f65 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 309
35 UIKit 0x002247a5 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1536
36 UIKit 0x00228fb8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
37 UIKit 0x0023d42c -[UIApplication handleEvent:withNewEvent:] + 3447
38 UIKit 0x0023d999 -[UIApplication sendEvent:] + 85
39 UIKit 0x0022ac35 _UIApplicationHandleEvent + 736
40 GraphicsServices 0x036872eb _PurpleEventCallback + 776
41 GraphicsServices 0x03686df6 PurpleEventCallback + 46
42 CoreFoundation 0x016afdd5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
43 CoreFoundation 0x016afb0b __CFRunLoopDoSource1 + 523
44 CoreFoundation 0x016da7ec __CFRunLoopRun + 2156
45 CoreFoundation 0x016d9b33 CFRunLoopRunSpecific + 467
46 CoreFoundation 0x016d994b CFRunLoopRunInMode + 123
47 UIKit 0x002286ed -[UIApplication _run] + 840
48 UIKit 0x0022a94b UIApplicationMain + 1225
49 Blog_Reader 0x00002e2d main + 141
50 libdyld.dylib 0x01d70725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
This is the TableViewController.m file:
#implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSDictionary *blogPost1 = [NSDictionary dictionaryWithObjectsAndKeys:#"The Missing Widget in Android", #"title", #"Ben Jakuben", #"author", nil];
NSDictionary *blogPost2 = [NSDictionary dictionaryWithObjectsAndKeys:#"Getting Started with iOS Development", #"title", #"Amit Bijlani", #"author", nil];
NSDictionary *blogPost3 = [NSDictionary dictionaryWithObjectsAndKeys:#"An Interview with Shay Howe", #"title", #"Joe Villanueva", #"author", nil];
self.blogPosts = [NSArray arrayWithObjects:blogPost1, blogPost2, blogPost3, nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [self.blogPosts objectAtIndex:indexPath.row];
return cell;
}
#end
You are storing NSDictionary instances in self.blogPosts, but then in
cell.textLabel.text = [self.blogPosts objectAtIndex:indexPath.row];
you are assigning one of them to text, which expects a NSString. The compiler is not catching it since objectAtIndex: returns an object of type id, which can potentially be anything.
A possible fix (but it really depends on what you need) could be
cell.textLabel.text = self.blogPosts[indexPath.row][#"title"];