Swift2.2 failable initializer允许提前返回nil以及和Java的不同

前端之家收集整理的这篇文章主要介绍了Swift2.2 failable initializer允许提前返回nil以及和Java的不同前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

发现swift和java有一个完全不一样的地方

在swift中,子类必须先初始化子类的所有属性,然后才能调用父类的构造器. 而在java中.super调用必须出现在构造函数的第一行.

java代码

public class Dog {
	String name;
	Dog(String name){
		this.name = name;
	}
}


class NoisyDog extends Dog {
	int age;
	
	NoisyDog(String name) {
        // 交换以下两行的顺序会报错: Constructor call must be the first statement in a constructor
		super(name);
        this.age = 5;
	}
	
}

对应的swift代码:

class Dog {
    var name: String;
    init(name: String){
      self.name = name;
    }
}


class NoisyDog: Dog {
    var age: Int
    
    override init(name: String) {
        //交换以下两行的顺序会报错error: property 'self.age' not initialized at super.init call
        self.age = 5;
        super.init(name: name);
       
    }
    
}

书中关于failable initializer描述有错误

以下代码在swift2.1及之前会编译错误,在swift2.2中修正了这个bug
swift2.2: 子类failable designated 构造器在返回nil前不必初始化子类的属性也不必调用父类的designated initializer,也就是说,在子类的failable initilizer中允许提前返回nil

//: Playground - noun: a place where people can play
import Foundation

class Dog{
    var name: String
    init(name: String){
        self.name = name
    }
    
}

class NoisyDog : Dog {
    var age: Int
    
    override init(name: String){
        self.age = 5
        super.init(name: name)
    }
    
    init?(name: String,age: Int){
        // as of swift2.2: 子类failable designated 构造器在返回nil前不必初始化子类的属性
        // 也不必调用父类的designated initializer
        if age < 0 {
            return nil
        }
        
        self.age = age;
        super.init(name: name)
    }
    
    
}

见: http://stackoverflow.com/questions/26495586/best-practice-to-implement-a-failable-initializer-in-swift/26497229#26497229

原文链接:https://www.f2er.com/swift/323679.html

猜你在找的Swift相关文章