iPhone SDK合成BOOL数组 [英] iPhone SDK synthesizing BOOL Array
问题描述
在尝试合成这样的bool数组时,我收到编译器错误:
// .h
#import< UIKit / UIKit.h>
@interface SomeViewController:UIViewController {
BOOL boolArray [100];
}
@property(nonatomic)BOOL boolArray;
@end
// m
#importSomeViewController
@implementation SomeViewController
@synthesize boolArray;
@end
我可能犯了一个根本的错误,
你可能需要完整的类型,即
@property(nonatomic)BOOL boolArray [100] ;
[100]是重要的类型信息,
此外,我认为该属性将被视为一个 const BOOL *
因此它可能必须 readonly
。正确的事情可能是让这个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
/ strong>
这会编译:
@interface TestClass:NSObject {
BOOL boolArray [100];
}
@property(nonatomic,readonly)const BOOL * boolArray;
@end
@implementation TestClass;
- (const BOOL *)boolArray {
return boolArray;
}
@end
这是一个奇怪的问题。我希望编译器能够解释什么是不喜欢的不能声明属性与数组类型或某事。
还有其他更新
查看此问题:在Objective C中创建一个整数数组属性
显然,根据C规范,数组不是一个普通数据类型,而Objective-C规范仅允许您声明POD类型的属性。假设这是POD的定义:
http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html
但是看来,它看起来像一个POD数组是一个POD。所以我不明白。
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.
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 NSNumber
s 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.
这篇关于iPhone SDK合成BOOL数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!