15

How do I import a Swift class from a framework?

  1. I have Xcode Workspace
  2. I create a Cocoa touch framework with using Swift
  3. I create a Swift class named TestClass in that Cocoa touch framework
  4. I create a Tabbed Base iOS application
  5. I import my framework in that tabbed iOS application using import FrameWorkTest

The application project compiler can't find TestClass. How can I import TestClass for use in the main application?

It says that TestClass is not defined. I also want to add an extension in FrameWork an then use it.

https://github.com/ArtemKyslicyn/-SwiftImportFrameworkTest

I'm using this Tutorial http://www.swift-studies.com/blog/2014/6/30/creating-a-pure-swift-framework-for-both-ios-and-mac

I'm on Xcode version 6.0 seed

Thanks

4 Answers 4

39

Make the class in framework public. Also any variables must be public if you want them to use outside the framework.

public class MyClass... {
 ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Very easy to forget this simple step! Wish there was a specific error in XCode that you were attempting to access a class or method or variable that was private...
3

Here's one solution I found:

  1. Create new iOS project
  2. Press on project
  3. Press + to add target
  4. In the resulting window, choose cocoa touch framework
  5. Press on project and press add dependency and add framework
  6. Success!

screenshot illustrating the success

I used the solution here: https://github.com/ArtemKyslicyn/-SwiftImportFrameworkTest in the project named AddingFrameWork

Comments

0

@Artem Kislitsvn: I did see your github repository,

In which you have TestClass.Swift file outside the targets 'test' and 'FrameWorkTest'. Which is out of the scope of the namespaces.

In Swift the namespaces are specified per targets. In your case 'test' and 'FrameWorkTest' are two targets. If you keep your file out of the targets, then your class(.swift file will not visible to at any time).

The solution is very simple. You just move your TestClass.Swift file inside any of the target groups (Drag and drop through Project explorer).

Following this your project works...!

2 Comments

My test class is already in target of FrameWorkTest and common folder is only related of this file i delete this file in sample github project and then you can see i have my test class in namespace of FrameWorkTest but in iOS project i can't used TestClass. Thanks a lot for your answer
Project name and target name are same by default. The target is inside the project name. In your example FrameWorkTest -> FrameWorkTest, test (after the -> symbol the two names denotes targets. The name in front of -> symbol is project name). What I'm saying is you just move your TestClass.Swift file into the targets which are inside the project. It's working for me.
0

To import a Swift class from a framework:

Go to your Project -> Build phases -> Select test target -> Link Binary With Libraries -> + Button -> Add other (on the dialog) -> Select your framework and congrats!

I leave the explanation in images below:

enter image description here

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.