-3

Somehow i can not change the properties of my custom object anymore. I used Xcode 6 to create my project and moved to XCode 7 now. It told me to "update to recommended settings" and i did it.

Object.h

@interface Object : NSObject <NSCoding>

@property (nonatomic, copy) NSString *ID;

ViewController.m

#import "Object.h"

@interface ViewController ()

@end

Object *myObject;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    myObject = [[Object alloc] init];
}

- (IBAction)editProperty:(id)sender {
    myObject.ID = _textfield.text;
    NSLog(@"ID : %@",myObject.ID);
}

This all worked perfectly fine, but now myObject.ID is always (null).....

When i write this code:

    myObject.ID = _textfield.text;
    NSLog(@"ID : %@",myObject.ID);

inside viewDidLoad it works...

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    You have a wonderful debugger. Debug! Is `myObject` nil? Is `_textfield` nil? Figure out what's going on. Solve it yourself. – matt Nov 21 '15 at 15:46
  • Also, a lot depends on when and where you call `editProperty`, doesn't it? But you have not shown that, so who knows what you're doing. If you wanted help, you would reveal, not conceal. – matt Nov 21 '15 at 15:48
  • myObject is not nil and even if i 'myObject.ID = @"TEST";' it will result in ID being nil – MothaFvckaJones Nov 21 '15 at 16:02

1 Answers1

0

One major issue is this line:

Object *myObject;

What is myObject? It is just floating free. It is not a property. It is not an instance variable. So what is it? That line is legal but it makes little sense.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I create 'myObject' there so i can access it everywhere in the class. – MothaFvckaJones Nov 21 '15 at 16:21
  • But you can't access it everywhere in that class, can you? That's the problem. It isn't working. I'm telling you why. But... You are really not listening. You could do it with a property or an instance variable and then you would have memory management. But what you have now, you don't even know what it is. – matt Nov 21 '15 at 16:26
  • Never saw this either, put this between interface ViewController () and end: @property (strong, nonatomic) Object *myObject; Change myObject = [[Object alloc] init]; into self.myObject = [[Object alloc] init]; and access it with _myObject instead of myObject. Edit: this will simply create a property. – Rick van der Linde Nov 21 '15 at 17:00