xcode - Use of unresolved Identifier when using for loop in Swift -
i new programming swift , confused why program not recognise have labeled "test", claiming unresolved.
import uikit var rainfall = [38,94,142,149,236,305,202,82,139,222,178,103] var raindays = [3,6,8,7,12,16,10,8,12,14,11,7] let crit_raindays = 11 let et_raindays_lessthan_11 = 150 let et_raindays_morethan_11 = 120 let max_h2ostore = 150 var evap_transpiration: [int] = [] in raindays { if <= 11 { var test = et_raindays_lessthan_11 } else if > 11 { var test = et_raindays_morethan_11 } evap_transpiration.append(test) }
the variable test not seem assigned properly, have no idea why. error message: use of unresolved identifier "test"
when declare local variable in swift, accessible after declaration , in the same scope. in sample code: first declaration accessible inside first if
statement, , second declaration accessible inside second one. correct way is:
for in raindays { var test = 0 if <= 11 { test = et_raindays_lessthan_11 } else if > 11 { test = et_raindays_morethan_11 } evap_transpiration.append(test) }
even more, don't need second if, since if first 1 false, second 1 true. so, code is:
for in raindays { var test = 0 if <= 11 { test = et_raindays_lessthan_11 } else { test = et_raindays_morethan_11 } evap_transpiration.append(test) }
Comments
Post a Comment