我收到这些奇怪的错误,但我不明白它们。以下是错误:
error: variable - sized object may not be initialized (#1)
error: statically allocated instance of Objective-C class 'Joke' (#1)
error: statically allocated instance of Objective-C class 'Joke' (#1)
error: cannot convert to a pointer type (# 2)
(注意:错误后面的数字将指示错误在我的实现文件中的位置)
这是我的 .m 文件:
#import "Joke.h"
@implementation Joke
@synthesize joke;
@synthesize rating;
- (id)init {
[super init];
return self;
}
- (void)dealloc {
[joke release];
[super dealloc];
}
+ (id)jokeWithValue:(NSString *)joke {
Joke j = [[Joke alloc] init]; // (# 1) This is where #1 errors occurred
j.joke = joke;
return [j autorelease]; // (# 2) This is where #2 errors occurred
}
@synthesize joke;
@synthesize rating;
@end
谢谢!
最佳答案
Objective-C 对象的实例必须是指针,这导致了您的问题。你的笑话的初始化应该是:
Joke *j = [[Joke alloc] init];
此外,对象将自身保留为循环引用也是一个坏主意。你将有无限递归 j->joke->joke->joke->joke->joke...
关于iphone - 有人可以向我解释这些错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/850888/