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" 
so, decided make new .swift file , implement nsdatecomponents there. got "expressions not allowed @ top level" error. 
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
Post a Comment