我在我的.m
文件中定义了一些常量,我需要从我的 swift 代码中访问这些常量。它们被定义为:
const CGFloat testValue = 40.0;
在我的其他objective-c.m
文件中,我可以使用以下方法访问它们extern
:
extern const CGFloat testValue
有没有一种等效的方法可以从 .swift 文件中访问这些常量?
添加extern
到您的桥接头中,Swift 应该能够访问它。
这个简单的测试对我有用:
ObjCTest.m
#import <Foundation/Foundation.h>
const CGFloat testValue = 40.0;
ObjCSwiftBridgeTest-Bridging-Header.h
#import <Foundation/Foundation.h>
extern const CGFloat testValue;
main.swift
println(testValue);
输出
40.0
只需将var
声明放在类之上——它将成为一个全局变量。