ios Initializer element is not a compile-time constant
2014-05-27 18:11
330 查看
在scope外进行静态变量声明,
static const CGRect CouponListViewRect = CGRectMake(0.0,0.0,0.0,0.0);
报错:
Initializer element is not a compile-time constant
原理:
github上的回答:
static const CGRect CouponListViewRect = CGRectMake(0.0,0.0,0.0,0.0);
报错:
Initializer element is not a compile-time constant
原理:
github上的回答:
42 down vote | When you define a variable outside the scope of a function, that variable's value is actually written into your executable file. This means you can only use a constant value. Since you don't know everything about the runtime environment at compile time (which classes are available, what is their structure, etc.), you cannot create objective c objects until runtime, with the exception of constant strings, which are given a specific structure and guaranteed to stay that way. What you should do is initialize the variable to nil and use +initializeto create your image. initializeis a class method which will be called before any other method is called on your class. Example: NSImage*imageSegment = nil;+(void)initialize {if(!imageSegment) imageSegment =[[NSImage alloc] initWithContentsOfFile:@"/User/asd.jpg"];}-(id)init { self =[super init]; if(self){ // Initialization code here. } return self;} |
相关文章推荐
- initializer element is not a compile-time constant
- initializer element is not a compile-time constant
- initializer element is not a comply-time constant
- error: initializer element is not constant
- initializer element is not constant 问题
- 为什么出现“initializer element is not constant”错误
- C C++全局变量初始化 initializer element is not constant 错误
- error:“initializer element is not constant”
- 关于全局变量及静态变量和initializer element is not constant问题
- [问题记录]编译时出现 initializer element is not constant 错误
- initializer element is not constant
- initializer element is not constant 问题
- initializer element is not constant 问题
- initializer element is not constant
- initializer element is not constant
- C语言:initializer element is not constant
- initializer element is not constant
- 结构体初始化 initializer element is not constant
- initializer element is not constant 问题
- rbtree.c:6: error: initializer element is not constant