Interoperability (interoperability) allows developers to define swift classes that incorporate the Objective-c language features. When writing the Swift class, you can inherit not only the parent class written in objective-c language, the OBJECTIVE-C protocol, but also some other features of objective-c. This means that developers can create swift classes based on familiar, reliable classes, methods, and frameworks already in the objective-c, and optimize them with the modern and more efficient language features provided by Swift.
Classes that inherit Objective-c
In Swift, a developer can define a subclass that inherits the classes written by using Objective-c. The subclass is created by adding a colon (:) followed by the class name of Swift, followed by the Objective-c class name.
Copy Code code as follows:
SWIFT
Import Uikit
Class Myswiftviewcontroller:uiviewcontroller {
Defining classes
}
Developers can inherit all the functionality from the Objective-c parent class. If the developer wants to overwrite the methods in the parent class, do not forget to use the override keyword.
Adopt agreement
In Swift, developers can use the protocols defined in OBJECTIVE-C. Like the Swift protocol, all OBJECTIVE-C protocols are written in a comma-separated list, following the parent class name of the class (if it has a parent).
Copy Code code as follows:
SWIFT
Class Myswiftviewcontroller:uiviewcontroller, Uitableviewdelegate, Uitableviewdatasource {
Defining classes
}
The OBJECTIVE-C agreement is consistent with the use of the Swift protocol. If the developer wants to refer to the Uitableviewdelegate protocol in Swift code, you can use Uitableviewdelegate directly (refer to Objective-c in id<uitableviewdelegate\ > is equivalent).
Writing constructors and destructors
Swift's compiler ensures that when initialized, the constructor does not allow any uninitialized properties in the class, which can increase the security and predictability of the code. In addition, unlike the Objective-c language, Swift does not provide a separate memory allocation method for developers to invoke. When you use the native Swift initialization method (even with the Objective-c class), Swift converts the Objective-c initialization method into Swift's initialization method. See the Builder for more information on how to implement the developer Custom Builder.
When the developer wants to perform additional cleanup work before the class is freed, a destructor is needed to replace the Dealloc method. Before the instance is freed, Swift will automatically invoke the destructor to perform the destructor. After Swift calls the destructor of the subclass, the parent class's destructor is invoked automatically. When the developer uses the Objective-c class or inherits from the Objective-c class's Swift class, Swift also automatically invokes the Dealloc method in the parent class of the class for the developer. For more information about how to implement a developer custom destructor, see the destructor.
Integrated Interface Builder
The Swift compiler contains attributes that enable the developer's Swift class to integrate some of the features of the Interface Builder. Like Objective-c, you can use outlets,actions and real-time rendering in Swift (live rendering).
Using Outlets and action
Use Outlets and Action to connect source code and Interface Builder UI objects. Using Outlets and Action in Swift requires inserting the @iboutlet or @ibaction keyword before the property and method declarations. Declaring a outlet collection is also using the @iboutlet attribute, which specifies an array for the type.
When a developer declares a Outlet in Swift, the Swift compiler automatically converts the type to a weak (weak), implicit (implicitly), unpackaged (unwrapped) Optional (object-c the corresponding pointer type) data type and assigns it an initialized null value nil. In fact, the compiler uses @iboutlet weak var name:type! = Nil to replace @IBOutlet var name:type. The compiler converts the type to a weakly (weak), implicit (implicitly), unpackaged (unwrapped) optional type, so the developer does not need to assign an initial value to the type in the constructor. When the developer initializes the object class from the storyboard (storyboard) or the Xib file, the defined Outlet and the objects are connected together, so the Outlet are implicit and not packaged. Because the created outlets are generally weak, the default outlets is a weak type.
For example, the following Swift code declares a class that has Outlet, Outlets collections, and Action:
Copy Code code as follows:
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 parameter name of the method can be omitted.
Real-time rendering (live rendering)
Developers can create vivid, interactive, customizable views (view) using @ibdesignable and @ibinspectable in Interface Builder. When a developer inherits UIView or NSView from defining a view, you can add the @ibdesignable property before the class declaration. When you add a custom view to the Interface Builder (set in the custom view class in the monitor panel), Interface Builder will render your customized view on the canvas.
Note: Real-time rendering only for objects in the frame
You can also add the @ibinspectable attribute to a type attribute that is compatible with user-defined Run-time properties. This allows you to edit these properties in the Monitor panel when the developer adds the customized view to the Interface Builder.
Copy Code code as follows:
SWIFT
@IBDesignable
Class Mycustomview:uiview {
@IBInspectable var Textcolor:uicolor
@IBInspectable var iconheight:cgfloat
/* ... */
}
Indicates attribute attributes
In Objective-c, properties usually have a set of attribute (Attributes) descriptions to indicate some additional information about the property. In Swift, developers can identify these attributes in different ways.
Strongly typed and weak types
The properties in Swift are strongly typed by default. Use the weak keyword to decorate a property to indicate that its object is stored as a weak reference. This keyword can only be decorated with optional object types. For more information, please refer to the features.
Read/write and read-only
In Swift, there are no ReadWrite and readonly features. When declaring a storage property, use let to decorate it as read-only and use Var to modify it to be read/write. When declaring a calculated property, it is provided with a getter method to make it read-only, providing getter methods and setter methods to make it readable/writable. For more information, consult the properties.
Copy
In Swift, the copy attribute of Objective-c is converted to the @nscopying attribute. Attributes of this class must comply with the Nscopying protocol. For more information, consult the features.
Implementing the Core Data Managed object subclass
Core Data provides a set of properties for basic storage and implementation of Nsmanagedobject subclasses. In the Core Data model, the @nsmanaged attribute is added before each attribute of the attribute or relationship that is associated with the management of the object's subclass is defined. Similar to the @dynamic features inside the objective-c, the @NSManaged feature tells the Swift compiler that the store and implementation of this property will be completed at run time. However, unlike @dynamic, the @NSManaged feature is only available in Core Data support.