what is -objc in swift

Objective-C is an object-oriented programming language that has been widely used for developing applications on Apple's platforms, such as iOS and macOS. Swift, on the other hand, is a new programming language developed by Apple that is designed to work alongside Objective-C in mixed-language projects.

In order to use Objective-C code in a Swift application, you need to create a bridging header that allows the two languages to communicate with each other. The bridging header file is a special file in your project that imports Objective-C headers into Swift.

Here are the steps to create a bridging header file:

  1. Add a new file to your project, and select the "Header File" template.
  2. Name the file "[YourProjectName]-Bridging-Header.h".
  3. In the build settings for your project, search for "Objective-C Bridging Header".
  4. Set the value of this setting to the path of your bridging header file.
  5. In your bridging header file, import any Objective-C headers that you want to use in Swift.

Once you've created the bridging header file, you can use Objective-C code in your Swift project by importing the required headers in your Swift files. For example:

main.swift
import UIKit

class MyViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Objective-C code
        let myObject = MyObjectiveCClass()
        myObject.doSomething()
    }
}
243 chars
13 lines

In this example, the MyObjectiveCClass is an Objective-C class that has been imported into the Swift file using the bridging header file.

gistlibby LogSnag