1

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

Andriko13
  • 992
  • 1
  • 7
  • 34
David Gölzhäuser
  • 3,525
  • 8
  • 50
  • 98
  • Suggestions here: http://stackoverflow.com/questions/24206732/cant-use-swift-classes-inside-objective-c?rq=1 – Caroline Sep 20 '14 at 22:57
  • @Caroline I didn't create the `<#ProjectName#>-Swift.h` myself. I know that It is been created by Xcode. I imported the `<#ProjectName#>-Swift.h` file and of corse replaced `<#ProjectName#>` with the name of my Project. I really don't know what I am doing wrong. Do you have any other suggestion? – David Gölzhäuser Sep 20 '14 at 23:07
  • How are you creating an instance of the class and calling the functions from ObjC? What isn't working. You can't compile? Nothing happens when you run? – vacawama Sep 21 '14 at 01:39

1 Answers1

2

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.

Caroline
  • 4,875
  • 2
  • 31
  • 47