swift2 - Declaring a variable as Integer in Swift -


so i'm trying learn swift , on button action, want declare variable integer collects value text field, calculates if not nil , prints value. why asking me make variable constant?

this i've tried

import uikit  class viewcontroller: uiviewcontroller {      @iboutlet weak var enterage: uitextfield!     @iboutlet weak var catage: uilabel!      @ibaction func findage(sender: anyobject) {          var newage = int(enterage.text!)          if newage! == 0         {         var catyears = newage! * 7         catage.text = "your cat \(catyears) in age"         }          else         {              catage.text = "please enter whole number"         }      }         override func viewdidload() {         super.viewdidload()         // additional setup after loading view, typically nib.     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }   } 

xcode version 7.1 error: enter image description here

thanks

it warning, not error: program still compile , run if ignore (but down road, it's best practice if don't).

it telling because 'variable' gets assigned initial , value once, , thereafter read (never written to). hence, doesn't need variable; constant (and better reflects intent of program).


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -