<legend id='ATvFu'><style id='ATvFu'><dir id='ATvFu'><q id='ATvFu'></q></dir></style></legend>
    1. <i id='ATvFu'><tr id='ATvFu'><dt id='ATvFu'><q id='ATvFu'><span id='ATvFu'><b id='ATvFu'><form id='ATvFu'><ins id='ATvFu'></ins><ul id='ATvFu'></ul><sub id='ATvFu'></sub></form><legend id='ATvFu'></legend><bdo id='ATvFu'><pre id='ATvFu'><center id='ATvFu'></center></pre></bdo></b><th id='ATvFu'></th></span></q></dt></tr></i><div id='ATvFu'><tfoot id='ATvFu'></tfoot><dl id='ATvFu'><fieldset id='ATvFu'></fieldset></dl></div>

        <bdo id='ATvFu'></bdo><ul id='ATvFu'></ul>

      <tfoot id='ATvFu'></tfoot>
    2. <small id='ATvFu'></small><noframes id='ATvFu'>

      1. 如何在同一框架内访问 Objective-C 中的内部 Swift 类?

        How to access an internal Swift class in Objective-C within the same framework?(如何在同一框架内访问 Objective-C 中的内部 Swift 类?)
          <legend id='DnzAt'><style id='DnzAt'><dir id='DnzAt'><q id='DnzAt'></q></dir></style></legend>
        • <i id='DnzAt'><tr id='DnzAt'><dt id='DnzAt'><q id='DnzAt'><span id='DnzAt'><b id='DnzAt'><form id='DnzAt'><ins id='DnzAt'></ins><ul id='DnzAt'></ul><sub id='DnzAt'></sub></form><legend id='DnzAt'></legend><bdo id='DnzAt'><pre id='DnzAt'><center id='DnzAt'></center></pre></bdo></b><th id='DnzAt'></th></span></q></dt></tr></i><div id='DnzAt'><tfoot id='DnzAt'></tfoot><dl id='DnzAt'><fieldset id='DnzAt'></fieldset></dl></div>
            <tbody id='DnzAt'></tbody>

          1. <tfoot id='DnzAt'></tfoot>
              <bdo id='DnzAt'></bdo><ul id='DnzAt'></ul>

                <small id='DnzAt'></small><noframes id='DnzAt'>

                  本文介绍了如何在同一框架内访问 Objective-C 中的内部 Swift 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  使用混合框架.在 Obj-C 文件中导入,但内部类不可见,只有公共类.

                  Working on a mixed framework. imported inside the Obj-C file but the internal classes are not visible, only the public ones.

                  文档明确指出 Swift 和 Obj-C 之间应该可以使用内部类:

                  The documentation clearly states the internal clasees should be available between Swift and Obj-C:

                  将 Swift 导入到 Objective-C
                  要将一组 Swift 文件导入到与 Objective-C 代码相同的框架目标中,您不需要需要将任何内容导入框架的伞头文件中.相反,为您的 Swift 代码导入 Xcode 生成的头文件到任何你想从中使用 Swift 代码的 Objective-C .m 文件中.因为为框架目标生成的标头是框架的公共接口,只有标有公共的声明修饰符出现在为框架目标生成的标头中.你仍然可以使用标有来自框架的 Objective-C 部分的内部修饰符,只要它们是在继承自Objective-C 类.有关访问级别修饰符的更多信息,请参阅访问控制在 Swift 编程语言 (Swift 2).

                  代码示例(使用框架创建新项目)

                  Code Sample (Create a new project with a framework)

                  // SwiftObject.swift
                  
                  public class SwiftObject: NSObject {
                      public class func doSomething() {}
                  }
                  
                  internal class YetAnotherSwiftObject: NSObject {
                      internal class func doSomething() {}
                  }
                  

                  <小时>

                  // SomeObject.m file
                  
                  @implementation SomeObject
                  
                  - (void)someMethod {
                      [SwiftObject doSomething];
                  }
                  
                  - (void)someOtherMethod {
                      [YetAnotherSwiftObject doSomething]; // Use of undeclared identifier
                  }
                  
                  @end
                  

                  推荐答案

                  如文档中所示,标有 internal 修饰符的声明不会出现在生成的标头中,因此编译器不知道关于他们,因此投诉.当然,您可以使用 performSelector 方法发送消息,但这不方便且容易出错.我们只需要帮助编译器知道那些声明就在那里.

                  As indicated in the docs, declarations marked with internal modifier don't appear in the generated header, so the compiler does not know about them and thus complaints. Of course, you could send messages using performSelector approach, but that's not convenient and bug-prone. We just need to help the compiler know that those declarations are there.

                  首先,我们需要使用 @objc 属性变体,它允许您在 Objective-C 中为符号指定名称:

                  First, we need to use @objc attribute variant that allows you to specify name for your symbol in Objective-C:

                  // SwiftObject.swift
                  
                  @objc(SWIFTYetAnotherSwiftObject)
                  internal class YetAnotherSwiftObject: NSObject {
                      internal class func doSomething() {}
                  }
                  

                  然后您只需要使用要在代码中使用的方法创建 @interface 声明 - 这样编译器会很高兴,并且还应用 SWIFT_CLASS 宏使用您之前指定的符号名称 - 所以链接器会选择实际的实现:

                  And then you just need to create @interface declaration with the methods you want to use in your code - so the compiler will be happy, and also apply SWIFT_CLASS macro with the symbol name you've specified earlier - so the linker would pick the actual implementation:

                  // SomeObject.m file
                  
                  SWIFT_CLASS("SWIFTYetAnotherSwiftObject")
                  @interface YetAnotherSwiftObject : NSObject
                  
                  + (void)doSomething;
                  
                  @end
                  
                  
                  @implementation SomeObject
                  
                  - (void)someOtherMethod {
                      [YetAnotherSwiftObject doSomething]; // Should work now !!!
                  }
                  
                  @end
                  

                  • 为了清楚起见,我在 .m 文件中使用了接口声明,更好的选择是将此类声明合并到 .h 文件中,并包含它.
                  • 通过在该接口中声明方法,我们向编译器做出了承诺,如果你将一个不存在的方法(或签名错误等)放在那里,它不会抱怨.显然,你会在这种情况下会在运行时崩溃 - 所以要小心.
                  • 这篇关于如何在同一框架内访问 Objective-C 中的内部 Swift 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS 6 rotations: supportedInterfaceOrientations doesn#180;t work?(iOS 6 旋转:supportedInterfaceOrientations 不起作用?)
                  CABasicAnimation rotate returns to original position(CABasicAnimation 旋转返回原始位置)
                  UITabBarController Rotation Issues in ios 6(ios 6 中的 UITabBarController 旋转问题)
                  iOS: How to run a function after Device has Rotated (Swift)(iOS:设备旋转后如何运行函数(Swift))
                  How to rotate an image 90 degrees on iOS?(如何在 iOS 上将图像旋转 90 度?)
                  iOS 8 Rotation Methods Deprecation - Backwards Compatibility(iOS 8 旋转方法弃用 - 向后兼容性)

                  <legend id='KbtPJ'><style id='KbtPJ'><dir id='KbtPJ'><q id='KbtPJ'></q></dir></style></legend>

                  <small id='KbtPJ'></small><noframes id='KbtPJ'>

                    <tbody id='KbtPJ'></tbody>

                  • <i id='KbtPJ'><tr id='KbtPJ'><dt id='KbtPJ'><q id='KbtPJ'><span id='KbtPJ'><b id='KbtPJ'><form id='KbtPJ'><ins id='KbtPJ'></ins><ul id='KbtPJ'></ul><sub id='KbtPJ'></sub></form><legend id='KbtPJ'></legend><bdo id='KbtPJ'><pre id='KbtPJ'><center id='KbtPJ'></center></pre></bdo></b><th id='KbtPJ'></th></span></q></dt></tr></i><div id='KbtPJ'><tfoot id='KbtPJ'></tfoot><dl id='KbtPJ'><fieldset id='KbtPJ'></fieldset></dl></div>

                            <bdo id='KbtPJ'></bdo><ul id='KbtPJ'></ul>

                          • <tfoot id='KbtPJ'></tfoot>