0

I've started coding in Swift with a game project using SpirteKit

I want to import SpriteKit in a file like a .pch file, so all the other files in the project can use and no need to call

import SKSpriteKit

again. But Swift seems not support .pch file

Is there any alternative way to achieve it

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Scofield Tran
  • 828
  • 1
  • 18
  • 30
  • Found my dupe: [Is there a Prefix Header (or something with this functionality) in Swift?](http://stackoverflow.com/questions/28694537/is-there-a-prefix-header-or-something-with-this-functionality-in-swift) – rickster Dec 07 '15 at 20:23

1 Answers1

1

I know I've answered this before, but digging out duplicates is cumbersome on mobile, so I'll just answer it again for now...

Unlike with (Obj)C (specifically, C before modules were introduced in Xcode 4), there is no cost to having the same import statement at the top of multiple files in a project. If there were a way to say "import SpriteKit for all files in my project", it wouldn't save on compilation time the way a pch file can for C.

On the other hand, even though you're typing the same import statement in each file, there's a benefit to that in terms of clarity. It lets you choose not to import the same modules in every file (which can be important for optimization and controlling which overloaded/generic functions get used), and it makes clear to readers of your code what its dependencies are. (Because it sucks to copy/paste code from some file and not know what modules you need to make it compile again in its new location.)

rickster
  • 124,678
  • 26
  • 272
  • 326
  • 1
    Yes, you're right about the attention and clarification. But in the large project, for example about UIKit and UIFoundation, every files and classes need them, I think it's really terrible if I have to type them on each file or even copy paste. Again in a big project, not just only these framework are imported, I suppose Apple did a horrible thing when they remove the use of pch file. Anyway, thank @rickster – Scofield Tran Dec 06 '15 at 17:48