Interoperability (interoperability) allows developers to define Swift classes that incorporate objective-C features. When writing Swift classes, you can not only inherit the parent class written in Objective-C language, use objective-C protocol, but also take advantage of some other features of Objective-C. This means that developers can build Swift classes based on familiar, reliable classes, methods, and frameworks that already exist in Objective-C and optimize them with the modern and more efficient language features that Swift provides.

Classes that inherit objective-C

In Swift, developers can define a subclass that inherits from a class written in Objective-C. This subclass is created by following the Swift class name with a colon (:) followed by the objective-c class name.

// SWIFT 2. import UIKit 4. class MySwiftViewController: UIViewController {5.Copy the code

Developers can inherit all functionality from objective-C’s parent classes. Don’t forget to use the Override keyword if a developer wants to override a method in a parent class.

Use agreement

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

1. // SWIFT 2. class MySwiftViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {3.Copy the code

The Objective-C protocol is consistent with the Swift protocol. If a developer wants to reference the UITableViewDelegate protocol in Swift code, he can use the UITableViewDelegate protocol directly (the equivalent of referring to id<UITableViewDelegate> in Objective-C).

Write the constructor and destructor

As a developer, it is particularly important to have a learning atmosphere and a communication circle. This is my iOS development public number: Programming Daxin, whether you are small white or big ox are welcome to enter. Let us progress together and develop together! (The group will provide some free study books collected by the group owner and hundreds of interview questions and answers documents for free!)

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

When a developer wants to perform additional cleanup before a class is released, a destructor procedure is performed instead of the dealloc method. Swift automatically invokes the destructor to perform the destructor process before the instance is released. After Swift calls the subclass’s destructor, it automatically calls the parent class’s destructor. When a developer uses an Objective-C class or a Swift class that inherits from objective-C, Swift also automatically calls the dealloc method in that class’s parent class for the developer. For more information on how to implement a developer custom destructor, see destructor

Integrated Interface Builder

The Swift compiler contains properties that enable developers to integrate some of the features in Interface Builder into their Swift classes. Just like in Objective-C, you can use OutLets, actions, and live rendering in Swift.

Use Outlets and Action

Outlets and Actions are used to connect source code to Interface Builder UI objects. To use Outlets and Actions in Swift, you need to insert the @ibOutlet or @ibAction keyword before property and method declarations. Declaring an Outlet collection again uses the @ibOutlet property, which specifies an array for the type.

When developers declare an Outlet in Swift, the Swift compiler automatically switches the type to weak, implicit, unwrapped optional data types. And assign it an initialized nil value. In fact, the compiler uses @ibOutlet weak var name: Type! = nil instead of @ibOutlet var name: Type. The compiler converts the type to weak, implicit, and unwrapped optional types, so developers don’t need to assign an initial value to the type in the constructor. When a developer initializes an object class from a storyboard or xiB file, the defined outlets are attached to those objects, so they are implicit and unwrapped. Because outlets created are generally weak, the default is weak.

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

1. // SWIFT 2. class MyViewController: UIViewController { 4. @IBOutlet var button: UIButton 6. @IBOutlet var textFields: UITextField[] 8. @IBAction func buttonTapped(AnyObject) { 9. println("button tapped!" ) 10.} 11.Copy the code

In the buttonTapped method, the sender’s information is not used, so you can omit the method’s parameter name.

Live Rendering

Developers can use @ibDesignable and @ibinspecTable in Interface Builder to create lively, interactive custom views. When developers customize a view by inheriting UIView or NSView, they can add the @ibDesignable property before the class declaration. After you add a custom view to Interface Builder (set in the custom View class of the monitor panel), Interface Builder will render your custom view on the canvas.

Note: Only render in real time for objects in the frame

You can also add the @ibInspectable property to type properties compatible with user-defined runtime properties. This allows developers to edit the properties in the monitor panel after adding custom views to the Interface Builder.

1.  // SWIFT

3.  @IBDesignable

5.  class MyCustomView: UIView {
6.  @IBInspectable var textColor: UIColor
7.  @IBInspectable var iconHeight: CGFloat
8.  /* ... */
9.  }
Copy the code

Specified properties

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

Strong and weak types

Properties in Swift are strongly typed by default. The weak keyword modifies a property to indicate that it is a weak reference for object storage. This keyword can only modify optional object types. For more information, see [Features]

Read/write and read-only

In Swift, there are no readwrite and readonly features. When declaring a stored property, use let to make it read-only. Use var to make it readable/write. When a computational property is declared, provide a getter method for it to make it read-only. Provide getter and setter methods to make them readable/write. For more information, see [Properties]

copy

In Swift, objective-C’s copy property is translated to the @nscopying property. Properties of this class must comply with the NSCopying protocol. For more information, see [Features]

Implement Core Data Managed Object subclasses

Core Data provides basic storage and a set of properties that implement nS-managed object subclasses. In the Core Data model, add the @NSManaged feature before defining each property or relationship related to a managed object subclass. Like the @dynamic feature in Objective-C, the @NSmanaged feature tells the Swift compiler that the property is stored and implemented at runtime. However, unlike @Dynamic, the @NSmanaged feature is only available in Core Data support.