当我在https://github.com/enormego/EGOTableViewPullRefresh玩游戏并弄清楚它是如何工作的时候,我发现了@property和@synthesize的神秘感.这是我提到的代码
@interface EGORefreshTableHeaderView : UIView {
id _delegate;
EGOPullRefreshState _state;
UILabel *_lastUpdatedLabel;
UILabel *_statusLabel;
CALayer *_arrowImage;
UIActivityIndicatorView *_activityView;
}
@property(nonatomic,assign) id <EGORefreshTableHeaderDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)
@synthesize delegate=_delegate;
Run Code Online (Sandbox Code Playgroud)
我已经阅读了这个http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html,据我所知,它为_delegate创建了一个新的名称,即委托.(这是我的理解吗?)
但我仍然不明白为什么他们必须使那些@synthesize =指令复杂化.
有时我们可以在synthesize语句中明确指定实例变量的名称,例如,
在SomeViewController.h,
//....
@property (nonatomic, retain) NSObject *variable;
//....
Run Code Online (Sandbox Code Playgroud)
在SomeViewController.m,
//....
@synthesize variable = _variable;
//....
Run Code Online (Sandbox Code Playgroud)
但是,如果将实例变量隐式命名为_variable即使我们简单地将它作为:
@synthesize variable;
Run Code Online (Sandbox Code Playgroud)
在SomeViewController.m.
任何人都可以分享为什么有必要吗?谢谢你:D
我有这个非常奇怪的问题,我是Objective-c的新手,它可能来自我还不理解的深度.
所以,在我的头文件中,我声明了变量
NSString *curTitle;
Run Code Online (Sandbox Code Playgroud)
然后在.m文件中我合成它:
curTitle = [[NSString alloc] init];
Run Code Online (Sandbox Code Playgroud)
在其他方法之后我分配它:
curTitle = string; // string is an instance of NSString
Run Code Online (Sandbox Code Playgroud)
在我试图分配的那天结束时
slide.title = curTitle; //slide is a managed object (CoreData)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'属性的值不可接受的类型:property ="title"; 期望的类型= NSString; 给定type = __NSCFDictionary; value = {}.'
有趣的事实,在iphone SDK 3.2中它工作,但在我安装SDK 4后,我有这个错误
另一个有趣的事实是,如果我的类的curTitle属性(使用@property和@synthesize)它也有效
有任何想法吗?谢谢