xcode “预期的 ';' 在 Swift 下的顶级声明符之后”

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

"Expected ';' after top level declarator" under Swift

objective-cxcodeswift

提问by Damian Worsdell

I'm trying to set all my colours in one Swift file that can be used throughout my application.

我正在尝试将所有颜色设置在一个 Swift 文件中,该文件可在我的整个应用程序中使用。

The following code below results in...

下面的代码导致...

import Foundation
import UIKit

class DotColors {

let tsblueColor = UIColor(red:58/255.0, green: 125/255.0, blue: 208/255.0, alpha: 1.0)

}

... Expected ';' after top level declarator

...预期 ';' 在顶级声明符之后

回答by lukas_o

The same error occurred to me after I've added the firstswift file to my objc project. That's how I fixed it:

在我将第一个swift 文件添加到我的 objc 项目后,我发生了同样的错误。这就是我修复它的方式:

  1. Make sure you use an "iOS Source" file (not "OS X Source") when you've added the file.
  2. Don't import the swift file in your objc file like: #import "ExampleFile.swift"but use #import "ProjectName-Swift.h"
  3. Make sure you use @objcstatements in your swift code that you want to import to objc
  1. 添加文件后,请确保使用“iOS 源”文件(而不是“OS X 源”)。
  2. 不要在 objc 文件中导入 swift 文件,例如:#import "ExampleFile.swift"但使用#import "ProjectName-Swift.h"
  3. 确保@objc在要导入 objc 的 swift 代码中使用语句


These are the links that were helpful:

这些是有用的链接:

回答by Iducool

You forgot to import UIKit.

你忘记导入 UIKit。

import UIKit

You can try this,

你可以试试这个

import Foundation
import UIKit

class DotColors {

  let tsblueColor = UIColor(red:58/255.0, green: 125/255.0, blue: 208/255.0, alpha: 1.0)

}

回答by Oldmicah

Try putting

尝试放置

import UIKit

In the top. Think that will solve your problem!

前几名。认为这将解决您的问题!

回答by zethraeus

I got this error, persisting even once i made the appropriate code changes, after accidentally selecting an OS X Source Cocoa Class in the new file prompt, instead of an iOS Source Cocoa Touch Class.

在新文件提示中意外选择了 OS X 源 Cocoa 类而不是 iOS 源 Cocoa Touch 类后,我收到此错误,即使我进行了适当的代码更改后仍然存在。

Try creating a new swift file that you're sure is kicked off as iOS.

尝试创建一个新的 swift 文件,您确定它会作为 iOS 启动。