Using Cocoa's existing design patterns is one of the most effective ways to help developers develop a reasonable design, stable performance, and a good scalability application. These patterns all depend on the classes defined in the objective-c. Because of the interoperability of Swift with OBJECTIVE-C, you can still use these design patterns in swift code. In some cases, you can even use the features of the Swift language to extend or simplify these Cocoa design patterns to make these design patterns more powerful and easier to use.
Delegate (delegation)
In Swift and Objective-c, a delegate is typically represented by a protocol that defines an interaction method and a delegate property that follows a specification. Compared with objective-c, when you inherit a delegate in Swift, the inheritance pattern does not change, but the internal implementation has changed. Just like in Objective-c, before you send a message to a delegate, whether it's nil or not, you'll see if the defined method is a method that must be implemented, regardless of whether the delegate has implemented the method, and you'll see it. In Swift, these cumbersome and unnecessary behavioral problems can be effectively eliminated by maintaining the characteristics of type safety.
The code listed below illustrates this process:
1. Check mydelegate not for nil.
2. Check to see if MyDelegate has implemented the inherited Window:willusefullscreencontentsize: method.
3. If MyDelegate is not nil and window:willusefullscreencontentsize is implemented: Method 4. Then call the method and assign the method's return value to the property named Fullscreensize.
Outputs the return value of the method to the console.
Copy Code code as follows:
@inteface Myobject:nsobject
@property (nonatomic, weak) id<nswindowdelegate> delegate;
@end
If let fullscreensize = MyDelegate? Window? (Mywindow, Willusefullscreencontentsize:mysize) {
println (Nsstringfromsize (fullscreensize))
}
Note: In an app that is written entirely with Swift, when defining the delegate property, it acts as an indeterminate nswindowdelegate object and sets the initial value to nil.
Deferred initialization (Lazy initialization)
You can learn more about deferred initialization in the Lazy Stored Properties.
Bug report (Error Reporting)
The error reporting pattern in Swift follows the objective-c pattern, but the new features of the variable value return value in Swift provide additional benefits. For a very simple example, you use the Bool value as the return value of a function to identify whether the function succeeds, and when you need to output an error message, you can add a nserrorpointer type of output parameter Nserror to the function. This type is similar to the nserror in objective-c and increases memory security and non-mandatory parameters. You can use the & operator as a prefix to refer to an indeterminate value nserror type to pass an error message as a Nserrorpointer object. As shown in the following code:
Copy Code code as follows:
var writeerror:nserror?
Let written = Mystring.writetofile (path, Atomically:false,
Encoding:nsutf8stringencoding,
Error: &writeerror)
If!written {
If let error = Writeerror {
println ("Write failure: \ (error.localizeddescription)")
}
}
When you implement your own method, you need to configure a Nserrorpointer object and set the memory property of the Nserrorpointer object to the Nserror object you created. First, check the parameters passed by the caller to make sure it is a nil Nserror object.
Copy a plain text new window
Copy Code code as follows:
Func Contentsfortype (typename:string! error:nserrorpointer)-> anyobject! {
If Cannotproducecontentsfortype (typeName) {
If error {
Error.memory = Nserror (Domain:domain, Code:code, UserInfo: [:])
}
return Nil
}
// ...
}
Target-action Mode (target-action)
When a specific event occurs and an object is required to send a message to another object, we usually use the Cocoa target-action design pattern. The target-action model in Swift and objective-c is basically similar. In Swift, you can use the Selector type to achieve the selectors effect in objective-c. See the example of using the target-action design pattern in Swift in Objective-c selectors.
Type matching and uniform specification (introspection)
In Objective-c, you can use the Iskindofclass: method to check whether an object is a specified type, and you can use the Conformstoprotocol: method to check whether an object follows the specification of a particular protocol. In Swift, you can use the IS operator to do the above function, or you can use as? Matches the specified type down.
You can use the IS operator to check whether an instance is a specified subclass. If the instance is a specified subclass, then the is operation evaluates to true, and vice versa.
Copy Code code as follows:
If object is UIButton {
object is of type UIButton
} else {
object is not of type UIButton
}
You can also use as? Operator tries to match the subtype down, as? operator returns an indeterminate value, which is used in conjunction with the If-let statement.
Copy Code code as follows:
If let button = object as? UIButton {
Object is successfully cast to type UIButton and bound to button
} else {
Object could not is cast to type UIButton
}
Please see more information in Type casting.
Checking the syntax of a matching protocol is the same as checking the syntax of a matching class, and the following is using as? Check for examples of matching protocols:
Copy Code code as follows:
If let DataSource = object as? Uitableviewdatasource {
Object conforms to Uitableviewdatasource and are bound to DataSource
} else {
Object not conform to Uitableviewdatasource
}
Note that when the match is done, DataSource is converted to the Uitableviewdatasource type, so you can access and invoke only the properties and methods defined by the Uitableviewdatasource protocol. When you want to do something else, you must convert it to another type.
The
can see more relevant information in protocols.