Reputation: 2684
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.
Upvotes: 74
Views: 80582
Reputation: 2684
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:
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.
Upvotes: 93
Reputation: 1174
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
Upvotes: 6
Reputation: 47059
In Document said in to swift programming There are no import statement.
Upvotes: 17