【问题标题】:Call Objective C method from Swift从 Swift 调用 Objective C 方法
【发布时间】:2014-06-25 18:51:58
【问题描述】:

我不断收到此错误:

<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)
Command /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254

我正在尝试做的事情: 用脸书登录。我有用于登录的目标 C 代码(来自他们的示例项目),但我使用的是 Swift,因为我不知道目标 C。

如何从 swift 调用目标 C 方法?

这是我的目标 C 代码:

// If the session state is any of the two "open" states when the button is clicked
    if (FBSession.activeSession.state == FBSessionStateOpen
        || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {

        // Close the session and remove the access token from the cache
        // The session state handler (in the app delegate) will be called automatically
        [FBSession.activeSession closeAndClearTokenInformation];

        // If the session state is not any of the two "open" states when the button is clicked
    } else {
        // Open a session showing the user the login UI
        // You must ALWAYS ask for public_profile permissions when opening a session
        [FBSession openActiveSessionWithReadPermissions:@[@"public_profile"]
                                           allowLoginUI:YES
                                      completionHandler:
         ^(FBSession *session, FBSessionState state, NSError *error) {

             // Retrieve the app delegate
             AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
             // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
             [appDelegate sessionStateChanged:session state:state error:error];
         }];
    }

还有我的快速代码:

var bridgeForFacebook: Bridge = Bridge()

@IBAction func fbSubmit(sender: AnyObject) {
    bridgeForFacebook.signIn();
}

【问题讨论】:

    标签: objective-c xcode facebook swift


    【解决方案1】:

    可能是代码文件不能完全看到正确,因此不能很好地播放。

    当您在 Swift 文件中使用 Objective-C 代码时,您需要将 Objective-C 类的标头桥接到 Swift。通常这会在您创建新的 Objective-C 文件时自动完成。

    您需要创建一个名为(your project name)-Bridging-Header.h 的Objective-C 标题文件。在该文件中,编写 #import 语句并导入您希望在 Swift 中访问的任何 Objective-C 类的标题文件。

    现在转到您的项目构建设置并在“Swift 编译器 - 代码生成”部分下找到“Objective-C Bridging Header”字段。在那里,填写新桥接文件的相对路径(应为(project name)/(project name)-Bridging-Header.h)。

    那么你的类和方法调用应该会按预期工作。

    来源:Swift with Cocoa, Swift and Objective-C in the Same Project

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多