Pergunta

How do I set the array in my model-class ValueItem with the content of an NSArrayControllerin my AppDelegate class:

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

and:

@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 returns NULL. What do I miss here? (valueArray is initialized with @property and @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
Foi útil?

Solução

Solution: Need to make sure you're dealing with the AppDelegate's vi property:

// 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);

Explanation : On the first two lines you were manipulating the array ValueItem variable, then attempting to set testArray2 to the value of the uninitialized vi ValueItem variable.

// 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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top