12

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

Damian W
  • 568
  • 2
  • 4
  • 14
  • Is it a iOS Project? So does it use the correct xcrun command? – lukaswelte Jul 07 '14 at 11:29
  • 1
    Yes, universal iOS 8 SDK project - run from the default built settings. – Damian W Jul 07 '14 at 11:34
  • How do you try accessing it? Cause you can check it with a iOS Playground that it is totally valid: class DotColors { let tsblueColor = UIColor(red:58/255.0, green: 125/255.0, blue: 208/255.0, alpha: 1.0) } var color = DotColors() color.tsblueColor – lukaswelte Jul 07 '14 at 11:37
  • Checked in Playground and it seems to bring up the same error. – Damian W Jul 08 '14 at 14:28
  • 1) What line is that error on? 2) Humour me, will you? Copy the code *from here on Stack Overflow* into a new Playground, and see if it suffers from the same problem? I'm wondering if you've got a (virtually-) invisible Xcode placeholder sitting in your source code, or some other character that you can't see. – Matt Gibson Jul 08 '14 at 16:06
  • @MattGibson 1) The error is on line of "import Foundation" 2) Entering this exact code into a brand new Playground delivers the error of "No such module 'UIKit' on the line of "import UIKit" – Damian W Jul 13 '14 at 12:26
  • @Damian That will be because you've created an OS X playground, not an iOS playground. Create the right one, or change the existing playground type using the File Inspector. (UIKit is unavailable in OS X playgrounds, just as Cocoa is unavailable in iOS playgrounds) – Matt Gibson Jul 13 '14 at 12:39
  • @MattGibson Ah whoops, haven't used Playgrounds before. Changed the platform and the code runs without error. Assuming their might be an issue with my iOS project? – Damian W Jul 14 '14 at 13:05
  • I'm thinking that there may just be some dodgy invisible character in your original file, or something like that. Try pasting the working code from the playground into the project that's not working, or deleting and re-typing it a line at a time, or something similar. – Matt Gibson Jul 14 '14 at 13:07
  • #import "ProjectName-Swift.h" to import from SDK. – Naresh Nov 16 '21 at 07:30

4 Answers4

8

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:

Community
  • 1
  • 1
lukas_o
  • 3,776
  • 4
  • 34
  • 50
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)

}
Iducool
  • 3,543
  • 2
  • 24
  • 45
0

Try putting

import UIKit

In the top. Think that will solve your problem!

CodaFi
  • 43,043
  • 8
  • 107
  • 153
Oldmicah
  • 170
  • 1
  • 10
0

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.

adamz
  • 353
  • 3
  • 10