ios - How to add a simple button in a ViewController? -


i have following code.

import uikit  class viewcontroller: uiviewcontroller {      var button : uibutton?      override func viewdidload() {         super.viewdidload()          button = uibutton.buttonwithtype(uibuttontype.system) uibutton?     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }  } 

i following error

error:

'anyobject' not convertible 'uibutton?' 

i know might doing fundamentally wrong. know is.

according me: have declared button optional uibutton - think means, value of button can unset or nil

therefore, while initialising type mentioned as uibutton?

is right way ?

you can't cast optional uibutton in way you're doing it. correct way cast optional uibutton is:

button = uibutton.buttonwithtype(uibuttontype.system) as? uibutton 

interpret as: cast can either return nil or uibutton object, resulting in optional uibutton object.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -