【发布时间】:2014-08-01 00:51:13
【问题描述】:
我是 Objective-C 的新手,正在尝试构建一个简单的类。我现在有一段工作代码,但由于某种原因,我无法摆脱两个警告。我做错了什么?
* 如果有任何不同,我会从终端使用 GCC 进行编译。
代码:
#import <objc/Object.h>
#import <stdio.h>
@interface ValueAssignment : Object
{
char name;
}
+ (void) setVar:(char) x_name;
+ (id) init;
@end
@implementation ValueAssignment
- (void) setVar:(char) x_name{
name = x_name;
}
- (id) init {
if(self = [super init]){
name = ' ';
}else{
return nil;
}
}
@end
/** Main program for the program execution entry **/
int main(int argv, char* argc[])
{
// id o = [ValueAssignment new];
id o = [[ValueAssignment alloc] init];
[o setVar:'9'];
printf("Bye.\n");
}
编译:
gcc -arch i386 -o hello -l objc test.m
输出:
test.m:26: warning: incomplete implementation of class ‘ValueAssignment’
test.m:26: warning: method definition for ‘+init’ not found
test.m:26: warning: method definition for ‘+setVar:’ not found
编辑:
如果我将实现部分更改为+ (id) init {,那么我会得到以下输出:
test.m: In function ‘+[ValueAssignment setVar:]’:
test.m:16: warning: instance variable ‘name’ accessed in class method
test.m: In function ‘+[ValueAssignment init]’:
test.m:21: warning: instance variable ‘name’ accessed in class method
【问题讨论】:
标签: objective-c