【发布时间】:2015-02-10 04:24:18
【问题描述】:
我正在关注 Apple 的“使用 Objective C 编程”文档,链接为:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html#//apple_ref/doc/uid/TP40011210-CH4-SW1
无论如何,我已经到了它要求调用 sayHello 方法的地步。
“使用alloc和init创建一个新的XYZPerson实例,然后调用sayHello方法。”
#import <Foundation/Foundation.h>
#import "XYZPerson.h"
int main(int argc, const char * argv[]);
XYZPerson *firstPerson = [[XYZPerson alloc] init]; //Initializer element is not a lime-time constant
[firstPerson sayHello]; //No Visible @interface for 'XYZPerson' delcares the selector 'sayHello'
@implementation XYZPerson
- (void)sayHello {
[self saySomething:@"Hello, World"];
}
- (void)saySomething: (NSString *)greeting {
NSLog(@"%@", greeting);
}
@end
我相信我对苹果如何解释这项工作存在误解,或者只是不知道。
希望苹果完成这些示例供我们复习。
【问题讨论】:
-
您是否按照他们所说的方式实现了 XYSperson.h 和 .m ?
-
您的
XYZPerson.m应该不有main的声明。摆脱那条线。另外,为什么@implementation行之前有代码?该代码不得存在。 -
这段代码在 main.m 文件中,而我也有上一章中的 XYZPerson.h 和 XYZPerson.m,为什么之前有它不好?该示例要求我在 main.m 文件中使用 allocate 和 init 创建一个实例。除非我做错了?
标签: objective-c methods