Swift: Error when trying to import UIKit

青春壹個敷衍的年華 提交于 2019-12-12 10:49:23

问题


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

My Code is simply:

import UIKit

class Test: NSObject {

}

The error I get at 'import UIKit' is:

  • 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.

I appreciate any help.

Thanks.

EDIT: Solved:

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


回答1:


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:

#import "ProductModuleName-Swift.h"

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




回答2:


Found good Trouble shooting guide - there is great Troubleshooting Tips and Reminders section!

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_87




回答3:


The import stuff in swift is case sensitive. Example :

import uikit

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

import UIKit



回答4:


In my case, it was because somehow the selected type in the file inspector was objective-c source instead of Default - Swift Source even though i was using a .swift file.

Changing it to Default - Swift Source solved the issue for me.



来源:https://stackoverflow.com/questions/29345594/swift-error-when-trying-to-import-uikit

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