Can't access My Swift classes into Objective-C

2019-07-15 06:30发布

问题:

I have created an application in Objective-C. I wants to use My Classes written in Swift into application for this i have done following things

  • set Defines Module to YES.
  • Defined Product Module Name

I created a swift file something like this MyClass.swift

import Foundation
class MyClass: NSObject
{
    override init() {
        super.init()
        print("SwiftClass init")
    }
}


@objc class SwiftClass : NSObject {

    override init() {
        super.init()
        print("SwiftClass init")
    }

    func sayHello() -> Void {
        print("hello");
    }

    func addX(x:Int, andY y:Int) -> Int {
        return x+y
    }

    // Make a dictionary
    // No, this code doesn't protect against values.count > keys.count
    func dictionaryWithKeys(keys:[String], andValues values:[String]) -> Dictionary<String,String> {

        var dictionary = Dictionary<String,String>()

        for var i = 0; i < keys.count; i++ {
            dictionary[keys[i]] = values[i]
        }

        return dictionary

    }

}

after successfully Clean & Build i have swift header file in my application named Testing-Swift.h as shown in pic -

I am able to import my header file in my ViewController.m but not able to access the classes written in swift -

Please help me .I am using Xcode 7.2

回答1:

Click on your Xcode Project file,click on Build Setting,find a search bar and Search "Defines module" and tick out "Yes". After search "PRODUCT MODULE,write Your Project name,rewrite in Your App delegates #Import "YourProjectName-swift.h"