“Expected ';' after top level declarator” under Swift

狂风中的少年 提交于 2021-01-27 00:02:00

问题


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

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


回答1:


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

  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 @objc statements in your swift code that you want to import to objc

These are the links that were helpful:

  • apple documentation
  • how-to-import-swift-code-to-objective-c



回答2:


You forgot to import 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)

}



回答3:


Try putting

import UIKit

In the top. Think that will solve your problem!




回答4:


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.

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



来源:https://stackoverflow.com/questions/24609255/expected-after-top-level-declarator-under-swift

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