Cocoa NSThread-获取布尔值
我试图通过NSThread获取Cocoa NSThread-获取布尔值,cocoa,macos,nsthread,Cocoa,Macos,Nsthread,我试图通过NSThread获取-(BOOL)backupDropletUpdateAvailable返回的布尔值 为此,我尝试了以下方法: `BOOL isAvailable=否 [NSThread detachNewThreadSelector:@selector(backupDropletUpdateAvailable) toTarget:isAvailable withObject:nil]; if (isAvailable == YES) {//etc 它返回一个警告,因为BOOL是
-(BOOL)backupDropletUpdateAvailable
返回的布尔值
为此,我尝试了以下方法:
`BOOL isAvailable=否
[NSThread detachNewThreadSelector:@selector(backupDropletUpdateAvailable) toTarget:isAvailable withObject:nil];
if (isAvailable == YES)
{//etc
它返回一个警告,因为BOOL是一个整数,toTarget:
是一个指针。但是我怎样才能得到价值呢?如果我不在一个单独的线程上执行此操作,我的xib将在出现时滞后
谢谢:)线程运行的方法需要写入关心结果的对象可以访问的位置。一种解决方案是让一个方法包装调用,获取结果,然后发布一个通知,将结果包含在用户信息中。然后,关心的对象可以处理通知。请注意,必须在线程启动之前创建对象,否则对象可能会错过通知 解决方案的示意图如下所示:
#define kDropletAvailabilityNotificationName @"com.myapp.notifications.DropletAvailability"
@implementation MyObject
- (void)registerNotifications {
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(dropletAvailabilityNotification:)
name:kDropletAvailaibiltyNotificationName
object:nil];
}
- (void)unregisterNotifications {
[[NSNotificationCenter defaultCenter]
removeObserver:self];
}
- (void)dropletAvailabilityNotification:(NSNotification *)note {
NSNumber *boolNum = [note object];
BOOL isAvailable = [boolNum boolValue];
/* do something with isAvailable */
}
- (id)init {
/* set up… */
[self registerNotifications];
return self;
}
- (void)dealloc {
[self unregisterNotifications];
/* tear down… */
[super dealloc];
}
@end
@implementation CheckerObject
- (rsotine)arositen {
/* MyObject must be created before now! */
[self performSelectorInBackground:@selector(checkDropletAvailability) withObject:nil];
}
- (void)checkDropletAvailability {
id pool = [[NSAutoreleasePool alloc] init];
BOOL isAvailable = [self backupDropletUpdateAvailable];
NSNumber *boolNum = [NSNumber numberWithBool:isAvailable];
[[NSNotificationCenter defaultCenter]
postNotificationName:kDropletAvailaibiltyNotificationName
object:boolNum];
[pool drain];
}
线程运行的方法需要写入关心结果的对象可以访问的位置。一种解决方案是让一个方法包装调用,获取结果,然后发布一个通知,将结果包含在用户信息中。然后,关心的对象可以处理通知。请注意,必须在线程启动之前创建对象,否则对象可能会错过通知 解决方案的示意图如下所示:
#define kDropletAvailabilityNotificationName @"com.myapp.notifications.DropletAvailability"
@implementation MyObject
- (void)registerNotifications {
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(dropletAvailabilityNotification:)
name:kDropletAvailaibiltyNotificationName
object:nil];
}
- (void)unregisterNotifications {
[[NSNotificationCenter defaultCenter]
removeObserver:self];
}
- (void)dropletAvailabilityNotification:(NSNotification *)note {
NSNumber *boolNum = [note object];
BOOL isAvailable = [boolNum boolValue];
/* do something with isAvailable */
}
- (id)init {
/* set up… */
[self registerNotifications];
return self;
}
- (void)dealloc {
[self unregisterNotifications];
/* tear down… */
[super dealloc];
}
@end
@implementation CheckerObject
- (rsotine)arositen {
/* MyObject must be created before now! */
[self performSelectorInBackground:@selector(checkDropletAvailability) withObject:nil];
}
- (void)checkDropletAvailability {
id pool = [[NSAutoreleasePool alloc] init];
BOOL isAvailable = [self backupDropletUpdateAvailable];
NSNumber *boolNum = [NSNumber numberWithBool:isAvailable];
[[NSNotificationCenter defaultCenter]
postNotificationName:kDropletAvailaibiltyNotificationName
object:boolNum];
[pool drain];
}