iphone - 有人可以向我解释这些错误吗?

标签 iphone objective-c cocoa-touch

我收到这些奇怪的错误,但我不明白它们。以下是错误:

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/

相关文章:

ios - 无法将多个参数从 iOS 发送到 Cloud Code

objective-c - 如何将 NIB 文件中的对象发送到前/后?

ios - writeToFile 在 iOS 7 上工作而不在 iOS 6 上工作

ios - 缩放图像直到完全适合 ImageView

iphone - 以编程方式强制 UIScrollView 停止滚动,以便与多个数据源共享 TableView

iphone - AppStore更新后神秘的应用程序崩溃

iphone - 在横向模式下隐藏标签栏

iphone - PDF 到 EPUB 转换过程中丢失的图像

objective-c - 点符号与方括号和 Objective-C 中的转换

cocoa-touch - 如何像 UIToolbar 一样使用色调颜色绘制 UIButton