Objective-C、枚举器和自定义设置器 - 如何使其工作?
我有一个带有枚举的应用程序委托类,如下所示:
typedef enum {
Online = 3500,
DoNotDisturb = 9500,
Offline = 18500,
Away = 15500,
Busy = 6500,
BeRightBack = 12500
} status;
另外,我有一个属性可以在接口文件中设置枚举器的值:
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
status userStatus;
}
@property (nonatomic, setter=setStatus) status userStatus;
@end
最后,我的实现文件中有以下消息:
@implementation Communicator2AppDelegate
- (void)setStatus:(status)_userStatus {
if ([NSThread isMainThread]) {
// some stuff happens here ...
} else {
[self performSelectorOnMainThread:@selector(setStatus:) withObject:_userStatus waitUntilDone:NO];
}
}
我的问题如下:performSelectorOnMainThread消息不起作用,因为它不接受“_userStatus”作为值。我的猜测是该消息假设它是一个枚举,而不是一个真正的值。我在编译时收到以下错误消息:“‘performSelectorOnMainThread:withObject:waitUntilDone’的参数 2 的类型不兼容。”
有谁知道如何进行这项工作吗?
I have an Application Delegate class with a enumeration which looks like this:
typedef enum {
Online = 3500,
DoNotDisturb = 9500,
Offline = 18500,
Away = 15500,
Busy = 6500,
BeRightBack = 12500
} status;
Additionally I have a property to set a value from the enumerator in my interface file:
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
status userStatus;
}
@property (nonatomic, setter=setStatus) status userStatus;
@end
Finally I have the following message in my implementation file:
@implementation Communicator2AppDelegate
- (void)setStatus:(status)_userStatus {
if ([NSThread isMainThread]) {
// some stuff happens here ...
} else {
[self performSelectorOnMainThread:@selector(setStatus:) withObject:_userStatus waitUntilDone:NO];
}
}
My issue is the following: the performSelectorOnMainThread message isn't working because it doesn't accept '_userStatus' as a value. My guess is the message assumes it's an enum, not a real value. I get the following error message upon compilation: "Incompatible type for argument 2 of 'performSelectorOnMainThread:withObject:waitUntilDone.'"
Does anyone have any idea on how to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将一个对象值传递给此方法,并且枚举(即 int)是标量值。要实现您所需要的,您必须将整数包装到 obj-c 对象(例如 NSNumber):
You need to pass an object value to this method and enum (that is int) is scalar value. To achieve what you need you must wrap your integer to obj-c object (e.g. NSNumber):