NSNumber 未正确转换并被解释为 NSCFString?

发布于 2025-01-01 08:05:37 字数 881 浏览 0 评论 0原文

我正在迭代通过 API 调用而来的数字数组:

例如 [6000, 6001, 2000]

这是我的代码:

    for(NSNumber* arrayID in array){                               
        NSManagedObject *ent = [NSEntityDescription insertNewObjectForEntityForName:@"Genre" inManagedObjectContext:self.managedObjectContext];
        [ent setValue:arrayID forKey:@"genreID"]; 
        [ent setValue:name forKey:@"genreName"];
        [mySet addObject:ent];
    }

当设置 arrayID (这是我的核心数据中的 NSNumber)时,我遇到了崩溃

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "genreID"; desired type = NSNumber; given type = __NSCFString; value = 6002.'

:如果 arrayID 尚未转换为 NSNumber?当我输入时,Xcode 说它是一个 NSNumber,但如果我输入 [arrayID doubleValue],Xcode 会告诉我“无法为类型‘id’发送 doubleValue”

有什么想法吗?

I'm iterating through an array of numbers that come through an API call:

[6000, 6001, 2000] for example

Here's my code:

    for(NSNumber* arrayID in array){                               
        NSManagedObject *ent = [NSEntityDescription insertNewObjectForEntityForName:@"Genre" inManagedObjectContext:self.managedObjectContext];
        [ent setValue:arrayID forKey:@"genreID"]; 
        [ent setValue:name forKey:@"genreName"];
        [mySet addObject:ent];
    }

When setting the arrayID (which is NSNumber in my core data), I get a crash:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "genreID"; desired type = NSNumber; given type = __NSCFString; value = 6002.'

It's as if arrayID has not been cast as an NSNumber? Xcode says its an NSNumber when I type it in, but if I put [arrayID doubleValue] for example, Xcode tells me that 'doubleValue cannot be sent for type 'id'

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

是伱的 2025-01-08 08:05:37

仅仅因为您将 arrayID 声明为 NSNumber 指针,并不能保证 arrayID 对象实际上是 NSNumber >。在这种情况下,数组似乎由 NSString 组成。

在 Objective-C 中,对象类型只是对编译器的真正提示,您可以将任何内容强制转换为任何内容,并调用对象上的任何方法,这些事情实际上只会在运行时失败。您必须查看 array 的创建位置才能了解实际类型。

在这种情况下,如果它们实际上都是字符串,你可以这样做

for(NSString* arrayID in array){                               
    NSManagedObject *ent = [NSEntityDescription insertNewObjectForEntityForName:@"Genre" inManagedObjectContext:self.managedObjectContext];
    [ent setValue:[NSNumber numberWithInt:[arrayID intValue]] forKey:@"genreID"]; 
    [ent setValue:name forKey:@"genreName"];
    [mySet addObject:ent];
}

Just because you declared arrayID as an NSNumber pointer, it doesn't guarantee that the arrayID object is actually an NSNumber. In this case, the array appears to consist of NSStrings.

In Objective-C, the object types are only really hints to the compiler, you can cast anything to anything, and call any method on an object, these things only actually fail at runtime. You'll have to look at where array was created to see what the type actually is.

In this case, if they are all in fact strings, you can do

for(NSString* arrayID in array){                               
    NSManagedObject *ent = [NSEntityDescription insertNewObjectForEntityForName:@"Genre" inManagedObjectContext:self.managedObjectContext];
    [ent setValue:[NSNumber numberWithInt:[arrayID intValue]] forKey:@"genreID"]; 
    [ent setValue:name forKey:@"genreName"];
    [mySet addObject:ent];
}
飘逸的'云 2025-01-08 08:05:37

你是如何创建你的数组的?听起来它确实包含 NSString,而不是 NSNumber。

How are you creating your array? It sounds like it really contains NSStrings, not NSNumbers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文