Can fail constructor (failable initializers)

Source: Internet
Author: User

The latest version of Swift in Xcode6.1 is 1.1, and a new feature has been introduced in this release: the constructor can fail. The process of instantiating an object is to class struct provide initial values for each stored property (parameter) that is actually given to or by the constructor initialization. In some cases, the initialization process is likely to fail. For example, instantiating an object that requires access to a resource file during instantiation is like loading a picture from a file:


"Swift.png")

If the file does not exist or is not allowed to be accessed for some reason, NSImage the initialization process will fail. In the swift 1.1 release, such cases can be captured by a failed constructor. If a failed constructor is used when constructing an object, the object is returned when the object construction succeeds and returned when the object construction fails nil . You can therefore instantiate an object with a conditional judgment statement using a failed constructor:


"Swift.png") {
Loaded the image successfully
else {
Could not load the image
}

initThe initialization method can be initialized by init adding ? or ! turning it into a failed initialization method, which means that an object's initialization method produces two results. For example, the Int initialization method of a type init becomes a fail-initialized method, and then a String type conversion is performed:

  
extension int {
init? ( FromString: string) {
if let i = Fromstring.toint () {
//Initialize
self = i
else {
//return nil, discarding self was implied
return nil

In a failed constructor or a fail-initialized method, once returned represents a failure of the nil construction or initialization, no additional values are returned. In the above example, when the String failure cannot be resolved Integer , the initialization fails, and the nil value returned after the parse succeeds is returned.

The failed constructor/initialization method resolves an issue that was previously only possible with the factory method to capture construction or initialization failures in Swift. For example, an enumeration that uses fromRaw a factory method to find an enumeration member for which it corresponds by using a primitive value, the return type is an optional enumeration type. That is, if the enumeration member that corresponds to the original value exists, then the enumeration member is returned, if it does not exist nil . The Swift compiler now combines a failed constructor with a switch statement to determine the corresponding enumeration member of the original value with a failed initialization method, and returns if there is no corresponding nil :


EnumColor:Int {
CaseRed =0,Green =1,Blue =2

Implicitly synthesized
var rawValue:Int {/* Returns raw value for current case */}

//implicitly synthesized
Init? (RawValue: int) {
switch rawValue {
case 0: self =. Red
case 1: self =. green
case 2: self =. blue
default: return nil

The use of a failed constructor can greatly unify the construction object syntax in swift, eliminating the confusing and repetitive redundancy syntax between the constructor and the factory method, making swift more concise. With the addition of the feature of the failed constructor, Swift NSError adjusts the factory initialization method with parameters in most cocoa, thus enhancing the unity of the construction object syntax in swift and creating a better development experience for the developer.

Original address: Failable initializers

Can fail constructor (failable initializers)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.