1
#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   
4

1 回答 1

3

用这个:

//假设你想将 wol 转换为 NSNumber,然后再转换为 NSInteger

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

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

于 2013-04-10T08:27:13.537 回答