Swift 在族类中预期声明错误

Swift 在族类中预期声明错误,swift,xcode,Swift,Xcode,我正在制作一个家族类,当它弹出一个错误时,它是预期的声明,我不知道它的意思是什么,以及如何调试它 // // ViewController.swift // familyClassSecond // // Created by Dordor Zheng on 11/13/16. // Copyright © 2016 Dordor Zheng. All rights reserved. // import UIKit class Person { var name : St

我正在制作一个家族类,当它弹出一个错误时,它是预期的声明,我不知道它的意思是什么,以及如何调试它

//
//  ViewController.swift
//  familyClassSecond
//
//  Created by Dordor Zheng on 11/13/16.
//  Copyright © 2016 Dordor Zheng. All rights reserved.
//

import UIKit

class Person {

    var name : String!
    var age : String!
    var idNum : Int!

    init(name:String, age:String, idNum:Int){
        self.name = name
        self.age = age
        self.idNum = idNum

    }

    func sayPerson() -> String{
    let S = "Hi i'm \(name) i am \(age) years old and my id is \(idNum)"
        print(S)
        return S

    }

}

class female : Person {
    var sex : String = "female"
    override func sayPerson() -> String {
        let S = "Hi i'm \(name) and i am \(age) years old"
        print(S)
        return S

    }
}


class male : Person {
    var sex : String = "male"
    override func sayPerson() -> String {
        let S = "Hi i'm \(name) and i am \(age) years old"
        print(S)
        return S

    }
}



class daughter: female {
    var mother : female!
    var father :  male!
    var sibbling : Array<Person>! = []


    init(me:female,father:male,mother:female,brother:male){
        super.init(name: me.name, age: me.age, idNum: me.idNum)
        self.father = father
        self.mother = mother

    }


    override func sayPerson() -> String {
        let S = "I'm \(name), i am \(age) years old my father is \(father.name) and my mother is \(mother.name)"
        print(S)
        return S

    }

    func appendSibbling(brother:Person){
    sibbling.append(brother)

    }



}



class brother: male {
    var mother : female!
    var father :  male!
    var sibbling : Array<Person>! = []


    init(me:male,father:male,mother:female,daughter:female){
        super.init(name: me.name, age: me.age, idNum: me.idNum)
        self.father = father
        self.mother = mother

    }


    override func sayPerson() -> String {
        let S = "I'm \(name), i am \(age) years old my father is \(father.name) and my mother is \(mother.name)"
        print(S)
        return S

    }

}







class father: male {
    var wife : female!
    var children : Array<Person>! = []


    init(me:male, wife:female,child:Person,brother:male){
        super.init(name: me.name, age: me.age, idNum: me.idNum)
        self.wife = wife
        self.children.append(child)
        self.children.append(brother)

    }



    if(children.count == 1 || children.count == 0){
    var choose = "child"
    }else{
    choose = "children"
    }


    override func sayPerson() -> String {
        let S = "I'm \(name), i am \(age) years old i have \(children.count) children and my wife is \(wife.name)"
        print(S)
        return S

    }

}


class mother: female {
    var husband : male!
    var children : Array<Person>! = []


    init(me:female, husband:male,child:Person,brother:male){
        super.init(name: me.name, age: me.age, idNum: me.idNum)
        self.husband = husband
        self.children.append(child)
        self.children.append(brother)

    }


    override func sayPerson() -> String {
        let S = "I'm \(name), i am \(age) years old i have \(children.count) children and my husband is \(husband.name)"
        print(S)
        return S

    }

}



class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        let one = female(name: "Dordor", age: "8", idNum: 388313)
        let two = male(name: "Tiger", age: "24", idNum: 773424)
        let three = male(name: "Chiwen", age: "48", idNum: 143303)
        let four = female(name: "Hongwen", age: "47", idNum: 243332)


        let child = daughter(me: one, father: three, mother: four, brother: two)
        let mom = mother(me: four, husband: three, child: one,brother:two)
        let dad = father(me: three, wife: four, child: one,brother: two)
        let bigB = brother(me: two, father: three, mother: four, daughter: one)

        child.sayPerson()
        mom.sayPerson()
        dad.sayPerson()
        bigB.sayPerson()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
你有什么想法,错误在if上吗
您可以将粘贴复制到您的代码中,因为我使用的是swift 9.2,它不是beta版

将choose转换为惰性变量:

惰性变量选择:字符串={ 如果self.children.count==1 | | self.children.count==0{ 返回儿童 }否则{ 返回儿童 } }

选择一个函数:

func选择->字符串{ 如果self.children.count==1 | | self.children.count==0{ 返回儿童 }否则{ 返回儿童 } }

您需要选择一个计算属性:

var choose : String {
    if children.count == 1 || children.count == 0 {
        return "child"
    } else {
        return "children"
    }
}