2

I have 2 classes, ClassA and ClassB

ClassA has one BOOL variable set to No.

I am trying to set this variable to Yes from ClassB, but can't seem to figure out how to.

Below is the code I am using which doesn't work, it is simply what I would've thought would work, I have stripped out the unnecessary information:

Class A:

ClassA.h

@interface AppDelegate : NSObject <NSApplicationDelegate> {
    BOOL boolean;
}

- (id) init;

ClassA.m

- (id) init {
    boolean = NO;
}

Class B:

ClassB.h

#import "ClassA.h"

- (IBAction) setBoolean: (id)sender;

ClassB.m

- (id) init {
    ClassA * theClassA = [[ClassA alloc] init];
    return self;
}

- (IBAction) setBoolean: (id)sender {
    [theClassA boolean] = YES;
}

I hope this makes sense. I simply want to set the BOOL boolean in ClassA to YES from ClassB.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Cristian
  • 6,765
  • 7
  • 43
  • 64
  • This code does not make much sense. Your `init`s are all wrong, as they do not assign to `self`; you are creating `theClassA` as a local variable that disappears as soon as your `init` is over; if your `ClassA` appears to be your `AppDelegate`, why would you create another instance of it?.. – Sergey Kalinichenko May 17 '12 at 14:48
  • I'm still learning, the answer posted below solved my questions, thanks anyway – Cristian May 17 '12 at 15:06

1 Answers1

4

You can't assign a property like that ([object property] = value). The proper syntax is [object setProperty:value] or object.property = value.

I wouldn't call a variable boolean. Might be misleading. Even though it's not the keyword for a boolean variable in Objective-C it is in a lot of other languages.

And you have to return the initialized object (self) in your init method (you have an id return type, not void):

- (id) init {
    self = [super init];
    if (self) {
        boolean = NO;
    }
    return self;
}

Also, you didn't specify an instance variable for theClassA in your ClassB implementation. You just create a local object and then leak it (you don't release it). Instead, declare it in your ClassB.h:

@class ClassA;
@interface ClassB : NSObject {
    ClassA *theClassA;
}
- (IBAction)setBoolean:(id)sender;
@end

Then initialize it like this:

- (id) init {
    self = [super init];
    if (self) {
         theClassA = [[ClassA alloc] init];
    }
    return self;
}

And don't forget to release it in dealloc:

- (void)dealloc {
    [theClassA release];
    [super dealloc];
}

And one last thing. Having a method - (IBAction) setBoolean: (id)sender in your ClassB implies that ClassB has a property called boolean, which is not the case. I recommend renaming that method and/or rethinking your class designs.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • 1
    You are missing the biggest issue: since `theClassA` is a *local* variable declared in another method, it's not available inside `setBoolean`. – Sergey Kalinichenko May 17 '12 at 14:51
  • Perfect, exactly what I needed, thanks so much! Can I just ask why in the init method you declare self as [super init]? and why self should evaluate to true? I don't understand that part, but thanks again! – Cristian May 17 '12 at 15:02
  • http://stackoverflow.com/questions/1287950/in-objective-c-why-should-i-check-if-self-super-init-is-not-nil – DrummerB May 17 '12 at 15:05