【发布时间】:2012-09-20 18:38:50
【问题描述】:
这是我在我的一个类的实现文件中的内容...
代码设置 #1
@interface MyViewController (PrivateMethods)
- (NSString *)myPrivateMethod;
@end
@implementation MyViewController
- (void)viewDidLoad
{
NSString *myString = [self myPrivateMethod];
NSLog(@"%@", myString);
}
- (NSString *)myPrivateMethod
{
return @"someString";
}
@end
使用此代码,一切正常,并记录“someString”。
但是我的代码不应该以某种方式看起来不同吗?我实际上是偶然使用该类别的(我复制/粘贴了一些东西,但没有注意到“PrivateMethods”在那里;我的意思是使用类扩展)。
我的代码不应该看起来像以下之一吗:
代码设置 #2
@interface MyViewController ()
- (NSString *)myPrivateMethod;
@end
@implementation MyViewController
....
或者:
代码设置 #3
@interface MyViewController (PrivateMethods)
- (NSString *)myPrivateMethod;
@end
@implementation MyViewController (PrivateMethods)
....
在这种情况下发生的事情背后有哪些细微差别?代码设置 #1 与代码设置 #2 有何不同?
编辑:关于设置 #3 的问题
这样设置有什么作用?这甚至会“工作”吗?
@interface MyViewController (PrivateMethods)
- (NSString *)myPrivateMethod;
@end
@implementation MyViewController
- (void)viewDidLoad
{
NSString *myString = [self myPrivateMethod];
NSLog(@"%@", myString);
}
@end
@implementation MyViewController (PrivateMethods)
- (NSString *)myPrivateMethod
{
return @"someString";
}
@end
【问题讨论】:
标签: objective-c categories class-extensions