Swift Online weather forecast case tutorial

Source: Internet
Author: User

After a week of preparation, the swift online weather forecast video tutorial met with you. This set of video cases requires a certain language base learner.

Knowledge points involve many aspects.

For example: Swift location location corelocation, how Swift calls OC components, installation of afnetworking, etc. also involve Mac System terminal command line operations.

Stickers look at the interface

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/49/89/wKiom1QUT0bDwfrCAAKspGVdIVA930.jpg "title=" screen shot 2014-09-12 pm 11.13.23.png "alt=" wkiom1qut0bdwfrcaakspgvdiva930.jpg "/>650" this.width=650; "src=" http:// S3.51cto.com/wyfs02/m00/49/8b/wkiol1quuefxfwcgaae15zy3luk061.jpg "title=" screenshot 2014-09-13 pm 10.09.19.png "alt=" Wkiol1quuefxfwcgaae15zy3luk061.jpg "/>

The implementation is simple, but the knowledge points use a lot, such as search location and so on.

  ViewController.swift//  WeatherApp//  Wenqi Pilotage Training// bjflexedu.com//  Qq:376610000import uikitimport corelocationclass viewcontroller: uiviewcontroller, cllocationmanagerdelegate {         @IBOutlet  var  cityname : uilabel         @IBOutlet  var icon  : uiimageview         @IBOutlet  var tempTxt :  uilabel         @IBOutlet  var busy :  uiactivityindicatorview         @IBOutlet  var messageinfo  : uilabel        let locationmanage:cllocationmanager  = cllocationmanager ()         override func  Viewdidload () &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; super.viewdidload ()        busy.startanimating ()                 let img = uiimage (named :  "Background.jpg")       self.view.backgroundcolor = uicolor ( PATTERNIMAGE:&NBSP;IMG)                  locationManage.delegate = self       locationmanage.desiredaccuracy = kcllocationaccuracybest     Locationmanage.requestalwaysauthorization ()     locationmanage.startupdatinglocation ()                      }    func locationmanager (manager: cllocationmanager!,  didupdatelocations locations: anyobject[]!) {&NBSP;&NBSP;&NBSP;&Nbsp;          var location =  locations[ locations.count-1] as cllocation        if  location.horizontalaccuracy > 0{            let latitude = location.coordinate.latitude            let longitude = location.coordinate.longitude                          self.updateweatherdata (latitude,longitude:longitude)                         Locationmanage.stopupdatinglocation ()                                  }                    }    func  Updateweatherdata (latitude:cllocationdegrees,longitude:cllocationdegrees) {           let url =  "Http://api.openweathermap.org/data/2.5/weather"         let params = ["lat": Latitude, "lon": Longitude, "CNT":0]       let manage = afhttprequestoperationmanager ()               let success = {          (operation:afhttprequestoperation!, response:anyobject!)  -> Void in              &NBSP;&NBSP;&NBSP;//PRINTLN (response)                  self.busy.stopanimating ()          self.busy.hidden = true         self.messageInfo.text = nil                         //name         let jsonResult:NSDictionary =  response as  nsdictionary                 self.cityname.text = jsonresult["Name"]? as string         var tempnum:double        if let tempvalue  =   jsonresult["main"]? ["Temp"]? as? double{          tEmpnum = round (tempvalue - 273.15)             self.tempTxt.text =  "\ (tempnum)  "                      }                          let idcon =  (jsonresult["Weather"]? as nsarray) [0]? ["id"]? as? int                         let nowtime = nsdate (). timeintervalsince1970        let  sunrise =  jsonresult["SYS"]? ["Sunrise"]? as? double        let  sunset =  jsonresult["SYS"]? ["Sunset"]? as? double        var nighttime = false//true  means night. ; false means daytime         if nowtime < sunrise | |  nowTime > sunset{             nighttime = true         }else{             nightTime = false         }                 self.updateicon (idcon!,nighttime:nighttime)                  }        let failure =  {             (Operation: afhttprequestoperation!, error:nserror!)  -&gT; void in            println ("\ (Error)" )                          self.messageInfo.text =  "Remote Data not fetching"                  }                     manage. GET (url, parameters: params, success: success, failure: failure)                                                }             Func updateicon (Condition:int,nighttime:boOL) {                if  ( CONDITION&NBSP;&LT;&NBSP;300)  {            if  nightTime {                 self.icon.image = uiimage (named:  "Tstorm1_night")              } else {                 self.icon.image = uiimage (named:  "Tstorm1")             }         }            // Drizzle         else if  (condition < 500)  {        &Nbsp;    self.icon.image = uiimage (named:  "Light_rain")          }            //  Rain / Freezing rain / Shower rain         else if  (condition < 600)  {             self.icon.image = uiimage (named:  "Shower3")          }            //  Snow        else if  (condition < 700)  {            self.icon.image =  UIImage (named:  "snow4")         }             // fog / mist / haze / etc.         else if  (condition < 771)  {             if nightTime {                 self.icon.image = uiimage (named:  "Fog_night")              } else {                 self.icon.image = uiimage (named:   "Fog")             }         }            //  tornado / squalls        else if  (condition  < 800)  { &nbsP;          self.icon.image = uiimage (named:  "TSTORM3")         }             // sky is clear        else  if  (condition == 800)  {             if  (Nighttime) {                 self.icon.image = uiimage (named:  "Sunny_night")  // sunny night ?            }             else {                 self.icon.image = uiimage (named:  "Sunny")              }        }             // few / scattered / broken  clouds        else if  (condition < 804)  {             if  (Nighttime) {                 self.icon.image =  UIImage (named:  "cloudy2_night")             }             else{                 self.icon.image = uiimage (named:   "Cloudy2")             }         }            // overcast clouds         else if  (condition == 804)  {             self.icon.image = uiimage (named:  " Overcast ")         }             // Extreme        else if  ( condition >= 900 && condition < 903)  | |   (condition > 904 && condition < 1000)  {             self.icon.image = uiimage (named:  " TSTORM3 ")         }             // Cold        else if  (condition == 903)  {             self.icon.image = uiimage (named:  "snow5")          }             // Hot        else if  (condition == 904 )  {            self.icon.image =  UIImage (named:  "Sunny")         }             // Weather condition is not available         else {             self.icon.image = uiimage (named:  "dunno")          }                                     }             func locationmanager (manager:  cllocationmanager!, didfailwitherror error: nserror!) {    self.messageinfo.text =  "Location information not found"        &NBSP;&NBSP;PRINTLN ("location information not found \ (Error)")              }    override func didreceivememorywarning ()  {         super.didreceivememorywarning ()         / / dispose of any resources that can be recreated.     }}


Swift Online weather forecast case tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.