【发布时间】:2012-11-01 17:18:22
【问题描述】:
我是 Objective-c 的新手,但已经编码多年了。不知何故,我对 Objective-C 不感冒。我搜索了谷歌和stackoverflow,但我认为我的问题只是简单而愚蠢,没有人问过这个问题。
我的代码基于 DateSelectionTitles 示例。 http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html
我有一个 NSManagedObject
@interface Event : NSManagedObject
@property (nonatomic, retain) NSDate * date;
...
// Cache
@property (nonatomic, retain) NSString * primitiveSectionIdentifier;
所有属性都在我的数据模型中定义,除了primitiveSectionIdentifier(如苹果示例)
但是当我打电话时
NSString *tmp = [self primitiveSectionIdentifier];
我得到了异常
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Event originalSectionIdentifier]: unrecognized selector sent to instance 0x74850c0”
简单来说:
Event *foo = [[Event alloc] init];
if (foo.primitiveSectionIdentifier) {
NSLog(@"YEAH");
}
抛出相同的异常。所以我基本上想检查primitiveSectionIdentifier是否为零。但是当我访问该属性时,它会引发异常吗?我是否需要先分配每个属性才能检查它是否有值?!
我没有了解哪些 Objective-C 基础知识?
非常感谢您的回复!
【问题讨论】:
-
你在
@implementation部分写过@synthesize primitiveSectionIdentifier;吗? -
你的代码编译时没有警告吗?
-
如果您使用的是 Xcode 4.5 编译器不会给出未合成的警告(@synthesize 属性)。尝试改用 self.primitiveSectionIdentifier。 Xcode 4.5 发行说明中提到developer.apple.com/library/mac/#releasenotes/DeveloperTools/…
标签: iphone objective-c ios