swift - Xcode "Expected Delcaration","Expression not allowed at the top level" error -


i trying implement nsdatecomponent in viewcontroller.swift file when got "expected declaration" error , "instance member"something" cannot used on type "viewcontroller" enter image description here

so, decided make new .swift file , implement nsdatecomponents there. got "expressions not allowed @ top level" error. enter image description here

from found googling, know should implement in function, not have idea kind of function should wrap into. know why errors in viewcontroller.swift file future reference. thank in advance, , sorry if question duplicate.

and here original code

let weekbmondaycomponents:nsdatecomponents = nsdatecomponents() weekbmondaycomponents.year = 2016 weekbmondaycomponents.month = 01 weekbmondaycomponents.day = 11 weekbmondaycomponents.hour = 08 weekbmondaycomponents.minute = 39 weekbmondaycomponents.timezone = nstimezone.systemtimezone() let weekbmonday = usercalendar.datefromcomponents(weekbmondaycomponents)! 

you'll want declare weekbmondaycomponents property or variable of subclass, , initialize perhaps in viewdidload notification handler.

class viewcontroller: uiviewcontroller {     var weekbmondaycomponents:nsdatecomponents = nsdatecomponents()  func setupaday() {     super.viewdidload()     // additional setup after loading view, typically nib.     weekbmondaycomponents.year = 2016     weekbmondaycomponents.month = 01     weekbmondaycomponents.day = 11     weekbmondaycomponents.hour = 08     weekbmondaycomponents.minute = 39     weekbmondaycomponents.timezone = nstimezone.systemtimezone() } 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -