快速的Objective C代码是什么?

时间:2018-08-27 06:47:42

标签: ios objective-c swift xcode

我找到了解决问题的方法,但是代码在Objective C中。请有人能告诉我这是什么吗?

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

谢谢。

3 个答案:

答案 0 :(得分:6)

尝试一下:

do {
       try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
       try AVAudioSession.sharedInstance().setActive(true)
       UIApplication.shared.beginReceivingRemoteControlEvents()

   }catch{
        print("error")
   }

此外,如果您需要将任何代码从 Objective C 转换为 Swift ,则可以使用以下链接进行转换。

https://objectivec2swift.com

答案 1 :(得分:0)

尝试一下:

do{ 
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    try AVAudioSession.sharedInstance().setActive(true)
    UIApplication.shared.beginReceivingRemoteControlEvents()
}catch {
   print("error")
}

此外,您可以通过XCode做到这一点。

  1. 选择要转换的ObjC代码。
  2. 转到Editor > Swiftify > Convert Selection to Swift
  3. 完成

答案 2 :(得分:0)

这是代码的完整转换:

unsigned long long operator"" s(unsigned long long n)
{
    return n + 1;
}
int main()
{
    int x = 16s;
}

转换Objective-C代码的最佳方法是查看最新文档。转到Xcode>窗口>开发人员文档,然后搜索该类(例如,在这种情况下为AVAudioSession)