-2

I have an instance variable _sessions of type NSArray. Throughout the TableViewController methods, I access it and it is fine. But when in prepareForSegue, I try to access it or its values, it throws this error..

Relavent code:

@interface SessionsTVC()
{

    NSArray *_sessions;
    JSONDataRetriever *jsonRetriever;
    NSString *requestURL;
}
@end

in @implementation:

_sessions = [[NSArray alloc]initWithArray:_tempArray];

Lines giving me trouble:

NSLog([_sessions objectAtIndex:1]);

or

NSLog(_sessions);

What is even weirder is that this works:

NSLog([@(_sessions.count) stringValue]);

EDIT:

NSLog([@(_sessions.count) stringValue]); gives me back 2 (which is exactly right)..

FULL ERROR MESSAGE

2014-08-13 23:14:46.548 ReadDatabase[15142:650918] -[Session length]: unrecognized selector sent to instance 0x7fb5fbf55090 2014-08-13 23:14:46.644 ReadDatabase[15142:650918] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Session length]: unrecognized selector sent to instance 0x7fb5fbf55090' * First throw call stack: ( 0 CoreFoundation 0x00000001074313e5 exceptionPreprocess + 165 1 libobjc.A.dylib 0x00000001070e1967 objc_exception_throw + 45 2 CoreFoundation 0x00000001074384fd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x00000001073907ec ___forwarding_ + 988 4 CoreFoundation 0x0000000107390388 _CF_forwarding_prep_0 + 120 5 CoreFoundation 0x000000010732a39b CFStringAppendFormatCore + 235 6 CoreFoundation 0x0000000107411c00 _CFStringCreateWithFormatAndArgumentsAux2 + 256 7 CoreFoundation 0x0000000107420a4f _CFLogvEx2 + 127 8 Foundation 0x0000000106c7da22 NSLogv + 99 9 Foundation 0x0000000106c7d9a7 NSLog + 148 10 ReadDatabase 0x00000001054a36f6 -[SessionsTVC prepareForSegue:sender:] + 518 11 UIKit 0x0000000105f04716 -[UIStoryboardSegueTemplate _perform:] + 151 12 UIKit 0x0000000105aa0d40 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1242 13 UIKit 0x0000000105aa0eb4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219 14 UIKit 0x00000001059dc97e _applyBlockToCFArrayCopiedToStack + 314 15 UIKit 0x00000001059dc7f8 _afterCACommitHandler + 516 16 CoreFoundation 0x0000000107366337 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 17 CoreFoundation 0x0000000107366290 __CFRunLoopDoObservers + 368 18 CoreFoundation 0x000000010735c0c3 __CFRunLoopRun + 1123 19 CoreFoundation 0x000000010735b9f6 CFRunLoopRunSpecific + 470 20 GraphicsServices 0x000000010960c9f0 GSEventRunModal + 161 21 UIKit 0x00000001059b9990 UIApplicationMain + 1282 22 ReadDatabase 0x00000001054a2bf3 main + 115 23 libdyld.dylib 0x0000000109def145 start + 1 24 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

peddy
  • 31
  • 4
  • 1
    Please include error in question (not only title). – Halvor Holsten Strand Aug 13 '14 at 02:41
  • 1
    Please copy/paste the *full error*, including the jargon after that text. – Undo Aug 13 '14 at 02:46
  • 1
    yes please add your error description as well – thndrkiss Aug 13 '14 at 02:48
  • The error message tells you precisely what's wrong, if you bother to read it. Or if you post it here then we can read it and tell you what's wrong. Lacking that, though, there's no point in even asking the question. – Hot Licks Aug 13 '14 at 03:08
  • I assure you I played around with the error for hours without any luck. I cannot possibly understand how the [_sessions count] message could work fine but anytime I try to access items stored in _sessions, I get the error. THE FULL ERROR has been added to the question. Thank you folks. – peddy Aug 14 '14 at 03:17

2 Answers2

1

NSLog requires a string as the first argument.

Try this:

NSLog(@"%@", _sessions);

NSLog(@"%@", [_sessions objectAtIndex:1]);

make sure that _session.count is grater than one in the second case.

SauloT
  • 153
  • 7
  • NSLog can also take any nsobject and print its +(nsstring*)description. since all objects are descendants of NSObject that shouldn't be the reason for crash. – thndrkiss Aug 13 '14 at 02:58
  • 1
    Where did you read this? The function signature is "void NSLog(NSString *format, ...)". – SauloT Aug 13 '14 at 03:07
  • yeah my bad, it should have a format specifier, i just did a quick check. – thndrkiss Aug 13 '14 at 03:13
  • https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html search for printf which talks about this, it do needs format specifier – thndrkiss Aug 13 '14 at 03:15
  • I'm afraid that is not the problem since before the log message, I was actually doing stuff with the object returned by the message and it still resulted in the error. – peddy Aug 14 '14 at 03:21
  • As I told, the ONLY problem is that you are passing a non string variable in the first argument. The error log clarifies that... It is trying to access the method length (from NSString) of your _session variable. Trust me... Use the form **NSLog(@"%@", object)**. To log the count property (which is an int), pass @"%d" as the format instead of @"%@". [basic explanation](http://stackoverflow.com/questions/5486270/xcode-using-nslog-for-debugging) – SauloT Aug 14 '14 at 20:30
0

What result do you get with this line:

NSLog([@(_sessions.count) stringValue]);

?

Because I think it is equivalent to:

NSLog([[NSNumber numberWithInt:_sessions.count] stringValue]);

Probably the issue is _sessions is nil, therefore _sessions.count will return 0, and you will see 0 as a string in the Console.

BTW, this is the correct way to print objects with NSLog():

NSLog(@"%@", [_sessions objectAtIndex:1]);
ppalancica
  • 4,236
  • 4
  • 27
  • 42