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;
}
1
  • 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: stackoverflow.com/questions/476843/… Commented Jun 9, 2011 at 17:04

3 Answers 3

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).

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

4 Comments

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.
Ah... Okay I see. My bool just cannot be used outside of this class?
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.
Thank you zneak for explaining.
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.

6 Comments

This is very insightful, but I still don't know what to do to resolve this.
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.
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.
@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)?
@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.
|
0

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

5 Comments

You also don't need the [100]... unless I am missing something here? A BOOL can be YES or NO, or nil. (if it's just one toggle you are working with for now, I assume)
It's a hundred bools. It worked before I subclassed this bool into a different file and got this error.
Did you try removing the * as well?
Like the one zneak suggested to add? Yeah because I had that at first.
Can someone explain why this was downvoted? Is krypton wrong?

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.