2

I get a compiler error when trying to synthesize a bool array like this:

// .h

#import <UIKit/UIKit.h>


@interface SomeViewController : UIViewController {

    BOOL boolArray[100];
}

@property (nonatomic) BOOL boolArray;

@end


//m

#import "SomeViewController"


@implementation SomeViewController

@synthesize boolArray;

@end

I probably did a fundamental mistake, but I can find it right now, synthesizing with boolArray[100] didn't work either.

Nimrod
  • 5,168
  • 1
  • 25
  • 39
scud
  • 1,147
  • 1
  • 14
  • 25

1 Answers1

0

You probably need the full type, i.e. @property (nonatomic) BOOL boolArray [100];

The [100] is significant type information, not just an indication of how much space to allocate.

Also, I think the property will be treated like a const BOOL * that can't be assigned, so it would probably have to be readonly. The correct thing to do is probably make this readonly, which means that thins will fetch the array pointer then subscript it to assign to members of the array.

Alternately you can use an NSArray for this, but that will require that you use NSNumbers with boolVaules which is more of a biotch to deal with.

UPDATE

Actually the stupid compiler doesn't like the [] for some reason. Try this:

@interface TestClass : NSObject {

    const BOOL *boolArray;
}

@property (nonatomic, readonly) const BOOL *boolArray;

@end


@implementation TestClass;

- (const BOOL *)boolArray {
    if (!boolArray)
        boolArray = malloc(sizeof(BOOL) * 100);
    return boolArray;
}

- (void)dealloc {
    [super dealloc];
    free((void *)boolArray);
}

@end

ANOTHER UPDATE

This compiles:

@interface TestClass : NSObject {

    BOOL boolArray[100];
}

@property (nonatomic, readonly) const BOOL *boolArray;

@end


@implementation TestClass;

- (const BOOL *)boolArray {
    return boolArray;
}

@end

This is a bizarre issue. I wish the compiler would explain exactly what it's unhappy about like "Can't declare property with array type" or something.

YET ANOTHER UPDATE

See this question: Create an array of integers property in Objective C

Apparently according to the C spec, an array is not a "Plain Old Data" type and the Objective-C spec only lets you declare properties for POD types. Supposedly this is the definiition of PODs:

http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html

But reading that it seems like an array of PODs is a POD. So I don't get it.

Community
  • 1
  • 1
Nimrod
  • 5,168
  • 1
  • 25
  • 39
  • Alright, I'll switch over and just use a NSArray - thanks for the advice – scud Jan 23 '10 at 19:46
  • Actually this doesn't work. I don't get what Obj-C's prob is with [100] so see update. – Nimrod Jan 23 '10 at 19:56
  • See yet another update, possibly better. This is weird. I'm going to research this further because I often want to bypass having to use NSArrays for simple stuff, and I really don't get what the compiler is bitching about with the property decl. – Nimrod Jan 23 '10 at 20:20