-1

I have two classes. I'm trying to access a property from Class2 in Class1 (both subclasses of UIImageView). The x property is 0 when accessing it from Class1.

In Class2.h:

@interface ...
{
    int x;
}

@property (readwrite, nonatomic) int x;

In Class2.m:

@synthesize x;

- (void)mainMethod
{
    x = 4;
    [NSTimer scheduledTimerWithTimeInterval:0.02 
                                     target:self 
                                   selector:@selector(updateX) 
                                   userInfo:nil 
                                    repeats:YES]
}

- (void)updateX
{
    x += 5;
}

In Class 1:

- (void)mainMethod
{
    Class2 obj = [[Class2 alloc] init];
    NSLog(@"%i", obj.x);
}

Have I done something wrong in the code provided or is the problem caused by something else?

Wilhelm Michaelsen
  • 665
  • 14
  • 32
  • 3
    Why do you have x as an instance variable as well as a property? – dudeman Jul 16 '15 at 16:49
  • @MikeAtNobel Doesn't matter, if I remove the instance variable and use self.x I still get no value for x in the other class – Wilhelm Michaelsen Jul 16 '15 at 16:53
  • Well, if you remove x as an instance variable, then I think you'll see inside your init you're actually modifying the instance variable and not the property (and the same for updateX). – dudeman Jul 16 '15 at 16:57
  • 3
    Does your code even compile? `init` should return (instancetype) or (id), not (void). – Glorfindel Jul 16 '15 at 17:47
  • There are a lot of questions you'll have to answer before we can help you. Also, remember your property won't have access to anything if its property isn't filled with something, and in this case it looks like your int x is private. Further, Have you included the .h file in your other class? – KFDoom Jul 16 '15 at 18:41
  • The method isn't `- (void)init` but `- (void)mainMethod` or something else. I just named the method init in this question to make it clear it is the first method being called in the class. I obviously shouldn't have used init – Wilhelm Michaelsen Jul 16 '15 at 18:52
  • Are you calling `mainMethod` somewhere? Why would the value change if you haven't changed it? – jscs Jul 16 '15 at 19:24

1 Answers1

0

You are never calling -[Class2 mainMethod], so x never gets the non-zero value. You'd want:

// in Class1

- (void)mainMethod
{
    Class2 c2obj = [[Class2 alloc] init];
    [c2obj mainMethod];
    NSLog(@"%i", c2obj.x);
}

While I'm at it:

  • Class1 is just confusing the issue. You don't need it for your example.
  • Likewise timer/updateX stuff.
  • If you are creating any UIView object, you should be using something like initWithFrame:CGRectZero.
  • Simplicity, simplicity, simplicity! ;)

[Answer to original question follows]

Many problems here, as enumerated by AlexC. That (void)init method will not be called to initialize the object, so .x would always be 0.

But I suspect the root problem is that init is not the designated initializer for any UIView. You likely should be using:

- (instancetype)initWithFrame:(CGRect)frame

or possibly initWithCoder:, if you are creating those views from Interface Builder.

Cf. Apple docs.

Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
  • The method isn't `- (void)init` but `- (void)mainMethod`. I just named the method init in this question to make it clear it is the first method being called in the class. I obviously shouldn't have used init. – Wilhelm Michaelsen Jul 16 '15 at 18:52
  • @WilhelmMichaelsen OK. While you a simplifying, axe the timer/updateX methods as well, they are not part of your problem, and are confusing the issue. – Clay Bridges Jul 16 '15 at 19:07