Question or problem with Swift language programming:
I had been messing around with Swift for a while in XCode 6.0 DP to use it in my existing project.
I am trying to access MyModel.h(My existing Objective C Model object) from my ViewController.swift file. I wanted to import
#import “MyModel.h” to my Swift file.
But I could not find how this can be done.
How to solve the problem:
Solution 1:
Posting the answer if it helps some one facing the same issue.
I found that a pretty straight forward solution for How to do this is given in the iOS Developer Library. Please refer to the following link:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_75
Apple Doc says:
To import a set of Objective-C files in the same app target as your
Swift code, you rely on an Objective-C bridging header to expose those
files to Swift. Xcode offers to create this header file when you add a
Swift file to an existing Objective-C app, or an Objective-C file to
an existing Swift app.
So I created MyApp-Bridging-Header.h
file and just added the following line:
#import "MyModel.h"
Now it lets me use the model in my ViewController.swift
as follows:
var myModel = MyModel() myModel.name = "My name" myModel.dobString = "11 March,2013" println ("my model values: Name: \myModel.name and dob: \myModel.dobString")
FYI to anyone who is trying to figure this out. If you have to create the bridging file from scratch, you also have to specify a path to it in Build Settings > Swift Compiler > Objective-C Bridging Header.
Solution 2:
In Document said in to swift programming There are no import statement.
Solution 3:
To import into Swift app or in Objective C app that is creating a mixed-language app you need to create a bridging header you can refer Apple Docs
Its says
Objective-C and Swift files can coexist in a single project, whether the project was originally an Objective-C or Swift project. You can simply add a file of the other language directly to an existing project. This natural workflow makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.
To import a set of Objective-C files in the same app target as your Swift code, you rely on an Objective-C bridging header to expose those files to Swift. Xcode offers to create this header file when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.
EDIT:
I have created a code for your help you can find it here