如何设置 属性 的 UICollectionViewCell 只读 eligantly
How to set a property of UICollectionViewCell readonly eligantly
我有一个 UITableViewCell ProductsCell
,我在 nib 中注册了它,然后出队使用它。
我想将它的 属性 设置为只读。如何做得更好?
代码如下:
@property (nonatomic, strong, readonly) MyProductsVC * targetMyProductsVC;
- (MyProductsVC *)targetMyProductsVC{
if(!_targetMyProductsVC){
UIResponder *target = self.nextResponder;
do {
target = target.nextResponder;
} while (![target isKindOfClass: ZBMyProductsVC.self] && target != nil);
_targetMyProductsVC = (ZBMyProductsVC *)target;
}
return _targetMyProductsVC;
}
我无法执行。在 -init
,-awakeFromNib
中,因为我使用 UIResponder
找到父 ViewController.
因为在上面的方法中,单元格似乎没有添加到超级视图中。
如果我这样做,
@property (nonatomic, strong, readonly) MyProductsVC * targetMyProductsVC;
Xcode 报告:
Use of undeclared identifier '_targetMyProductsVC'
设置两个就可以了属性。一个像上面一样在里面,只需将另一个只读 属性 设置在 getter
方法之外(return
前者 属性。)
有点脏,
有更好的方法吗?
代码可以运行:
@property (nonatomic, strong, readonly) MyProductsVC * targetMyProductsVCReadOnly;
@property (nonatomic, strong) MyProductsVC * targetMyProductsVC;
- (MyProductsVC *)targetMyProductsVC{
if(!_targetMyProductsVC){
UIResponder *target = self.nextResponder;
do {
target = target.nextResponder;
} while (![target isKindOfClass: ZBMyProductsVC.self] && target != nil);
_targetMyProductsVC = (ZBMyProductsVC *)target;
}
return _targetMyProductsVC; }
- (MyProductsVC *)targetMyProductsVCReadOnly{
return self.targetMyProductsVC; }
因为您正在为 readonly
属性 实现自己的 属性 getter,这意味着只有一个访问器(即没有 setter), 编译器不会自动声明支持变量。来自 Encapsulating Data:
Note: The compiler will automatically synthesize an instance variable in all situations where it’s also synthesizing at least one accessor method. If you implement both a getter and a setter for a readwrite
property, or a getter for a readonly
property, the compiler will assume that you are taking control over the property implementation and won’t synthesize an instance variable automatically.
If you still need an instance variable, you’ll need to request that one be synthesized:
@synthesize property = _property;
您在 getter 实施之前插入以上内容以解决 未声明的变量 错误。
HTH
我有一个 UITableViewCell ProductsCell
,我在 nib 中注册了它,然后出队使用它。
我想将它的 属性 设置为只读。如何做得更好?
代码如下:
@property (nonatomic, strong, readonly) MyProductsVC * targetMyProductsVC;
- (MyProductsVC *)targetMyProductsVC{
if(!_targetMyProductsVC){
UIResponder *target = self.nextResponder;
do {
target = target.nextResponder;
} while (![target isKindOfClass: ZBMyProductsVC.self] && target != nil);
_targetMyProductsVC = (ZBMyProductsVC *)target;
}
return _targetMyProductsVC;
}
我无法执行。在 -init
,-awakeFromNib
中,因为我使用 UIResponder
找到父 ViewController.
因为在上面的方法中,单元格似乎没有添加到超级视图中。
如果我这样做,
@property (nonatomic, strong, readonly) MyProductsVC * targetMyProductsVC;
Xcode 报告:
Use of undeclared identifier '_targetMyProductsVC'
设置两个就可以了属性。一个像上面一样在里面,只需将另一个只读 属性 设置在 getter
方法之外(return
前者 属性。)
有点脏,
有更好的方法吗?
代码可以运行:
@property (nonatomic, strong, readonly) MyProductsVC * targetMyProductsVCReadOnly;
@property (nonatomic, strong) MyProductsVC * targetMyProductsVC;
- (MyProductsVC *)targetMyProductsVC{
if(!_targetMyProductsVC){
UIResponder *target = self.nextResponder;
do {
target = target.nextResponder;
} while (![target isKindOfClass: ZBMyProductsVC.self] && target != nil);
_targetMyProductsVC = (ZBMyProductsVC *)target;
}
return _targetMyProductsVC; }
- (MyProductsVC *)targetMyProductsVCReadOnly{
return self.targetMyProductsVC; }
因为您正在为 readonly
属性 实现自己的 属性 getter,这意味着只有一个访问器(即没有 setter), 编译器不会自动声明支持变量。来自 Encapsulating Data:
Note: The compiler will automatically synthesize an instance variable in all situations where it’s also synthesizing at least one accessor method. If you implement both a getter and a setter for a
readwrite
property, or a getter for areadonly
property, the compiler will assume that you are taking control over the property implementation and won’t synthesize an instance variable automatically.If you still need an instance variable, you’ll need to request that one be synthesized:
@synthesize property = _property;
您在 getter 实施之前插入以上内容以解决 未声明的变量 错误。
HTH