【问题标题】:setting Array in model-class from NSArrayController从 NSArrayController 在模型类中设置数组
【发布时间】:2013-01-31 22:46:39
【问题描述】:

如何在我的模型类 ValueItem 中使用 AppDelegate 类中的 NSArrayController 的内容设置数组:

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    ValueItem *vi;
}

和:

@implementation AppDelegate
{

    ValueItem *array = [[ValueItem alloc]init];
    [array setValueArray:[outArrayController arrangedObjects]];

    NSArray *testArray2 = vi.valueArray; // !!!getter or setter doesn't work!!!
    NSLog(@"test array 2 is:%@", testArray2);
}

NSLog 返回NULL。我在这里想念什么? (valueArray 用@property@synthesize 初始化)

ValueItem.h:

#import <Foundation/Foundation.h>
@interface ValueItem : NSObject
{
    NSNumber *nomValue;
    NSNumber *tolerancePlus;
    NSNumber *toleranceMinus;
    NSMutableArray *valueArray;
}
@property (readwrite, copy) NSNumber *nomValue;
@property (readwrite, copy) NSNumber *tolerancePlus;
@property (readwrite, copy) NSNumber *toleranceMinus;
@property (nonatomic, retain) NSMutableArray *valueArray;

@end

ValueItem.m:

#import "ValueItem.h"
@implementation ValueItem

@synthesize nomValue, tolerancePlus, toleranceMinus;
@synthesize valueArray;

-(NSString*)description
{
    return [NSString stringWithFormat:@"nomValue is: %@ | tolerancePlus is: %@ | toleranceMinus is: %@", nomValue, tolerancePlus, toleranceMinus];

}
@end

【问题讨论】:

  • 需要查看您的ValueItem 代码。 NSLog(@"%@", [outArrayController arrangedObjects]); 的输出是什么?
  • 您好迈克尔,感谢您的回复。使用 NSLog(@"%@", [outArrayController arrangedObjects]); 读取 arrayControllers 内容可以正常工作并按预期返回内容。我添加了ValueItem 代码。
  • 查看NSLog(@"%@", vi.valueArray);的输出
  • 嗨迈克尔,它仍然返回NULL。代码位于操作(按钮)方法中。这对“代码流”很重要吗

标签: macos cocoa nsarraycontroller


【解决方案1】:

解决方案:需要确保您正在处理 AppDelegate 的 vi 属性:

// We need to make sure we're manipulating the AppDelegate's vi property!
self.vi = [[ValueItem alloc]init];
[vi setValueArray:[outArrayController arrangedObjects]];

NSArray *testArray2 = vi.valueArray; // !!!getter or setter doesn't work!!!
NSLog(@"test array 2 is:%@", testArray2);

解释: 在前两行中,您正在操作 array ValueItem 变量,然后尝试将 testArray2 设置为未初始化的 vi ValueItem 变量的值。

// This is a new variable, unrelated to AppDelegate.vi
ValueItem *array = [[ValueItem alloc]init];
[array setValueArray:[outArrayController arrangedObjects]];

// Here, AppDelegate.vi hasn't been initialized, so valueArray *will* be null!
NSArray *testArray2 = vi.valueArray; 
NSLog(@"test array 2 is:%@", testArray2);

【讨论】:

  • 你好 Michael,它仍然返回 NULL 但我不确定我是否使用了正确的 getter 方法和 NSArray *testArray2 = vi.valueArray;NSLog(@"test array 2 is:%@", testArray2);
  • vi.valueArray 应该可以工作。检查我对您问题的评论。
  • 太棒了,它奏效了。谢谢。但我不明白区别。为什么我需要对数组使用不同的 setter 方法?要设置模型实体,我使用 ValueItem *newItem = [[ValueItem alloc]init];[newItem setValue:[NSNumber numberWithDouble:[_inputNomValue doubleValue]] forKey:@"nomValue"];
  • 因为您试图将testArray2 设置为尚未设置的vi.valueArray 的值。相反,您正在初始化 array,但没有在上述行中使用它。
  • 啊,我明白了。非常感谢你。不是每个人都和我这样的新手呆到最后。最适合你的!你帮了我很多。
猜你喜欢
  • 1970-01-01
  • 2012-07-10
  • 2019-11-18
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
相关资源
最近更新 更多