iOS点击计数器应用程序 - 线程1信号sigabrt错误

时间:2014-12-31 14:27:46

标签: ios xcode sigabrt

我得到"线程1信号sigabrt"该行的错误(返回UIApplicationMain(argc,argv,nil,NSStringFromClass([AppDelegate class]));)在main.m中,日志中包含以下内容:

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)

正如Sebastian Keller所建议的那样 - 我在`[RowCounterViewController viewDidLoad]中的字符串对象上调用[object objectForKey:]

进一步建议NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"]; =>并不是真正的NSDictionary。

为了调试目的,尝试了NSDictionary *dict = @{};并确实运行了。

这是该部分的代码,请您指出我做错了什么?

- (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" ;
    }
}

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] ;
}

在控制台NSLog(@"%@", dict);

中阅读10

谢谢

2 个答案:

答案 0 :(得分:0)

编辑:重置iPhone模拟器的内容和设置会有所帮助。

这是一条重要的路线:

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

这意味着你在字符串对象上调用[object objectForKey:](它没有实现objectForKey:)

这发生在[RowCounterViewController viewDidLoad]

5   RowCounter                          0x00000001025f1ebc -[RowCounterViewController viewDidLoad] + 444

答案 1 :(得分:0)

在最后一行中,使用

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