Accessing global const CGFloat defined in an Objective-c .m file from Swift

倾然丶 夕夏残阳落幕 提交于 2019-12-07 02:51:50

问题


I have defined some constants in my .m files that I need to access form my swift code. They are defined:

const CGFloat testValue = 40.0;

and in my other objective-c .m files I can access them by using extern:

extern const CGFloat testValue

Is there an equivalent way of making these constants accessible from the .swift files?


回答1:


Add the extern to your bridging header and Swift should be able to access it.

This simple test worked for me:

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);

Output

40.0



回答2:


Just put the var declaration above the class - it will become a global variable.



来源:https://stackoverflow.com/questions/25689442/accessing-global-const-cgfloat-defined-in-an-objective-c-m-file-from-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!