【问题标题】:Cocoa Touch Framework: ConstantsCocoa Touch 框架:常量
【发布时间】:2015-05-27 11:04:18
【问题描述】:

我想用 Swift 编写一个 Cocoa Touch 框架,并想在不同的项目中使用它。只要一切正常。

现在我想用可以在框架外使用的常量来扩展我的框架。我的问题是我不能在我的 Objective-C 项目中使用 Swift-Framework 中的常量。

例如: MyFramework.swift

 public struct Constants{
     public static let constant = "myConstant"
 }

public class MyClass : NSObject {

    public func myFunc(){
     doSomeThing
    }
}

在 Swift 项目中,我可以访问所有内容(MyClass 和常量)。所以这就是我想在 Swift 和 Objective-C 中做的事情:

import MyFramework

class ClassWithImport{
 var xC = Constant.constant
}

但是我怎样才能在 Objective-C 中做到这一点呢?目前我在我的 MyFramework.h 中:

#import "MyFramework-Swift.h"

这在我想使用常量的类中。导入后,您可以看到我可以访问 MyClass,但我无法访问结构常量。

#import <MyFramework.framework/Headers/MyFramework.h>

@implementation ClassWitheImport

- (void)funcOC
{
  MyClass *test;
  test.myFunc;
}
@end

我必须以不同的方式处理常量吗?这样做的想法,我是在看到这个链接后来的:Global constants file in Swift

【问题讨论】:

    标签: objective-c swift cocoa cocoa-touch


    【解决方案1】:

    真正的问题是结构的使用。 struct 是 swift 唯一的功能,与 Objective-C 不兼容。

    可以在post of rintaro 中找到解决方法。

    在他帖子的链接旁边,我也希望您像他一样,提供与该主题相关的Apple documentation 的直接链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-01
      • 2015-08-26
      • 2014-12-14
      • 2019-02-21
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多