【问题标题】:iOS tap counter app - thread 1 signal sigabrt erroriOS点击计数器应用程序-线程1信号sigabrt错误
【发布时间】:2015-02-27 14:54:02
【问题描述】:

我在 main.m 中收到“线程 1 信号 sigabrt”错误 (return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));),日志中有以下内容:

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

在控制台10 中读取NSLog(@"%@", dict);

谢谢

【问题讨论】:

    标签: ios xcode sigabrt


    【解决方案1】:

    编辑:重置 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
    

    【讨论】:

    • 好吧,我不认为你的字典是 NSDictionary。
    • NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"]; => 这真的是 NSDictionary 吗?
    • @dicobraz:您的 dict 实际上不是 NSDictionary。出于调试目的,您可以尝试:NSDictionary *dict = @{};。 (这应该运行)
    • @dicobraz:在您的代码中某处,您将字符串传递给 [[NSUserDefaults standardUserDefaults] setObject:object forKey:@"saveCount"];
    • 不能投票,还没有 15 名声望,但我检查并编辑您的答案是正确的
    【解决方案2】:

    在最后几行,使用

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

    【讨论】:

    • NSInteger 是一个原始类型。这里不需要指针。如果删除不必要的指针,if 子句将检查 numCounter 是否等于 1。
    • 还是一样,在我暗示你建议的代码之后没有任何改变
    猜你喜欢
    • 2014-11-08
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多