如何使用performSelector进行对象赋值

How to use performSelector for object value assignment

我想在 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来写这样的语句呢?

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

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

也许你少了“:”

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