从Swift访问在Objective-c .m文件中定义的全局const CGFloat [英] Accessing global const CGFloat defined in an Objective-c .m file from Swift

查看:151
本文介绍了从Swift访问在Objective-c .m文件中定义的全局const CGFloat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的 .m 文件中定义了一些常量,这些常量需要我的快速代码进行访问。它们的定义是:

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;

在我的另一个目标-c .m 文件,我可以使用 extern 来访问它们:

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

extern const CGFloat testValue

是否存在使.swift文件可访问这些常量的等效方法?

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

推荐答案

extern 添加到您的桥接标头和Swift应该可以访问

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

输出

40.0

这篇关于从Swift访问在Objective-c .m文件中定义的全局const CGFloat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆