user4385051
user4385051

Reputation:

using swift and obj c both

I've read this question: How to call Objective-C code from Swift1. But I can found what I need. I'm using swift like first language in my project and later I've add Objective C. But my problem is in Objective c file how I can call a function in swift, now I can do only the contrary (working in swift and using objective c class). When I following code :

#import "ViewController-Swift.h"

xcode issues is that: file not found.

That's my problem now:

@objc class ShareViewController: UIViewController {
    var name: String
    init(name: String) {
        self.name = name
    }

    // Needed to add a class level initializer
    class func newInstanceNamed(name: String) -> ShareViewController {
        return ShareViewController(name: name)
    }

    func abilitaTimer(){
        println("ciao")

        }


    }
}

xcode issues is that: required initializer init coder...

Upvotes: 0

Views: 113

Answers (2)

virus
virus

Reputation: 1243

Swift migrate in Objc-based project:

  • Create new *.swift file (in Xcode) or add it by using Finder
  • Add swift bridging empty header if Xcode have not done this before (see 4 below)
  • Implement your Swift class by using @objc attribute:

    import UIKit @objc class Hello { func sayHello () { println("Hi there!") } }

  • Open Build Settings and check following parameters:
    Product Module Name : myproject
    Defines Module : YES
    Embedded Content Contains Swift : YES
    Install Objective-C Compatibility Header : YES
    Objective-C Bridging Header : $(SRCROOT)/Sources/SwiftBridging.h

  • Import header (which is auto generated by Xcode) in your *.m file. Ex Suppose your project name is myproject then that file will be myproject-Swift.h.

    #import "myproject-Swift.h"

  • Clean and rebuild your Xcode project

Upvotes: 0

vacawama
vacawama

Reputation: 154691

Unless the project you are building is called ViewController, then you have the wrong file name. Look at the bridging header file that was created for you. If it is called xyzzy-Bridging-Header.h, then the file you want to import in Objective-C is xyzzy-Swift.h.

Upvotes: 2

Related Questions