尝试像这样合成 bool(boolean) 数组时收到编译器错误:
// .h
#import <UIKit/UIKit.h>
@interface SomeViewController : UIViewController {
BOOL boolArray[100];
}
@property (nonatomic) BOOL boolArray;
@end
//m
#import "SomeViewController"
@implementation SomeViewController
@synthesize boolArray;
@end
我可能犯了一个根本性的错误,但是我现在可以找到它,用boolArray [100]进行的合成也不起作用。
最佳答案
您可能需要完整类型,即
@property(非原子)BOOL boolArray [100] ;
[100]是重要的类型信息,不仅表示要分配多少空间。
另外,我认为该属性将被视为无法分配的const BOOL *
,因此可能必须是readonly
。正确的做法可能是将其设置为只读,这意味着Thins将获取数组指针,然后对其下标以分配给数组的成员。
或者,您可以为此使用NSArray
,但这将要求您将NSNumber
与boolVaules一起使用,这更像是一种生物技术。
更新
实际上,由于某些原因,愚蠢的编译器不喜欢[]。尝试这个:
@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
另一个更新
这样编译:
@interface TestClass : NSObject {
BOOL boolArray[100];
}
@property (nonatomic, readonly) const BOOL *boolArray;
@end
@implementation TestClass;
- (const BOOL *)boolArray {
return boolArray;
}
@end
这是一个奇怪的问题。我希望编译器能确切解释它对“无法使用数组类型声明属性”之类的不满意之处。
还可以更新
看到这个问题:Create an array of integers property in Objective C
显然,根据C规范,数组不是“普通旧数据”类型,而Objective-C规范仅允许您声明POD类型的属性。大概这是POD的定义:
http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html
但是阅读似乎是一系列POD就是POD。所以我不明白。
关于objective-c - iPhone SDK合成BOOL数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2124410/