Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Extension in a Framework

Tags:

swift

I'm learning about Swift's extensions and ran into a somewhat strange problem.

When I write an extension on String and compile it into a framework, I am able to import the framework into a different project and use the string extension without any issues. However, when I write an extension on NSDate and try attempt to use it in a different project, the compiler reports "NSDate does not have a member named..."

To be exactly, I created a very simply swift file including these lines of code –

import Foundation
extension NSDate {
    func blah() -> Int {
        return 0
    }
}

I then created a target (Cocoa Framework) and added this file to the compile list. The framework was compiled successfully.

I then created a command line tool and imported this project, while linking against the framework. When I call the function blah() on an NSDate, the compiler complained.

I'm using Xcode beta 3.

like image 208
haginile Avatar asked Jul 08 '14 23:07

haginile


People also ask

What is Swift extension?

A Swift extension allows you to add functionality to a type, a class, a struct, an enum, or a protocol.

What is extension in Swift example?

In Swift, we can add new functionality to existing types. We can achieve this using an extension. Here, we have created an extension of the Temperature class using the extension keyword. Now, inside the extension, we can add new functionality to Temperature .

Can I use Swift extension in Objective-C?

You can write a Swift extension and use it in Objective-C code.

How do I create a Swift file extension?

Creating an extension in Swift When creating an extension, you add the word extension before the name. extension SomeNamedType { // Extending SomeNamedType, and adding new // functionality to it. }


1 Answers

(as outlined in http://colemancda.github.io/programming/2015/02/12/embedded-swift-frameworks-osx-command-line-tools/)

1. Create an Objective-C command line tool and change the Search Paths

Not Swift. You can create a Swift framework for the code you'd put in your command line tool, but the tool itself must not compile any Swift code. Doing so will confuse the linker and make it see duplicate declarations of the Swift library (one in the shipped .dylib, another embedded in the command line tool).

Embedded Frameworks Screen Shot 1

  • Runpath Search Paths:

Debug: $(inherited) @executable_path/../Frameworks @executable_path/$(PRODUCT_NAME).bundle/Contents/Frameworks

Release: $(inherited) @executable_path/../Frameworks

2. Create a bundle and change the Build Settings

Give it the same name as your command line tool, but suffixed with Bundle (e.g. CommandLineToolProductNameBundle). Also make sure its a target in the same project as your command line tool.

Embedded Frameworks Screen Shot 3

  • Target Name: Command Line Tool Product Name + Bundle

  • Product Name: Same as Command Line Tool Product Name

Embedded Frameworks Screen Shot 2

  • Embedded Content Contains Swift Code: YES

3. Add dependencies in Build Phases

Embedded Frameworks Screen Shot 5

  • Target Dependencies: Your command line tool

  • Create a new Copy files phase, set the Destination to Executables and add your command line tool to the list of files to copy.

  • Create a new Copy files phase, set the Destination to Frameworks and add your embedded frameworks to the list of files to copy.

4. Change the Run configuration in the bundle's scheme

You can also optionally hide the scheme of your command line tool since it cannot run standalone.

Embedded Frameworks Screen Shot 4

  • Executable: Your command line tool

  • Debug Executable: YES

like image 65
ColemanCDA Avatar answered Oct 20 '22 05:10

ColemanCDA