初始化程序只能在类型内声明

时间:2019-06-12 16:59:04

标签: swift

import UIKit

func Question() {
    var questiontext : String
    var answer : Bool

    init(text: String , correctanswer: Bool ){ //error in this point : Initializers may only be declared within a type
        questiontext = text
        answer = correctanswer
    }
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

init应该在class / struct类型内,而不是func

class Question { 
    var questiontext : String
    var answer : Bool 
    init(text: String , correctanswer: Bool ){  
        questiontext = text
        answer = correctanswer
    } 
}