2015-11-06 54 views
1

我想使用的iOS 9一个新的LocationManager财产,我会写:如何使用performSelector为对象赋值

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

但如何在这里使用performSelector,使该行​​的两个XCode中编译6和7,因为上面会给编译错误上的XCode 6为allowsBackgroundLocationUpdates不可有,一个选项可以一直使用默认的对象setter方法

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

,但我看不出有什么选择在智能感知:

setAllowsBackgroundLocationUpdates

所以如何编写这样使用performSelector声明?

+0

不能使用'performSelector:withObject:'用这个方法因为它的参数不是指向对象类型的指针。 – newacct

回答

2

也许你缺少 “:”

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

的选择被称为setAllowsBackgroundLocationUpdates:(注意冒号),所以这应该工作:

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