1

I am getting the "Subscripted value is neither array nor pointer" error on a boolean made from another class that gets allocated in another. After looking up lots of reasons for getting this error, I'm not sure how I can relate it to mine. I'm using the Cocos2D library, but I don't think it has to do with that.

In another class this is my interface with a property. Just calling it ClassA for this example:

#import <Foundation/Foundation.h>
@interface ClassA : NSObject {
@public

    @public
        BOOL _deactivateLabelToggle;

}

@property(nonatomic, assign) BOOL deactivateLabelToggle;

.m

#import "ClassA.h"

@implementation ClassA
@synthesize deactivateLabelToggle = _deactivateLabelToggle;

BOOL _deactivateLabelToggle[100];

-(id) init{
    self = [super init];
    if (!self) {
        return nil;
    }

    return self;
}

- (void) dealloc{

    [super dealloc];

}   


@end

My main class header

#import "ClassA.h"
@class ClassA;

@interface MainClass : CCLayer {

    ClassA *classA;
}


@property(nonatomic, retain) ClassA *classA;

@end

MainClass.m

#import "MainClass.h"

@implementation MainClass

@synthesize classA;

-(id) init {

    if( (self=[super init] )) {

          classA = [[ClassA alloc] init];

          classA.deactivateLabelToggle[i] = 0; // <---- Error here

    }
    return self;
}
Chewie The Chorkie
  • 4,896
  • 9
  • 46
  • 90
  • In addition to zneaks helpful explanation, I just want to add that C arrays are not one of the supported data types for properties. Doesn't mean you can't flat out use them, but something like a NSArray or NSMutableArray should be used instead to make things easier to manage. See this Stack Overflow thread for more details: http://stackoverflow.com/questions/476843/create-an-array-of-integers-property-in-objective-c – Chewie The Chorkie Jun 09 '11 at 17:04

3 Answers3

3

Your deactivateLabelToggle property with this declaration in ClassA:

@property(nonatomic, assign) BOOL deactivateLabelToggle;

that you access on the // <---- Error here line is indeed neither array nor pointer. Simple BOOL variables can't be subscripted.

If you meant to reference the file-global BOOL array in your ClassA.m file, you can't do it with a synthesized property (and the type needs to be BOOL* instead of BOOL).

zneak
  • 134,922
  • 42
  • 253
  • 328
  • So are you saying it should be BOOL *_deactivateLabelToggle; in the interface and then a property @property BOOL *deactivateLabelToggle;? I still get the same error. – Chewie The Chorkie Jun 08 '11 at 21:49
  • Ah... Okay I see. My bool just cannot be used outside of this class? – Chewie The Chorkie Jun 08 '11 at 22:32
  • Your `BOOL` _instance variable_ (the one inside the `@interface` scope) can be accessed from outside the class. Your file-global `BOOL` array of the same name, however, can't (as easily as it is for the instance variable). Technically, the only relation this array has to your class is that it's defined in the same file. – zneak Jun 08 '11 at 23:43
  • Thank you zneak for explaining. – Chewie The Chorkie Jun 09 '11 at 16:08
1

Your problem is that you have an instance variable called "_deactivateLabelToggle" which is of type BOOL, and also a global variable with the same name which is an array of BOOLs. You seem to think you'll be accessing the global variable when you write classA.deactivateLabelToggle, but you're actually accessing the property that wraps the instance variable.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • This is very insightful, but I still don't know what to do to resolve this. – Chewie The Chorkie Jun 08 '11 at 21:51
  • WhyTF was this downvoted? If I'm wrong, it would be more useful to correct the misinformation. Just downvoting is pretty useless. It won't stop people from upvoting this some more, or me from further spreading whatever misconceptions I might have. – Chuck Jun 09 '11 at 00:30
  • I don't know, I don't even think I have the ability to downvote yet. But going back to my comment, I really like your information, but do you have anything else to add what I can do to access the global variable? I think I might be on to something now since zneak's explanation, but if you want to add anything else that would be helpful, thanks. – Chewie The Chorkie Jun 09 '11 at 16:11
  • @VagueExplanation: In your `init` method, you can put an `extern BOOL _deactivateLabelToggle` declaration to shadow the instance variable instead. But why is this a global variable? Is it really supposed to be shared among all instances of ClassA (and in fact the whole program)? – Chuck Jun 09 '11 at 16:59
  • @VagueExplanation: BTW, I didn't think it was you. I was just hoping the downvoter (whoever it is) might clarify, because I think downvotes without comment are just so rude and unhelpful. – Chuck Jun 09 '11 at 19:13
  • Thanks, yes I think it is supposed to be shared among all instances of ClassA. What's really going on is that I'm subclassing a character sprite in a game that can be instantiated x number of times so you can see more than one on the screen with all individual behaviors. deactivateLabelToggle is in another class for those characters that holds the array. So whenever I want to use a number of these characters I allocate the character sprite class x number of times and the class that holds the bool once. I'll look into extern BOOL and see if it works for me. – Chewie The Chorkie Jun 10 '11 at 16:00
0

You do not need the (nonatomic, assign) on the BOOL.

Luke
  • 11,426
  • 43
  • 60
  • 69