Safawii Kobba
Safawii Kobba

Reputation: 11

Initializers may only be declared within a type

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
    }
}

How can I solve this problem?

Upvotes: 0

Views: 1230

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100541

An init should be inside class / struct type not func

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

Upvotes: 3

Related Questions