idoodler
idoodler

Reputation: 3545

Integrating Swift to Objective-C application

I have an Objective-C application where I like to integrate Swift, but I have an odd issue.

I imported <#ProjectName#>-Swift.h to my implementation file, where I needed the Swift Class. My Swift Class looks like this:

import Foundation

@objc class FileIconColor: NSObject {

    @objc func fileIconColorForSuffix(suffix: NSString) -> UIColor {
        var color:UIColor!
        //Some huge calculations
        return color
    }

    //Just for testing
    @objc func test () {
        println("Test")
    }

}

I am able to call the Class, but I am not able to access the functions in the class; As you can see, the @objc prefixes are there, on both the Class and its functions.

Do you know why this happens? I also build the project before I want to use the function, because the Swift bridging header first needs to be built, am I correct?

Oh and btw, I managed to access Objective-C code in my Swift Class. So there can't be much wrong (I manually created the Objective-C bridging header) :)

Thanks, David

Upvotes: 1

Views: 1354

Answers (1)

Caroline
Caroline

Reputation: 4970

I just tested to see that it works for me in an old Objective C project. I'm on Xcode 6.0.1.

I did this:

  1. Upgraded my old project to Xcode 6, and set Deployment Target to 7.1
  2. Created a new Swift file and said yes to configuring an Objective-C bridging header.
  3. Pasted your code into it. (I also had to import UIKit for UIColor to compile.)
  4. In my main View controller, I put this:

At the top:

#import "PhotoUnzipper-Swift.h"

(PhotoUnzipper's the name of my project)

and in viewDidLoad:

FileIconColor *color = [[FileIconColor alloc]  init];
[color test];

It compiled, and I got "Test" printing out in the console.

Important: I did not import the <#ProjectName#>-Swift.h file. Xcode takes care of that.

The first time I tried, I had problems because I had a deployment target of 4.1. (silly me.) I did not seem to be able to resolve that, but started again, and followed those steps above exactly and it worked.

Upvotes: 2

Related Questions