Swift Basic usage-for loop traversal, iterate through the dictionary, loop through the generated array

Source: Internet
Author: User

Playground-noun:a place where people can playimport uikit//------------------------------------------------------- -----------------------//1. for//traditional for loop mode also supports var num = 0for in Swift (var i = 0; i < i++) {num + = i}num//--------------------------------- ---------------------------------------------//2. For-in is used to traverse a range (range), sequence (sequence), set (collection), series (progression)//All elements execute a series of statements//1> closed interval loops, from 1 cycles to 10num = 0for I in 1...10 {num + = i}num//2> open interval loop, from 1 cycles to 9num = 0for i in 1..<10 {num + = i}num//3> If you do not need to know the value of each item in the interval, you can use an underscore (_ Instead of the variable name, omit access to the interval value num = 0for _ in 1...10 {num++}numnum = 0for I in 0...5 {to J in 0...5 {num = i + j}} num//------------------------------------------------------------------------------//3. Loop-generated array var array = [String] () for I in 0..<10 {array.append ("itcast \ (i)")}array//----------------------------------- -------------------------------------------//4. Loop Traversal dictionary var agedict:dictionary<string, int> = ["Mary ":", "Rose": 20]var names = [String] () var ages = [Int] () for (key, value) in Agedict {names.append (key) Ages.ap  Pend (value)}namesagesvar keys = [string] () var values = [string] () var dict1 = ["Name": "Mike", "Age": "+"];for (name, age) In Dict1 {keys.append (name) values.append (age)}keysvalues//------------------------------------------------------ ------------------------//5. 1 of the functions that use labels to exit loops//Use labels are: You can explicitly specify which loop to exit, and the following example exits the loop with the name "Mainloop" var result = 1;mainloop:for I in 1...5 {for j in 1...5 {result + = J if (i = = 3 && j = 3) {Break Mainloop}}} Result

Swift Basic usage-for loop traversal, iterate through the dictionary, loop through the generated array

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.