什么是swift中的Objective C代码?

D-A*_* UK 3 xcode objective-c ios swift

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

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Run Code Online (Sandbox Code Playgroud)

谢谢.

Nik*_*ani 6

试试这个 :

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

   }catch{
        print("error")
   }
Run Code Online (Sandbox Code Playgroud)

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

https://objectivec2swift.com