Write classes in Swift using Objective C and inherit from Objective C

  • 2020-05-07 20:31:18
  • OfStack

Interoperability allows developers to define Swift classes that incorporate features of the Objective-C language. When writing Swift classes, you can not only inherit from the parent classes in Objective-C language, use the Objective-C protocol, but also take advantage of some of the other functions of Objective-C. This means that developers can create Swift classes based on familiar, reliable classes, methods, and frameworks already available in Objective-C, and optimize them in combination with the modern and more efficient language features offered by Swift.

inherits from Objective-C

In Swift, a developer can define a subclass that inherits from a class written using Objective-C. This subclass is created by adding a colon (:) after the Swift class name, followed by the Objective-C class name.


// SWIFT
import UIKit class MySwiftViewController: UIViewController {
    // Define the class
}

Developers can inherit all functionality from the parent of Objective-C. Don't forget to use the override keyword if the developer wants to override methods in the parent class.
Use agreement

In Swift, developers can use the protocols defined in Objective-C. Like Swift protocol 1, all Objective-C protocols are written in a comma-separated list, following the parent name of the class (if it has a parent).


// SWIFT
class MySwiftViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    // Define the class
}

The Objective-C protocol is 1 to Swift protocol. If developers want to reference the UITableViewDelegate protocol in Swift code, they can use UITableViewDelegate directly (id is referenced in Objective-C) < UITableViewDelegate\ > Is equivalent).

writes constructors and destructors

Swift's compiler ensures that at initialization time, the constructor does not allow any uninitialized properties in the class, which increases the security and predictability of the code. Also, unlike the Objective-C language, Swift does not provide a separate memory allocation method for developers to call. When you use the native Swift initialization method (even in collaboration with the Objective-C class), Swift converts the initialization method of Objective-C to the initialization method of Swift. For more information on how to implement a developer custom constructor, see the constructor.

When the developer wishes to perform additional cleanup before the class is released, a destructor procedure is required to replace the dealloc method. Before the instance is released, Swift automatically invokes the destructor to perform the destructor procedure. After Swift has called the destructor of the subclass, it automatically calls the destructor of the parent class. When a developer USES the Objective-C class or Swift class inherited from the Objective-C class, Swift will also automatically call the dealloc method in the parent class for the developer. For more information on how to implement a developer's custom destructor, see the destructor.

integrates Interface Builder

The Swift compiler contains a number of attributes that enable the developer's Swift class to incorporate some of the features of Interface Builder. As in Objective-C, you can use OutLets, actions, and real-time rendering in Swift (live rendering).

USES Outlets and Action

Outlets and Action are used to connect the source code to UI objects of Interface Builder. Using Outlets and Action in Swift requires the @IBOutlet or @IBAction keyword to be inserted before the property and method declarations. Declaring an Outlet collection is also done with the @IBOutlet attribute, which specifies an array for the type.

When the developer declares an Outlet in Swift, the Swift compiler automatically converts the type to a weak (weak), implicit (implicitly), unwrapped (unwrapped) optional (Object-c) data type and assigns it an initialized null value of nil. In fact, the compiler USES @IBOutlet weak var name: Type! = nil instead of @IBOutlet var name: Type. The compiler converts the type to a weak (weak), implicit (implicitly), and unwrapped (unwrapped) optional type, so the developer does not need to assign an initial value to the type in the constructor. After the developer initializes the class object from the storyboard (storyboard) or xib file, the Outlet defined is connected to the objects in 1, so the Outlet is implicit and unwrapped. Since the outlets 1 created is generally weak, the default outlets is weak.

For example, the following Swift code declares a class with Outlet, Outlets collections, and Action:


// SWIFT
class MyViewController: UIViewController {     @IBOutlet var button: UIButton     @IBOutlet var textFields: UITextField[]     @IBAction func buttonTapped(AnyObject) {
        println("button tapped!")
    }
}

In the buttonTapped method, the message sender's information is not used, so the method's parameter name can be omitted.
Real-time rendering (live rendering)

Developers can use @IBDesignable and @IBInspectable in Interface Builder to create lively, interactive custom views (view). When a developer inherits an UIView or NSView custom view (view), he can add the @IBDesignable attribute before the class declaration. When you add a custom view to Interface Builder (set in the custom view class in the monitor panel), Interface Builder will render your custom view on the canvas.

Note: only objects in the framework can be rendered in real time

You can also add the @IBInspectable attribute to a type attribute that is compatible with the user-defined runtime attribute. This way, when a developer adds a custom view to Interface Builder, he can edit these properties in the monitor panel.


// SWIFT @IBDesignable class MyCustomView: UIView {
    @IBInspectable var textColor: UIColor
    @IBInspectable var iconHeight: CGFloat
    /* ... */
}

specifies the property property

In Objective-C, an attribute usually has a set of attributes (Attributes) that indicate some additional information about the attribute. In Swift, developers can specify these properties of properties in different ways.

strong and weak types

Properties in Swift are strongly typed by default. Using the weak keyword to modify a property indicates that the object is stored as a weak reference. This keyword can only modify the optional object type. For more information, see features.

read/write and read only

In Swift, there are no readwrite and readonly features. When a stored property is declared, let is used to make it read-only. Make it readable/write with var. When declaring a computed property, provide an getter method to make it read-only. Provide the getter method and setter method to make it readable/write. For more information, see properties.

Copy

In Swift, the copy property of Objective-C is converted to the @NSCopying property. The properties of this 1 class must comply with the NSCopying protocol. For more information, see features.

implements Core Data Managed Object subclass

Core Data provides basic storage and implements a set of attributes for the NSManagedObject subclass. In the Core Data model, the @NSmanaged feature is added before each property or property of the relationship associated with the managed object subclass is defined. Similar to the @dynamic feature in Objective-C, the @NSManaged feature tells the Swift compiler that the storage and implementation of this property will be completed at runtime. However, unlike @dynamic, the @NSManaged feature is only available in Core Data support.


Related articles: