2

Newbie here (to C, objective-C, and iOS)...

For this code in my AppDelegate.m, why is the freshly initialized array empty? Looks to me like I give it some values. Clue1 etc are are series of UILabels and there are no issues noted by Xcode. I've been through SO quite a bit and this seems to be the way to do it. Xcode 4.2. Thanks!

- (id)init {       
    self = [super init];
    if(self) {           
        // Make arrays containing the objects: this is the objective-C way!
       self.clueLabs = [[NSMutableArray alloc] initWithObjects: Clue1, Clue2, Clue3, nil];
    }   
    NSLog(@"%@", [clueLabs description]);
    if (!clueLabs || ![clueLabs count]) { // these 2 do m/l the same thing
        NSLog(@"clueLabs is empty!");
    }       
    return self; 
}
2
  • Are you using ARC? If not, is clueLabs a retained property? If so, you are over retaining it. Commented Jan 29, 2012 at 4:34
  • Yes, ARC is on. Header file contains "IBOutlet UILabel *Clue1;" and '@property (strong) UILabel *Clue1' for each clue Commented Jan 29, 2012 at 4:49

1 Answer 1

3

Check if Clue1, Clue2 Clue3 are not nils.

Sign up to request clarification or add additional context in comments.

8 Comments

Ah, that might be it. I declare them, but don't give them any values. Thanks, I'll try that. Silly me.
Can I alloc and init the array with nil objects, then set them to some value in a loop, or must I individually init the UILabels to some non nil value? 1st option much shorter code! EDIT: maybe loop approach is not possible b/c count is zero and can't iterate over an empty array to fill it...
When you init the array with method initWithObjects - they may not be nil. You may create an empty NSMutableArray and fill it with objects later though.
Thanks, I'm back in business!
OK, partially back in business. By init'ing these labels, I'm creating them programatically, but I have already created them via IB. So essentially I have created a totally new set of labels. My creation of the new labels is like this: self.Clue1 = [[UILabel alloc] init]; then [Clue1 setText:@""]; Do I need to add something to make Xcode realize these are the same buttons as in the nib/xib? Thanks.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.