6

我在我的.m文件中定义了一些常量,我需要从我的 swift 代码中访问这些常量。它们被定义为:

const CGFloat testValue = 40.0;

在我的其他objective-c.m文件中,我可以使用以下方法访问它们extern

extern const CGFloat testValue

有没有一种等效的方法可以从 .swift 文件中访问这些常量?

4

2 回答 2

15

添加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
于 2014-09-05T15:55:44.367 回答
0

只需将var声明放在类之上——它将成为一个全局变量。

于 2014-09-05T15:49:11.803 回答