gpt4 book ai didi

ios - `@objc(ViewController) ` 是做什么的?

转载 作者:行者123 更新时间:2023-11-30 13:43:40 25 4
gpt4 key购买 nike

我有这个快速代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateInitialViewController()!
if (controller is InlineMainViewController ){
mainViewController = controller as! InlineMainViewController
}
window?.rootViewController = controller
window?.makeKeyAndVisible()
}

当我的 viewController 具有此属性时:

@objc(ViewController)  // match the ObjC symbol name inside Storyboard
public class InlineMainViewController: UIViewController,

( Controller 是 InlineMainViewController ) 返回 false

当它没有此属性时:

public class InlineMainViewController: UIViewController, 

( Controller 是 InlineMainViewController ) 返回 true

我的Main.sotryboard已连接到InlineMainViewController

我从 Google Analytics github 示例中获取了 @objc(ViewController)

最佳答案

来自Swift Type Compatibility

You can use the @objc attribute if your Swift class doesn’t inherit from an Objective-C class, or if you want to change the name of a symbol in your interface as it’s exposed to Objective-C code.

这段代码:

@objc(ViewController)

当通过生成的 header 将其导入到 Objective-C 代码中时,将导致此属性后面的类具有括号中的名称,如 Importing Swift into Objective-C 中所述。

To import Swift code into Objective-C from the same target

  • Import the Swift code from that target into any Objective-C .m file within that target using this syntax and substituting the appropriate name:

    #import "ProductModuleName-Swift.h"

关于ios - `@objc(ViewController) ` 是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35243508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com