我在我的 .m
文件中定义了一些常量,我需要从我的 swift 代码中访问这些常量。它们被定义为:
const CGFloat testValue = 40.0;
在我的其他 objective-c .m
文件中,我可以使用 extern
访问它们:
extern const CGFloat testValue
是否有等效的方法使这些常量可从 .swift 文件访问?
最佳答案
将 extern
添加到您的 bridging header并且 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
关于ios - 从 Swift 访问在 Objective-c .m 文件中定义的全局常量 CGFloat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25689442/