Ran*_*all 10 iphone xcode properties objective-c ios
有人告诉我,我可以将属性设为私有,这样只有类的一个实例可以引用它们(通过self.)
但是,如果我在类接口中使用@private然后正常声明属性,它仍然可以从类外部访问...那么如何才能将属性设为私有?请说出语法示例.
dtu*_*net 21
您需要在类扩展中包含这些属性.这允许您在接口声明中定义实现文件中的属性(以及最近的iVars).它类似于定义类别但在括号之间没有名称.
所以如果这是你的MyClass.m文件:
// Class Extension Definition in the implementation file
@interface MyClass()
@property (nonatomic, retain) NSString *myString;
@end
@implementation MyClass
- (id)init
{
self = [super init];
if( self )
{
// This property can only be accessed within the class
self.myString = @"Hello!";
}
}
@end
Run Code Online (Sandbox Code Playgroud)
在实现(.m)文件中声明属性,如下所示:
@interface MyClass()
@property (nonatomic, retain) MyPrivateClass *secretProperty;
@end
Run Code Online (Sandbox Code Playgroud)
您将能够在您的类中使用该属性而无需编译器警告.
归档时间: |
|
查看次数: |
6566 次 |
最近记录: |