Im getting these weird errors, but I dont understand them. Here are the errors:
我得到这些奇怪的错误,但我不理解它们。以下是错误:
error: variable - sized object may not be initialized (#1)
错误:可能无法初始化变量大小的对象(#1)
error: statically allocated instance of Objective-C class 'Joke' (#1)
错误:静态分配的Objective-C类实例'笑话'(#1)
error: statically allocated instance of Objective-C class 'Joke' (#1)
错误:静态分配的Objective-C类实例'笑话'(#1)
error: cannot convert to a pointer type (# 2)
错误:无法转换为指针类型(#2)
(Note: The number after the error will indicate where the error was in my implementation file)
(注意:错误后面的数字将指示错误在我的实现文件中的位置)
Here is my .m file:
这是我的.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
Thanks!
2 个解决方案
#1
Instances of Objective-C objects must be pointers, which is causing your problem. Your initialization of joke should be:
Objective-C对象的实例必须是指针,这会导致您的问题。你的笑话初始化应该是:
Joke *j = [[Joke alloc] init];
Also, it's a bad idea for an object to hold onto itself as a circular reference. You would have infinite recursion with j->joke->joke->joke->joke->joke...
此外,对象保持自身作为循环引用是一个坏主意。你可以使用j-> joke-> joke-> joke-> joke-> joke来进行无限递归...
#2
You need a "*" before your variables -- for instance, "Joke *j = [[Joke alloc] init];"
你的变量之前需要一个“*” - 例如,“Joke * j = [[Joke alloc] init];”
You also only want @synthesize in there once - not for each property. Like this: @synthesize joke, rating;
你也只想在那里使用@synthesize - 而不是每个属性。像这样:@synthesize笑话,评级;
#1
Instances of Objective-C objects must be pointers, which is causing your problem. Your initialization of joke should be:
Objective-C对象的实例必须是指针,这会导致您的问题。你的笑话初始化应该是:
Joke *j = [[Joke alloc] init];
Also, it's a bad idea for an object to hold onto itself as a circular reference. You would have infinite recursion with j->joke->joke->joke->joke->joke...
此外,对象保持自身作为循环引用是一个坏主意。你可以使用j-> joke-> joke-> joke-> joke-> joke来进行无限递归...
#2
You need a "*" before your variables -- for instance, "Joke *j = [[Joke alloc] init];"
你的变量之前需要一个“*” - 例如,“Joke * j = [[Joke alloc] init];”
You also only want @synthesize in there once - not for each property. Like this: @synthesize joke, rating;
你也只想在那里使用@synthesize - 而不是每个属性。像这样:@synthesize笑话,评级;