如何使用performSelector进行对象值赋值

时间:2015-11-06 10:01:19

标签: ios objective-c ios9 performselector

我想在iOS 9中使用新的LocationManager属性,我会写:

if ([_manager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]){
        _manager.allowsBackgroundLocationUpdates = YES;
}

但是如何在这里使用performSelector,以便该行在XCode 6和7中编译,因为上面会在XCode 6上给出编译错误,因为allowsBackgroundLocationUpdates在那里不可用,一个选项本来可以使用默认对象setter方法

[_manager performSelector:@selector(setAllowsBackgroundLocationUpdates) withObject:@{1}];

但我在intellisense中看不到选择器:

  

setAllowsBackgroundLocationUpdates

那么如何使用performSelector编写这样的语句?

2 个答案:

答案 0 :(得分:2)

也许你错过了“:”

#define MAX_UTF8_BYTES 6
NSString *utf8String;
NSMutableData *_data = [[NSMutableData alloc] init]; //for easy 'appending' bytes

int bytes_read = 0;
while (!utf8String) {
    if (bytes_read > MAX_UTF8_BYTES) {
        NSLog(@"Can't decode input byte array into UTF8.");
        return;
    }
    else {
        uint8_t byte[1];
        [_inputStream read:byte maxLength:1];
        [_data appendBytes:byte length:1];
        utf8String = [NSString stringWithUTF8String:[_data bytes]];
        bytes_read++;
    }
}

答案 1 :(得分:1)

选择器被称为setAllowsBackgroundLocationUpdates:(注意冒号),所以这应该有效:

if ([_manager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]){
    [_manager setAllowsBackgroundLocationUpdates:YES];
}