Objective c @属性未设置为新值
我有一个类作为模型过滤器。我需要从控制器访问此模型。 过滤器.h 在我的控制器中:Objective c @属性未设置为新值,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我有一个类作为模型过滤器。我需要从控制器访问此模型。 过滤器.h 在我的控制器中: @implementation NavigationController @synthesize filter = _filter; - (IBAction)setVirtualShowProperty:(UISwitch*)sender { self.filter.virtualRoomSet = YES; _filter.virtualRoomSet = YES; // I dont k
@implementation NavigationController
@synthesize filter = _filter;
- (IBAction)setVirtualShowProperty:(UISwitch*)sender {
self.filter.virtualRoomSet = YES;
_filter.virtualRoomSet = YES; // I dont know which syntax is better
}
@end
但它没有更新virtualRoomSet,它仍然是“否”。臭虫在哪里?谢谢展示更多的过滤器实现。可能有很多事情导致属性无法设置。最值得注意的是,它不在您的公共界面中
@interface Filter : NSObject
@property (nonatomic, assign) BOOL name;
@property (nonatomic, assign) BOOL virtualRoomSet;
@end
把它放在那里不会有什么区别,但它应该使目标的意图更加明确。关于控制器的实现:
#import "Filter.h"
@implementation NavigationController
@property (nonatomic, retain) Filter *filter;
@end
@implementation NavigationController
@synthesize filter;
//assuming you've properly alloced in init...
-(void) dealloc
{
[filter release];
[super dealloc];
}
- (IBAction)setVirtualShowProperty:(UISwitch*)sender {
self.filter.virtualRoomSet = YES; //better syntax as dealing directly w/ iVars is error prone
}
@end
@implementation Filter
@synthesize filter;
//other code
@end
使用属性而不是直接访问IVAR可以减少必须编写的内存管理代码的数量,并提供在必要时使用协作对象替换IVAR的能力。我通常避免同时声明IVAR,因为它们现在是隐式假定的。这让你远离了“合成myVar=\u myVar;”的丑陋
如果过滤器中有类似的内容:
@interface Filter : NSObject {
BOOL _virtualRoomSet;
}
@property (nonatomic, assign) BOOL name;
@property (nonatomic, assign) BOOL virtualRoomSet;
@end
在您的过滤器实现中:
#import "Filter.h"
@implementation NavigationController
@property (nonatomic, retain) Filter *filter;
@end
@implementation NavigationController
@synthesize filter;
//assuming you've properly alloced in init...
-(void) dealloc
{
[filter release];
[super dealloc];
}
- (IBAction)setVirtualShowProperty:(UISwitch*)sender {
self.filter.virtualRoomSet = YES; //better syntax as dealing directly w/ iVars is error prone
}
@end
@implementation Filter
@synthesize filter;
//other code
@end
…它可以解释这个bug,这进一步强调了我上面关于避免声明IVAR的观点!换句话说,请确保您实际设置了要设置的内容。自大约iOS SDK 3.0或更早版本以来,现在自动为所有属性声明隐式iVar。根据经验,在代码中删除IVAR,只处理属性。展示更多的过滤器实现。可能有很多事情导致属性无法设置。最值得注意的是,它不在您的公共界面中
@interface Filter : NSObject
@property (nonatomic, assign) BOOL name;
@property (nonatomic, assign) BOOL virtualRoomSet;
@end
把它放在那里不会有什么区别,但它应该使目标的意图更加明确。关于控制器的实现:
#import "Filter.h"
@implementation NavigationController
@property (nonatomic, retain) Filter *filter;
@end
@implementation NavigationController
@synthesize filter;
//assuming you've properly alloced in init...
-(void) dealloc
{
[filter release];
[super dealloc];
}
- (IBAction)setVirtualShowProperty:(UISwitch*)sender {
self.filter.virtualRoomSet = YES; //better syntax as dealing directly w/ iVars is error prone
}
@end
@implementation Filter
@synthesize filter;
//other code
@end
使用属性而不是直接访问IVAR可以减少必须编写的内存管理代码的数量,并提供在必要时使用协作对象替换IVAR的能力。我通常避免同时声明IVAR,因为它们现在是隐式假定的。这让你远离了“合成myVar=\u myVar;”的丑陋
如果过滤器中有类似的内容:
@interface Filter : NSObject {
BOOL _virtualRoomSet;
}
@property (nonatomic, assign) BOOL name;
@property (nonatomic, assign) BOOL virtualRoomSet;
@end
在您的过滤器实现中:
#import "Filter.h"
@implementation NavigationController
@property (nonatomic, retain) Filter *filter;
@end
@implementation NavigationController
@synthesize filter;
//assuming you've properly alloced in init...
-(void) dealloc
{
[filter release];
[super dealloc];
}
- (IBAction)setVirtualShowProperty:(UISwitch*)sender {
self.filter.virtualRoomSet = YES; //better syntax as dealing directly w/ iVars is error prone
}
@end
@implementation Filter
@synthesize filter;
//other code
@end
…它可以解释这个bug,这进一步强调了我上面关于避免声明IVAR的观点!换句话说,请确保您实际设置了要设置的内容。自大约iOS SDK 3.0或更早版本以来,现在自动为所有属性声明隐式iVar。根据经验,在代码中删除IVAR并仅处理属性。在调用navigationController.filter=someFilter;之前,您似乎正在调用setVirtualShowProperty: 将布尔值设置为“是”,但仍然看到它是“否”,这一事实具有误导性——它不是真的“否”,只是您设置它的对象可能为零
当您访问setVirtualShowProperty:时,应该确保navigationController.filter对象确实不是nil。在调用navigationController.filter=someFilter;之前,您似乎正在调用setVirtualShowProperty: 将布尔值设置为“是”,但仍然看到它是“否”,这一事实具有误导性——它不是真的“否”,只是您设置它的对象可能为零
在设置VirtualShowProperty:时,您应该确保navigationController.filter对象确实不为零。virtualRoomSet在哪里定义以及如何定义?名称和/或virtualRoomSet是一个输入错误?您是否初始化了
过滤器?virtualRoomSet在哪里定义以及如何定义?名称和/或virtualRoomSet是一个输入错误?是否初始化了您的过滤器
?