在.m中声明一些私有元素,而不是在.h上执行@property是一个好习惯吗?
并且,如果没关系,这些元素是否被视为weak?
示例:(在.m的顶部)
@implementation ParticipantMaterials{
UIImageView *imgBackground;
UILabel *lblTitle;
UITableView *tvTableContent;
NSMutableDictionary *tblElements;
}
Run Code Online (Sandbox Code Playgroud)
Leo*_*ica 14
在@implementation区域中声明变量时,您要声明实例变量,而不是属性.你没有@synthesizeivars.如果要声明隐藏在公共.h文件中的私有属性,可以像这样创建它们:
@interface ParticipantMaterials ()
@property (nonatomic) NSUInteger uintProp;
@property (nonatomic, copy) NSString* strProp;
@end
Run Code Online (Sandbox Code Playgroud)
这称为类扩展.
strong默认情况下会考虑实例变量,除非您指定了__weak类型修饰符.
如果要声明私有变量,那么任何这些声明都将执行:
@interface Animal : NSObject {
@private
NSObject *iPrivate;
}
@end
@interface Animal(){
@public
NSString *iPrivate2;
}
@property (nonatomic,strong) NSObject *iPrivate3;
@end
@implementation Animal {
@public
NSString *iPrivate4;
}
@end
Run Code Online (Sandbox Code Playgroud)
我添加@public指出它没有任何区别.所有这些变量都是私有的,如果你试图从子类或不同的类中设置它们,它将compiler error: undeclared identifier在第一种情况下产生或compiler error: variable is private.
默认情况下,ARC下的所有对象变量都很强,因此strong如果您愿意,可以省略@property.
Objective-C中的变量不是100%私有的.你可以得到/使用运行时功能随时随地为它们设置class_getInstanceVariable和object_getIvar.