【问题标题】:NSArrayController addObject Returns nullNSArrayController addObject 返回 null
【发布时间】:2010-01-06 03:36:12
【问题描述】:

我有一个带有两个 NIB 的核心数据应用程序,一个带有一对 NSTableView 的主窗口和一个用于添加内容的表单。该表单有一个 NSTextView 和一个 NSTextField。我在核心数据中有两个实体,并使用两个 NSArrayControllers 管理内容。使用一个 NSArrayController,我的 AppDelegate 中的以下代码可以很好地添加内容:

    id o = [bookController newObject];
    [o setValue:@"Drafts" forKey:@"bookName"];
    [o setValue:0 forKey:@"sortOrder"];
    [bookController addObject:o];

但是,我的 AppController 类中的这段代码总是返回 null:

NSObject *o = [chapterArrayController newObject];

[o setValue:contentOfchapter forKey:@"chapterText"];
[o setValue:chapterTitleString forKey:@"chapterTitle"];
[o setValue:@"Drafts" forKey:@"bookChapter"];
NSLog(@"Where is the object?: %@", o);
[chapterArrayController addObject:o];

好像 chapterArrayController 没有连接到 core data 中的 Chapter 实体,但是 IB 中的绑定是正确的。我认为这与多个笔尖有关,但我在这里有点不知所措。

赞赏任何正确方向的指针。

谢谢。

更新 2:我创建了一个名为 JBAddChapter 的类,如下所示:

JBAddChapter.h

#import <Cocoa/Cocoa.h>


@interface JBAddChapter : NSObject {

IBOutlet NSArrayController *bookController;
IBOutlet NSArrayController *chapterArrayController;

}
- (IBAction)testArrayControllers:(id)sender;
+ (void)getChapterData:(NSString *)passedChapterTitle withChapterText:(NSString *)passedChapterText;
- (void)standAloneTestArrayControllers;

@end

JBAddChapter.m

#import "JBAddChapter.h"


@implementation JBAddChapter


+ (void)getChapterData:(NSString *)passedChapterTitle withChapterText:(NSString *)passedChapterText;
{
    [[self alloc] standAloneTestArrayControllers];
}


- (IBAction)testArrayControllers:(id)sender
{
    [self standAloneTestArrayControllers];
}

- (void)standAloneTestArrayControllers
{
    [chapterArrayController fetchWithRequest:nil merge:NO error:nil];
    NSLog(@"1. chapterArrayController %@", chapterArrayController);

    NSArray *a = [chapterArrayController arrangedObjects];
    NSLog(@"2. NSArray a = %@", a);

    NSUInteger numberOfMenuItems = [a count];
    NSLog(@"3. Count of items in array: %d", numberOfMenuItems);

    [bookController fetchWithRequest:nil merge:NO error:nil];
    NSLog(@"1. bookController %@", chapterArrayController);

    NSArray *b = [chapterArrayController arrangedObjects];
    NSLog(@"2. NSArray b = %@", b);

    NSUInteger newNumberOfMenuItems = [a count];
    NSLog(@"3. Count of items in array: %d", newNumberOfMenuItems);
}
@end

我在主窗口的 IB 中创建了两个按钮,并将一个连接到上面的 testArrayControllers IBAction。我连接到 AppController 和这个 IBAction 的另一个按钮:

- (IBAction)testArrayControllers:(id)sender
{
    [JBAddChapter getChapterData:nil withChapterText:nil];
}

如果我从 JBAddChapter 的 IBAction 中调用 standAloneTestArrayControllers,一切正常。如果我使用 JBAddChapter 中的工厂方法从 AppController 调用相同的方法,则数组控制器为零。

2010-01-07 06:15:36.971 Scout[3881:a0f] 1. chapterArrayController <NSArrayController: 0x200060b40>[entity: Chapter, number of selected objects: 1]
2010-01-07 06:15:36.972 Scout[3881:a0f] 2. NSArray a = (
loads of stuff
)
2010-01-07 06:15:36.973 Scout[3881:a0f] 3. Count of items in array: 9
2010-01-07 06:15:36.974 Scout[3881:a0f] 1. bookController <NSArrayController: 0x200060b40>[entity: Chapter, number of selected objects: 1]
2010-01-07 06:15:36.978 Scout[3881:a0f] 2. NSArray b = (
loads of stuff
)
2010-01-07 06:15:36.979 Scout[3881:a0f] 3. Count of items in array: 8
2010-01-07 06:15:38.402 Scout[3881:a0f] 1. chapterArrayController (null)
2010-01-07 06:15:38.402 Scout[3881:a0f] 2. NSArray a = (null)
2010-01-07 06:15:38.402 Scout[3881:a0f] 3. Count of items in array: 0
2010-01-07 06:15:38.403 Scout[3881:a0f] 1. bookController (null)
2010-01-07 06:15:38.403 Scout[3881:a0f] 2. NSArray b = (null)
2010-01-07 06:15:38.403 Scout[3881:a0f] 3. Count of items in array: 0

那么,为什么数组控制器会从一种方法返回 nil,而不是另一种?据我所知,IB 中的所有绑定都是正确的。

【问题讨论】:

  • 您应该使用NSUInteger 作为count 的返回值。 int 在 64 位机器上太短了,并且符号错误(值可以是负数,这对于事物的计数没有意义)。
  • 谢谢彼得,我已经按照您的建议进行了调整。

标签: cocoa core-data nsarraycontroller


【解决方案1】:

当你向它请求一个对象时,它是 nil 吗?

你说你有两个笔尖……是在第二个笔尖中实例化了 chapterArrayController 吗?如果是,在调用 newObject 时是否已加载 nib?

【讨论】:

  • 好吧,我正在强制 chapterArrayController 加载:[chapterArrayController fetchWithRequest:nil merge:NO er​​ror:nil];我将 chapterArrayController 绑定到一个 NSArrayController 对象,并且在两个 nib 中都有绑定。 NSArray a = [chapterArrayController 排列对象] int b = [a count]; NSLog(@"数组中的项目:%d", b);上面的 int b 每次都是 0,如果我尝试这样做: NSObject *o = [chapterArrayController newObject]; NSLog(@"对象在哪里:%@", o); NSLog 将记录“null”。
  • 如果在调用 fetchWithRequest:merge:error: 时 chapterArrayController 为 nil,则不会发生任何事情。我假设 chapterArrayController 是一个 IBOutlet ivar,是吗?尝试输入: NSLog(@"controller: %@", chapterArrayController);在您的提取请求之后。我想它会返回零。你能显示加载第二个 nib(chapterArrayController 引用的那个)的代码吗?
  • MyztikJenz:如果chapterArrayControllernil,则向它询问arrangedObjects 将返回nil。向nil 询问count 将返回0。他说count 返回非零;因此,chapterArrayController 不是 nil
  • Peter:我的 chapterArrayController 在发送到 NSLog 时返回 (null)。我只是不知道为什么要这样做。 NSLog(@"Chapter Array Controller %@", chapterArrayController);
  • 你确定chapterArrayController 被连接到笔尖了吗?您的代码看起来应该可以正常工作。您可以单击按钮触发 saveChapter: 的事实表明 nib 已加载,但为什么 ivar 为 nil 很奇怪。
【解决方案2】:
[[self alloc] standAloneTestArrayControllers];

永远不要这样做。始终通过发送init(或initWithSomethingElse:,取决于类提供的初始化程序)消息来初始化实例。如果您向新分配但尚未初始化的实例发送任何其他消息,则结果是未定义的并且几乎肯定是错误的。

[bookController fetchWithRequest:nil merge:NO error:nil];
NSLog(@"1. bookController %@", chapterArrayController);

您确定您不是要在此处登录bookController,而不是chapterArrayController

如果我从 JBAddChapter 的 IBAction 调用standAloneTestArrayControllers,一切正常。如果我使用 JBAddChapter 中的工厂方法从 AppController 调用相同的方法,则数组控制器为零。

因为你还没有加载笔尖。您既没有从 nib 加载章节对象,也没有从 nib 加载章节对象,也没有任何明确的(如setChapterArrayController:)给章节对象一个数组控制器来保持。

它通过 action 方法起作用,因为在这种情况下,您已经加载了笔尖。

那么,为什么数组控制器会从一种方法返回 nil,而不是另一种?

数组控制器没有返回nil;他们不存在。 (此外,函数和方法返回值,而不是对象或类。)您为保存数组控制器而创建的变量持有 nil,因为您没有在其中放置任何数组控制器。

假设 JBAddChapter 旨在拥有添加章节表,它应该在其 init 方法中加载 nib(或实例化窗口控制器),并且您应该从主窗口的所有者创建对象(并拥有该对象拥有 add-chapter 对象,并且只使用它拥有的 add-chapter 对象)或使 add-chapter 对象成为单例。前者更容易。

当然,当您创建此对象或任何其他对象时,您必须始终使用alloc + init/initWith… 对(或提供的自动释放便利方法);否则,和现在一样,您的对象将不会加载其 nib,因为您从未向其发送 init 消息。

【讨论】:

    【解决方案3】:

    我想我会在这里回答我自己的问题。我的 NIB 出了点问题,一旦我删除了我的阵列控制器,并在 IB 中重新连接它们,一切都会自动恢复正常。

    【讨论】:

    • 你知道,我以前见过。我发现唯一可行的其他选项(并且仅在某些时候)是将 nib 保存为 xib,反之亦然。 IB中的错误?我一直无法可靠地复制它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    相关资源
    最近更新 更多