How To Call Objective C code in Swift?

 A new programming language Swift which is Compatible with also objective C, So that objective c file code is use in  Swift file, But how can use this code in swift file. 
In the swift project you can directly added Objective C file or create an objective C file. but without Bridge in project you can’t use the objective C file in swift.
Follow the steps to Bridge Objective C to Swift file.


Step-1 : Create new Xcode project in Swift language,
Choose File > New > Project > DemoBridge 

Step-2: In swift project adding new objective C file Name as : MyObjectiveFile,
Step-3 : When you adding new objective C file in Swift project at first time, then there is display one objective C bridge header popup as below,  Then select on Create Bridging Header option of popup.
Step 4 : When you Select the option of Create Bridging Header, one file is automatically generated with name :  DemoBridge-Bridging-Header.h

But if you are directly added objective C file or Drag objective C file then above file doen't generated in your project, So you have to generate bridge file name as : "YourProjectName-Bridging-Header.h"

Step 5 : Now you set Bridge file path in bridge header, So click on project target > Build Settings > Search Objective C bridging header in it. see below gif image and follow step and set path of bridging header.



Drag "YourProjectName-Bridging-Header.h" file and added in Box then it takes path of bridging file.

Step 6 : Now "YourProjectName-Bridging-Header.h"open file and Import the objective file which you want to use in your swift file, In this project MyObjectiveFile.h which want to use in Swift project, then import in bridge file,

#import "MyObjectiveFile.h"

See :


Step 7 :  Now in your swift file, you can use the class of objective C as bellow.
override func viewDidLoad() {
    let myObject = MyObjectiveFile()
    print(myObject.Name())
}
You can get result of Print.
Error : if you got the error that Bridging Header doen't exist,

Step 8 : Remove path from bridging header box as below, Remove selected path, and add again show as below,

See Demo project : Bridging-Demo
Thanks,

According to normal string in ios, There is not provide the facility of colorful string in ios, but have a class NSAttributeString which have provide color of string facility. For formatting a part of a string, you need an attributed string which specifies parts of the string formatted in different way - See more at: http://iosdevcenters.blogspot.in/2015/12/how-to-set-use-multiple-font-colors-in.html#sthash.ktuoVmBH.dpuf
According to normal string in ios, There is not provide the facility of colorful string in ios, but have a class NSAttributeString which have provide color of string facility. For formatting a part of a string, you need an attributed string which specifies parts of the string formatted in different way - See more at: http://iosdevcenters.blogspot.in/2015/12/how-to-set-use-multiple-font-colors-in.html#sthash.ktuoVmBH.dpuf
According to normal string in ios, There is not provide the facility of colorful string in ios, but have a class NSAttributeString which have provide color of string facility. For formatting a part of a string, you need an attributed string which specifies parts of the string formatted in different way - See more at: http://iosdevcenters.blogspot.in/2015/12/how-to-set-use-multiple-font-colors-in.html#sthash.ktuoVmBH.dpuf
According to normal string in ios, There is not provide the facility of colorful string in ios, but have a class NSAttributeString which have provide color of string facility. For formatting a part of a string, you need an attributed string which specifies parts of the string formatted in different way - See more at: http://iosdevcenters.blogspot.in/2015/12/how-to-set-use-multiple-font-colors-in.html#sthash.ktuoVmBH.dpuf
According to normal string in ios, There is not provide the facility of colorful string in ios, but have a class NSAttributeString which have provide color of string facility. For formatting a part of a string, you need an attributed string which specifies parts of the string formatted in different way - See more at: http://iosdevcenters.blogspot.in/2015/12/how-to-set-use-multiple-font-colors-in.html#sthash.ktuoVmBH.dpuf
According to normal string in ios, There is not provide the facility of colorful string in ios, but have a class NSAttributeString which have provide color of string facility. For formatting a part of a string, you need an attributed string which specifies parts of the string formatted in different way - See more at: http://iosdevcenters.blogspot.in/2015/12/how-to-set-use-multiple-font-colors-in.html#sthash.ktuoVmBH.dpuf
According to normal string in ios, There is not provide the facility of colorful string in ios, but have a class NSAttributeString which have provide color of string facility. For formatting a part of a string, you need an attributed string which specifies parts of the string formatted in different ways. - See more at: http://iosdevcenters.blogspot.in/2015/12/how-to-set-use-multiple-font-colors-in.html#sthash.ktuoVmBH.dpuf
According to normal string in ios, There is not provide the facility of colorful string in ios, but have a class NSAttributeString which have provide color of string facility. For formatting a part of a string, you need an attributed string which specifies parts of the string formatted in different ways. - See more at: http://iosdevcenters.blogspot.in/2015/12/how-to-set-use-multiple-font-colors-in.html#sthash.ktuoVmBH.dpuf
According to normal string in ios, There is not provide the facility of colorful string in ios, but have a class NSAttributeString which have provide color of string facility. For formatting a part of a string, you ne - See more at: http://iosdevcenters.blogspot.in/2015/12/how-to-set-use-multiple-font-colors-in.html#sthash.ktuoVmBH.dpuf
According to normal string in ios, There is not provide the facility of colorful string in ios, but have a class NSAttributeString which have provide color of string facility. For formatting a part of a string, you ne - See more at: http://iosdevcenters.blogspot.in/2015/12/how-to-set-use-multiple-font-colors-in.html#sthash.ktuoVmBH.dpuf
According to normal string in ios, There is not provide the facility of colorful string in ios, but have a class NSAttributeString which have provide color of string facility. For formatting a part of a string, you ne - See more at: http://iosdevcenters.blogspot.in/2015/12/how-to-set-use-multiple-font-colors-in.html#sthash.ktuoVmBH.dpuf
How To Call Objective C code in Swift? How To Call Objective C code in Swift? Reviewed by KIRIT MODI on 23:03:00 Rating: 5

No comments:

Powered by Blogger.