NSArray valueForKey返回NSNull?(NSArray valueForKey returning NSNull?)

编程入门 行业动态 更新时间:2024-10-28 13:17:34
NSArray valueForKey返回NSNull?(NSArray valueForKey returning NSNull?)

给出以下代码(使用magicrecord进行coredata获取):

// Get all of our apps. NSArray * applications = [Application MR_findAllInContext: [NSManagedObjectContext MR_context]]; Application * application = [applications firstObject]; NSLog(@"Test: %@", application.applicationId); NSArray * applicationIds = [applications valueForKey: @"applicationId"]; NSLog(@"Application ids: %@", applicationIds);

我得到以下日志:

Test: 586001240 Application ids: ( "<null>", "<null>" )

我知道applications数组包含两个条目。 每个都有一个唯一的applicationId属性。 我不明白为什么对valueForKey的调用会返回两个空值? 相反,我应该得到两个NSNumber对象。

我最近发现了关于valueForKey(以前获取唯一条目我会使用枚举并将条目添加到数组中)。 我正在尝试使用valueForKey来提高可读性。 显然我不是正确理解的东西。 关于我哪里出错的提示?

编辑 - 更多详情:

Application类是通过核心数据模型生成的。 界面定义为:

@interface Application : NSManagedObject @property (nonatomic, retain) NSNumber * applicationId; @property (nonatomic, retain) NSString * name; @end

Given the following code (uses magicrecord for the coredata fetching):

// Get all of our apps. NSArray * applications = [Application MR_findAllInContext: [NSManagedObjectContext MR_context]]; Application * application = [applications firstObject]; NSLog(@"Test: %@", application.applicationId); NSArray * applicationIds = [applications valueForKey: @"applicationId"]; NSLog(@"Application ids: %@", applicationIds);

I get the following logs:

Test: 586001240 Application ids: ( "<null>", "<null>" )

I know that the applications array contains two entries. Each has a unique applicationId property. I don't understand why the call to valueForKey is returning me two nulls? Instead I should be getting two NSNumber objects.

I recently found out about valueForKey (previously to get unique entries I would use an enumeration and add the entries to an array). I'm trying to use valueForKey to improve the readability. Obviously I am not understanding something correctly. Any tips on where I am going wrong?

Edit - More details:

The Application class is generated via a core data model. The interface is defined as:

@interface Application : NSManagedObject @property (nonatomic, retain) NSNumber * applicationId; @property (nonatomic, retain) NSString * name; @end

最满意答案

想出来 - 我的问题非常愚蠢。

我不得不改变:

[Application MR_findAllInContext: [NSManagedObjectContext MR_context]];

成为:

[Application MR_findAllInContext: [NSManagedObjectContext MR_defaultContext]];

这解决了这个问题。

Figured it out - Very dumb problem on my end.

I had to change:

[Application MR_findAllInContext: [NSManagedObjectContext MR_context]];

To be:

[Application MR_findAllInContext: [NSManagedObjectContext MR_defaultContext]];

That solved the issue.

更多推荐

本文发布于:2023-08-02 15:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1378286.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:valueForKey   NSArray   returning   NSNull

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!