gpt4 book ai didi

ios - 从 CoreData 实体中的 NSNumber 获取 intValue 时,无法识别的选择器崩溃

转载 作者:行者123 更新时间:2023-12-02 03:07:20 26 4
gpt4 key购买 nike

我正在从核心数据存储中检索一个实体并将其放入名为 fetchedObjects 的数组中。我正在尝试从 NSNumber 获取整数值,但我的应用程序崩溃了。

代码如下:

 NSNumber *idioma = [fetchedObjects valueForKey:@"idioma"];

如果像这样执行NSLog():

  NSLog(@"idioma = %@", idioma);

我得到类似的东西 idioma = ( 0 )

但是,如果我尝试在 NSNumber 上调用 intValue,应用程序就会崩溃。这是一个例子:

if ([idioma intValue] > 0 ) {
//do something
} else {
//else do something else
}

错误消息和堆栈跟踪顶部:

2014-04-30 21:50:15.100 Mensagens 2[18797:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI intValue]: unrecognized selector sent to instance 0x8f31080'*** First throw call stack: (        0   CoreFoundation                      0x01b5d1e4 __exceptionPreprocess + 180        1   libobjc.A.dylib                     0x018dc8e5 objc_exception_throw + 44        2   CoreFoundation                      0x01bfa243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275        3   CoreFoundation                      0x01b4d50b ___forwarding___ + 1019        4   CoreFoundation                      0x01b4d0ee _CF_forwarding_prep_0 + 14        5   Mensagens 2                         0x000034fd -[ViewController logMensagem] + 1645        6   Mensagens 2                         0x00002da0 -[ViewController viewDidLoad] + 112        7   UIKit                               0x006bb33d -[UIViewController loadViewIfRequired] + 696        8   UIKit                               0x006bb5d9 -[UIViewController view] + 35

This is how I'm setting the "idioma"

NSNumber *number = [NSNumber numberWithInteger:lingua]; // lingua is a NSInteger
[entitie setIdioma:number];

The Entitie entitie.h

最佳答案

看起来你有一个带有 NSNumber 的 NSArray,但你试图像访问 NSNumber 一样访问它。这一行:

NSNumber *idioma = [fetchedObjects valueForKey:@"idioma"];

应该是:

 NSNumber *idioma = [[fetchedObjects valueForKey:@"idioma"]objectAtIndex:0];

关于ios - 从 CoreData 实体中的 NSNumber 获取 intValue 时,无法识别的选择器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23400397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com