-1

I get the "thread 1 signal sigabrt" error for the line (return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));) in the main.m with following in the log:

2014-12-31 22:17:16.295 RowCounter[2994:110721] -[__NSCFString objectForKey:]: unrecognized   
selector sent to instance 0x7fa0ea504300
2014-12-31 22:17:16.298 RowCounter[2994:110721] *** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector   
sent to instance 0x7fa0ea504300'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000102e88f35 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x0000000102b21bb7 objc_exception_throw + 45
2   CoreFoundation                      0x0000000102e9004d -[NSObject(NSObject)   
doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x0000000102de827c ___forwarding___ + 988
4   CoreFoundation                      0x0000000102de7e18 _CF_forwarding_prep_0 + 120
5   RowCounter                          0x00000001025f1ebc -[RowCounterViewController 
viewDidLoad] + 444
6   UIKit                               0x000000010339aa90 -[UIViewController 
loadViewIfRequired] + 738
7   UIKit                               0x000000010339ac8e -[UIViewController view] + 27
8   UIKit                               0x00000001033be507 -[UINavigationController 
_startCustomTransition:] + 633
9   UIKit                               0x00000001033ca3fe -[UINavigationController 
_startDeferredTransitionIfNeeded:] + 386
10  UIKit                               0x00000001033caf47 -[UINavigationController 
__viewWillLayoutSubviews] + 43
11  UIKit                               0x0000000103510509 -[UILayoutContainerView 
layoutSubviews] + 202
12  UIKit                               0x00000001032ee973 -[UIView(CALayerDelegate) 
layoutSublayersOfLayer:] + 521
13  QuartzCore                          0x0000000106b76de8 -[CALayer layoutSublayers] + 150
14  QuartzCore                          0x0000000106b6ba0e 
_ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
15  QuartzCore                          0x0000000106b6b87e 
_ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
16  QuartzCore                          0x0000000106ad963e 
_ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
17  QuartzCore                          0x0000000106ada74a _ZN2CA11Transaction6commitEv + 390
18  UIKit                               0x000000010327214d _UIApplicationHandleEventQueue + 
2035
19  CoreFoundation                      0x0000000102dbe551 
__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
20  CoreFoundation                      0x0000000102db441d __CFRunLoopDoSources0 + 269
21  CoreFoundation                      0x0000000102db3a54 __CFRunLoopRun + 868
22  CoreFoundation                      0x0000000102db3486 CFRunLoopRunSpecific + 470
23  GraphicsServices                    0x000000010646a9f0 GSEventRunModal + 161
24  UIKit                               0x0000000103275420 UIApplicationMain + 1282
25  RowCounter                          0x00000001025f23c3 main + 115
26  libdyld.dylib                       0x0000000105418145 start + 1
27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

As suggested by Sebastian Keller - I am calling [object objectForKey:] on a string object in `[RowCounterViewController viewDidLoad].

Further suggested that NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"]; => is not really an NSDictionary.

For debugging purpose tried: NSDictionary *dict = @{}; and it did run.

This is code from that section, can you please point out what I am doing wrong?

- (void)viewDidLoad {
    [super viewDidLoad];

    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"] ;
    NSString *key = [NSString stringWithFormat:@"%ld", (unsigned long)_itemIndex] ;

    NSNumber *numCounter = [dict objectForKey:key] ;
    if (numCounter) {
        counter = [numCounter intValue] ;
        count.text = [NSString stringWithFormat:@"%d", counter] ;
    } else {
        counter = 0 ;
        count.text = @"0" ;
    }
}

another bit of code with NSUserDefaults:

- (void)saveCount {
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"] ;
    NSMutableDictionary *mDict = nil ;
    if (dict == nil) 
        mDict = [NSMutableDictionary dictionary] ;
    else 
        mDict = [dict mutableCopy] ;

    NSString *key = [NSString stringWithFormat:@"%ld", (unsigned long)_itemIndex] ;
    [mDict setObject:@(counter) forKey:key] ;

    [[NSUserDefaults standardUserDefaults] setObject:mDict forKey:@"saveCount"] ;
    [[NSUserDefaults standardUserDefaults] synchronize] ;
}

Reading for NSLog(@"%@", dict); in console 10

Thank you

dicobraz
  • 15
  • 8

2 Answers2

0

EDIT: Reseting iPhone Simulator content and settings will help.

This is one important line:

2014-12-31 22:17:16.295 RowCounter[2994:110721] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fa0ea504300

It means that your are calling [object objectForKey:] on a string object (which does not implement objectForKey:)

This is happening in [RowCounterViewController viewDidLoad]

5   RowCounter                          0x00000001025f1ebc -[RowCounterViewController viewDidLoad] + 444
dicobraz
  • 15
  • 8
  • Okay, i don't think your dict is an NSDictionary. –  Dec 31 '14 at 14:53
  • NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"]; => is this really an NSDictionary? –  Dec 31 '14 at 14:54
  • @dicobraz: Your dict is actually no NSDictionary. For debugging purpose you can try: NSDictionary *dict = @{};. (This should run) –  Dec 31 '14 at 15:10
  • @dicobraz: Somewhere in your code your are passing a string to [[NSUserDefaults standardUserDefaults] setObject:object forKey:@"saveCount"]; –  Dec 31 '14 at 15:11
  • hmm... see another edit, the bit where I save the count – dicobraz Dec 31 '14 at 15:20
  • Looks okay to me. Can you please add an "NSLog(@"%@", dict);" to your -viewDidLoad method and tell us the output? –  Dec 31 '14 at 15:25
  • I'm sorry, but this just don't work out. Do save 'saveCount' anywhere else? The code you have provided should work just fine. What about deinstalling and reinstalling the app? Maybe it's an old value in 'saveCount'. –  Jan 01 '15 at 11:25
  • I am running the app from xcode in Iphone6 simulator, I did not know that I can deinstall and reinstall an app, please explain how to do it? – dicobraz Jan 01 '15 at 14:33
  • To reset the simulator, see http://stackoverflow.com/questions/692064/cleaning-up-the-iphone-simulator –  Jan 02 '15 at 14:01
  • 1
    Cant up vote, don't have 15 reputation yet, but I checked and edited your answer as correct – dicobraz Jan 02 '15 at 14:20
0

In the last lines, use

NSInteger numCounter = [[dict objectForKey:key] intValue] ;
if (numCounter > 0) {
    count.text = [NSString stringWithFormat:@"%d", numCounter] ;
} else {
    numCounter = 0 ;
    count.text = @"0" ;
}
Fawad Masud
  • 12,219
  • 3
  • 25
  • 34
  • NSInteger is a primitive type. No need for pointers here. If you remove the unnecessary pointer, the if clause will check if numCounter is equal to one. –  Dec 31 '14 at 15:05
  • Still the same, after I implied your suggested code nothing changed – dicobraz Jan 02 '15 at 12:56