Array search function not working in Swift

后端 未结 5 1434
Happy的楠姐
Happy的楠姐 2021-01-26 06:20

I confess feeling little stupid, but after hours of trying I have to ask:

class AGE {

     static func getAge() -> Int {

        var age: Int

        for i         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-26 07:13

    You need to initilize it in the beginning:

    var age: Int = 0
    

    If for some reason your for-loop don´t execute then you can´t set your age variable when you only have declared it as var age: Int and not initilized it.

    And regarding:

    I even tried to set var age: Int = 0 at the beginning, but then the function return 0

    That means that you had intilizied your age variable to 0 and the for-loop where never executed and the return will be 0.

提交回复
热议问题