1

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
4
  • Need to see your ValueItem code. Also what is output of NSLog(@"%@", [outArrayController arrangedObjects]);? Jan 31, 2013 at 22:48
  • Hello Michael, thanks for your reply. Reading the arrayControllers content with NSLog(@"%@", [outArrayController arrangedObjects]); works fine and returns the content as expected. I added the ValueItem code.
    – JFS
    Jan 31, 2013 at 22:58
  • Check the output of NSLog(@"%@", vi.valueArray); Jan 31, 2013 at 23:04
  • Hi Michael it returns still NULL. The code is inside an action (button) method. Is that important for the "code flow"
    – JFS
    Jan 31, 2013 at 23:09

1 Answer 1

0

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);
8
  • Hello Michael, it still returns NULL but I'm not sure if i use the right getter method with NSArray *testArray2 = vi.valueArray; and NSLog(@"test array 2 is:%@", testArray2);
    – JFS
    Jan 31, 2013 at 23:02
  • vi.valueArray should work. Check my comment on your question. Jan 31, 2013 at 23:05
  • Awesome, it worked. Thanks. But I don't get the difference. Why do I need to use the different setter method for the array? To set the model entities I use ValueItem *newItem = [[ValueItem alloc]init]; and [newItem setValue:[NSNumber numberWithDouble:[_inputNomValue doubleValue]] forKey:@"nomValue"];
    – JFS
    Jan 31, 2013 at 23:19
  • Because you were trying to set testArray2 to the value of vi.valueArray, which had not been set yet. Instead you were initializing array, but not using it on the aforementioned line. Jan 31, 2013 at 23:20
  • Ah, I got it. A big Thank you to you. Not everybody is staying with a greenhorn like me until the end. The best for you! You helped me a lot.
    – JFS
    Jan 31, 2013 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.