Swift报错:“Return from initializer without initializing all stored properties”

struct User {
    var email: String
    var firstName: String
    var lastName: String
    init() {
    }
}

解决方案:

给成员变量添加?修饰符使其变成Optional类型,或者赋初始值,或者在init()中进行赋值

struct User {
    var email: String?
    var firstName: String?
    var lastName: String?
    init() {
    }
}
struct User {
    var email: String = ""
    var firstName: String = ""
    var lastName: String = ""
    init() {
    }
}
struct User {
    var email: String
    var firstName: String
    var lastName: String
    init() {
        email = ""
        firstName = ""
        lastName = ""
    }
}

 

你可能感兴趣的:(iOS,swift,ios)