1

I created a Swift framework which I want to import in every class I make in my Swift project. I don't want to have to type the import statement manually, and everywhere. How can I do this?

In Objective-C this would be the same as putting one #import statement in the pre-compiled header (.pch file).

What I tried

  • Create a new Xcode project with the Cocoa Touch Framework template, write all my code in Swift.
  • All my extensions and methods in the framework have the public keyword.
  • Added the framework's .xcodeproj to my app's Xcode project.
  • Add the .framework in Target Dependencies.
  • Create a .pch file in my app project (also written in Swift).
  • Wrote <FrameworkName/FrameworkName.h> in the .pch file. This doesn't solve the problem, and kinda doesn't make sense, because I don't really import anything in the FrameworkName.h inside the framework project.
Matthew Quiros
  • 13,385
  • 12
  • 87
  • 132
  • Is it a Swift-only project or mixed ObjC-Swift? – bontoJR Apr 15 '15 at 07:47
  • The framework is Swift-only, the app is also Swift-only. – Matthew Quiros Apr 15 '15 at 07:48
  • 2
    As far as I know, there's no solution at the moment. You can't auto import a framework in all Swift classes, like was possible in ObjC with a PCH file. The only solution I know is to import the framework in the bridge file, then is accessible everywhere, but in a Swift-only project you don't have this solution available. – bontoJR Apr 15 '15 at 07:53

1 Answers1

3

You can add .pch file manually and then you can import your common files in your .pch file. for more info see below link

Add new .pch file

see in this imageenter image description here

Community
  • 1
  • 1
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • I created a .pch from my project, but how do I import my Swift framework from it? – Matthew Quiros Apr 15 '15 at 07:27
  • @Matt Please see the given link who helps you because every step is defined there. – Dharmbir Singh Apr 15 '15 at 07:30
  • I'm afraid you misunderstood. I'm not just asking for how to create a .pch file to pre-import all the files that are within the same project. I'm asking for how to import a Cocoa Touch *Framework* that I built in pure Swift, and how to import that in the .pch so I don't have to import it in every file. Note that if a framework is built using pure Swift, there isn't a single `.h` file for the whole framework. – Matthew Quiros Apr 15 '15 at 07:35
  • @Matt You can #import UIKit and see my edited answer – Dharmbir Singh Apr 15 '15 at 08:13