将CFNumberRef转换为NSInteger

时间:2013-04-10 08:24:41

标签: objective-c nsinteger cfnumberref

#import "someClass.h"

@implementation someClass

- (NSInteger *)checkWakeOnLan {
    SCDynamicStoreRef ds = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("myapp"), NULL, NULL);
    CFDictionaryRef dr = SCDynamicStoreCopyValue(ds, CFSTR("State:/IOKit/PowerManagement/CurrentSettings"));
    CFNumberRef wol=CFDictionaryGetValue(dr, CFSTR("Wake On LAN"));
    CFRelease(dr);
    CFRelease(ds);

这里是我的问题,如何将CFNumberRef转换为NSInteger, 我一次又一次地尝试,但随时都有 "在没有强制转换的情况下从指针生成整数"

    NSInteger *value = [... ?];
    return value;
}

- (IBAction)doStuff:(NSButton *)sender {
    [myBevelButton setState:[self checkWakeOnLan]]; //setState takes NSInteger
    //myBevelButton defined elsewhere, shows different icons 
}

@end   

1 个答案:

答案 0 :(得分:3)

使用此:

//假设您要将wol转换为NSNumber,然后转换为NSInteger

NSNumber *nsnumber = (NSNumber*)wol;
NSInteger value = [nsnumber integerValue];

* 注意:您很少需要NSInteger指针。在您的情况下,您可以很容易地使用NSInteger。您还需要将方法更改为- (NSInteger)checkWakeOnLan