问题描述
我是一名经验丰富的.NET开发人员,首次进入Objective C编程。我有一个枚举类型的属性的困难。一些上下文...我有一个类头和枚举如下: typedef枚举{
打开,
不可用,
未知
} LocationStatus;
@interface位置:NSObject {
LocationStatus status;
}
@property(nonatomic)LocationStatus状态;
@end
和一个如下所示的实现:
@implementation位置
@synthesize状态;
@end
在代码的某个时候,我正在设置这样的值:
location1.status = Open;
然后,调试器将其评估为具有正确的值,并将其解析为正确的枚举(note还有其他属性未显示在这里...他们也评估正确)。
稍后在代码中,我尝试读这样的属性: p>
LocationStatus status = location.status;
在代码中的这一点上,调试器能够正确地评估我的类的所有属性,除了状态
,它显示一个内存地址,但不是实际值。当执行到达这一行,我一直在控制台中得到一个EXC_BAD_ACCESS错误,并且应用程序崩溃。
我很确定这反映了我的一个根本误会关于如何使用Objective C中的属性和枚举(通常可能是C)。如果有人可以说明这一点,我会非常感激的。
我在你的代码中注意到一件事。您在代码位置1和位置(不含1)中使用2个不同的变量。
EXEC_BAD_ACCESS 通常意味着您尝试向不存在的对象发送消息。通常这是因为它被释放了。但是,在你的情况下,它似乎从来没有存在。
正如你所说,你不分配一个枚举。但它不是这个问题的枚举。 objective-c中的点语法只是发送访问器消息的一个简短的过程。
您的代码相当于:
LocationStatus status = [location状态];
发送合成的 - (LocationStatus)状态{}
消息到不存在的位置对象(除非当然 location1
只是你的帖子中的打字错误,但不在你的代码中,这使我的评论不相关)。所以只需将 location.status
更改为 location1.status
,那么你应该很好(除非当然 location1
正在发送邮件之前发布)。
I'm a veteran .NET developer making my first foray into Objective C programming. I'm having difficulty with a property of an enum type. Some context... I have an class header and enum like this:
typedef enum {
Open,
Unavailable,
Unknown
} LocationStatus;
@interface Location : NSObject {
LocationStatus status;
}
@property (nonatomic) LocationStatus status;
@end
and an implementation that looks like this:
@implementation Location
@synthesize status;
@end
At some point in the code, I'm setting the value like this:
location1.status = Open;
The debugger then evaluates this as having the correct value, and it is resolving to the correct enum (note also that there are other properties not shown here... they too evaluate properly).
Later on in the code, I attempt to read that property like this:
LocationStatus status = location.status;
At this point in the code, the debugger is able to evaluate all the properties of my class correctly, except Status
, which shows a memory address, but not an actual value. When the execution reaches this line, I consistently get a EXC_BAD_ACCESS error in the console, and the app crashes.
I'm pretty sure this reflects a fundamental misunderstanding on my part on how to use properties and enums in Objective C (and probably C in general). If anyone could shed some light on this, I'd be most grateful.
It might be too late to answer this but I did notice one thing in your code. You are using 2 different variables in you code location1 and location (without the 1).
EXEC_BAD_ACCESS generally means that you are trying to send a message to an object that does not exist. Usually this is because it has been deallocated. However, in your case it appears that it never existed in the first place.
As you noted you don't allocate an enum. But its not the enum that is the problem. The "dot" syntax in objective-c is just a short cut for sending an accessor message.
Your code is equivalent to:
LocationStatus status = [location status];
That sends the synthesized -(LocationStatus)status{}
message to the non-existent location object (unless of course location1
was just a typo in your post, but not in your code, which makes my comment irrelevant). So just change location.status
to location1.status
and you should be good to go (unless, of course, location1
is being released before you send the message to it).
这篇关于使用枚举类型作为Objective C中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!