文件说:
Global constants defined in C and Objective-C source files are automatically imported by the Swift compiler as Swift global constants.
但是,它并没有说出任何其他方式.我需要定义一个全局快捷常量,并且能够看到它像目标c一样像一个全局c常量.喜欢在swift侧定义:
public let CARDS = ["card1","card2"]
并且看到在客观c上使用它像
NSLog(@"Cards count: %d",[CARDS count])
我该怎么办?我已经导入swift自动生成的标题,如:
#import "MyProject-Swift.h"
并且在Xcode中,如果我命令单击它,它需要我到正确的地方在swift代码,但在编译时我得到:
'User of undeclared Identifier CARDS'
在我的目标c方面.
解决方法
Here is the document关于它
You’ll have access to anything within a class or protocol that’s
marked with the @objc attribute as long as it’s compatible with
Objective-C. This excludes Swift-only features such as those listed
here:
- Generics
- Tuples
- Enumerations defined in Swift
- Structures defined in Swift
- Top-level functions defined in Swift
- Global variables defined in Swift
- Typealiases defined in Swift
- Swift-style variadics
- Nested types
- Curried functions
全局变量(包括常量)从Objective-C无法访问.
相反,您必须声明一个具有全局常量访问器的类.
// Swift public let CARDS = ["card1","card2"] @objc class AppConstant { private init() {} class func cards() -> [String] { return CARDS } } // Objective-C NSArray *cards = [AppConstant cards];
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。