Swift Combat Applet 1 weather Forecast

Source: Internet
Author: User

In the case of a certain swift foundation, let us do some small programs to practice practiced hand, today to try to do a simple weather forecast. Create a new project, named Weather, and then go online to find a weather forecast API, Baidu search "Weather API", open a website, such as:


We'd better find the JSON-formatted API, like this.



After the API is found, let's create a user interface, open storyboard, load a button, and name The weather information for Beijing:



Add a text view below the button to render the weather information:


Now add a button click event, open Viewcontroller, enter the following code:

@IBAction func btnpressed (sender:anyobject) {    println ("button Clicked")    }

Go back to Storyboard, right-click, drag the touch up inside to view controller to see the btnpressed we just defined, click on the link, the effect is as follows:



Run a look at the effect and click on the button to show:


Then we load the weather information, in order to make our program better, build a playground,targets tick weather in the project, build the playground


The code in Playground is as follows:

var url = nsurl (string: "http://api.k780.com:88/?app=weather.today&weaid=1&appkey=10003&sign= B59bc3ef6191eb9f747dd4e83c99f2a4&format=json ") var data:nsdata = NSData (contentsofurl:url!, Options: nsdatareadingoptions.datareadinguncached, error:nil)!var str = nsstring (Data:data, encoding:nsutf8stringencoding)

We then parse the JSON, using Swift's own parsing tool, with the following code:


var json:anyobject! = Nsjsonserialization.jsonobjectwithdata (data, options:NSJSONReadingOptions.AllowFragments, Error:nil) var Weatherinfo:anyobject? = JSON?. Objectforkey ("result") var city = Weatherinfo?. Objectforkey ("citynm")

Effect


You can see from the right that we have the city information in JSON and complete all the information we need:

var city = Weatherinfo?. Objectforkey ("citynm") var temp = Weatherinfo?. Objectforkey ("Temperature") var day = Weatherinfo?. Objectforkey ("Days")

Now we present the weather information in the UI, put the partial copy that we just wrote in the playground, then create a new method Loadweather in the Viewcontroller, copy the code in, and then call Loadweather in the Viewdid method, Finally, add a iboutlet associated with storyboard, and the method is still dragging, dragging the new referencing outlet in storyboard associated with the TV. Then the Loadweather method is still added in the Btnpressed method.

The display statement that adds information to the Loadweather method:

TV?. Text = "City: \" \ n temp: \ (temp) \ n Date: \ (Day) "

Run a look at the effect,


Although it appears, our text is editable, tick editable in storyboard and run again:



It's done, and now every time you click the button, the weather will be re-requested, so let's try it.

Swift Combat Applet 1 weather Forecast

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.