在 Swift 中使用 Objective-C 时,Xcode 无法识别 CGFloat / UIFont / CGSize
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25226860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode not recognizing CGFloat / UIFont / CGSize while using Objective-C with Swift
提问by xiaowoo
I created a new Swift project and tried to import my existing Objective C code into the Swift project. Everything is going fine except, Xcode 6 Beta 5 is complaining about CGFloatUIFontCGSize...The error I see is
我创建了一个新的 Swift 项目并尝试将我现有的 Objective C 代码导入到 Swift 项目中。一切都很好,除了 Xcode 6 Beta 5 抱怨CGFloatUIFontCGSize......我看到的错误是
Expect a type
期待一个类型
and
和
Unknown type name 'CGFloat'
未知类型名称“CGFloat”
right next to some of my methods. I thought Swift should be friendly with Objective C and accept all my Objective C code but unfortunately, this is not the case.
就在我的一些方法旁边。我认为 Swift 应该对 Objective C 友好并接受我所有的 Objective C 代码,但不幸的是,事实并非如此。
Any Idea?, suggestions, or comments, I would appreciate it. Thanks.
任何想法?,建议或评论,我将不胜感激。谢谢。
回答by Mohit Tomar
just Add this in which you are facing error
只需添加您面临错误的内容
#import <UIKit/UIKit.h>
回答by Andrey
Swift
迅速
import CoreGraphics
or
或者
import UIKit
If you'd like. (UIKit includes CoreGraphics)
如果你愿意。(UIKit 包括 CoreGraphics)
回答by iHart
If you tried to import existing Objective-C code than first of all you create one Objective-C header file in your Swift project. . .
如果您尝试导入现有的 Objective-C 代码,那么首先您需要在 Swift 项目中创建一个 Objective-C 头文件。. .
after clicking that, editor asked for creating Bridging-Header, you must click yes:
单击后,编辑器要求创建桥接头,您必须单击是:


these creates yourSwiftProjectName-Bridging-Header.hfile . . .
这些创建yourSwiftProjectName-Bridging-Header.h文件。. .
Now, import your existing objective-c file in your project & simply import your header file in Bridging-Header.h file for example,
现在,在您的项目中导入现有的objective-c 文件,只需在 Bridging-Header.h 文件中导入您的头文件,例如,
if your objective-c file is viewController.h & viewController.m than in Bridging-Header.h file write one line code:
如果你的objective-c 文件是viewController.h & viewController.m 而不是Bridging-Header.h 文件写一行代码:
#import "viewController.h"
I think, this code works for you . . .
我认为,这段代码适合你。. .

