xcode Swift:尝试导入 UIKit 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29345594/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:53:30  来源:igfitidea点击:

Swift: Error when trying to import UIKit

iosxcodeswiftimportuikit

提问by vilone

I'm getting this really weird error when trying to import UIKit in my swift file.

尝试在我的 swift 文件中导入 UIKit 时,我遇到了这个非常奇怪的错误。

My Code is simply:

我的代码很简单:

import UIKit

class Test: NSObject {

}

The error I get at 'import UIKit' is:

我在“导入 UIKit”中得到的错误是:

  • Unknown type name 'import'
  • Expected ';' after top level declarator
  • 未知类型名称“导入”
  • 预期的 ';' 在顶级声明符之后

I have added UIKit to my Frameworks folder, the class doesn't contain any code (so therefore there shouldn't be anything wrong with it) and I tried to restart both xCode and my Mac, but the error is still there.

我已将 UIKit 添加到我的 Frameworks 文件夹中,该类不包含任何代码(因此它不应该有任何问题)并且我尝试重新启动 xCode 和我的 Mac,但错误仍然存​​在。

I appreciate any help.

我很感激任何帮助。

Thanks.

谢谢。

EDIT:Solved:

编辑:解决:

I tried to import 'Test.swift' in AppDelegate.

我试图在 AppDelegate 中导入“Test.swift”。

回答by zvonicek

This problem usually happens when you try to import ".swift" file in your Objective-C code, like this: #import "HomeViewController.swift". This is wrong and you should import special, automatically generated Swift header, instead:

当您尝试导入您的Objective-C代码“.swift”文件,这样这个问题通常发生的情况:#import "HomeViewController.swift"。这是错误的,您应该导入特殊的、自动生成的 Swift 标头,而不是:

#import "ProductModuleName-Swift.h"

#import "ProductModuleName-Swift.h"

where ProductModuleNameis the name of your module (or project) containing the Swift code.

ProductModuleName包含 Swift 代码的模块(或项目)的名称在哪里。

回答by Evgeniy S

回答by Sukhi

The import stuff in swift is case sensitive. Example :

swift 中的导入内容区分大小写。例子 :

import uikit

will not work. You'll have to type it as

不管用。你必须把它输入为

import UIKit

回答by Amal T S

In my case, it was because somehow the selected type in the file inspector was objective-c sourceinstead of Default - Swift Sourceeven though i was using a .swiftfile.

就我而言,这是因为不知何故,即使我使用的是.swift文件,文件检查器中选定的类型也是Objective-c 源而不是Default - Swift Source

Changing it to Default - Swift Sourcesolved the issue for me.

将其更改为默认值 - Swift Source为我解决了这个问题。

enter image description here

在此处输入图片说明