Basic usage of Swift (one of Swift development)

Source: Internet
Author: User

Last night, Apple released the next-generation programming language Swift, which provides an iBook instruction document. You can refer to it if you need it. Https://itunes.apple.com/cn/book/swift-programming-language/id881256329? Mt = 11


Next, let me talk about some basic things. Everyone is just reading them, so I will inevitably encounter errors in understanding them. Welcome to note. Of course, you can also directly read the official documentation.


I. Basic Types of let and var

Let: constant. values cannot be assigned twice. You do not need to specify the type when assigning values. The Compiler automatically determines Based on the assignment. You can also manually specify the type.
Var: Variable

Let a = 12 // declare a as constant 12, type Intlet B: Double = 12 // declare a as constant 12, manually specify the type Double


Ii. String

1. the plus sign can be directly spliced into strings, and Other types cannot be directly spliced into strings.
 
let str = "Hello, playground. "let name = "Tom "let count = 10let say = str + name + String(count)  // "Hello, playground. Tom 10"


2. Directly introduce code into strings
let a = 1let b = 2let say = "the number is \(a + b)"  // "the number is 3"


3. arrays and dictionaries can be directly declared using []

1. Array
Var arr = ["catfish", "water", "tulips ", "blue paint"] arr [1] = "bottle of water" // modify the second arr // ["catfish", "bottle of water", "tulips ", "blue paint"]


2. Dictionary
Var man = ["like": "apple", "name": "Tom",] man ["iphone"] = "5s" // Add the man ["like"] = "mac" // modify the man field // ["iphone ": "5 s", "like": "mac", "name": "Tom"]


Iv. control statements

1. if cannot directly use a variable or constant as a condition. It must be a judgment statement.
let a = 12if a > 0 {    // do something} else {   // do something}

If you write it as if a {}, an error is returned. You cannot directly use variables or constants to determine bool.


2.
For I in 0 .. 3 {/I = 0, 1, 2} for var I = 0; I <3; ++ I {/I = 0, 1, 2} // array let scores = [1, 2, 3, 4, 5] for score in scores {// score} // dictionary let numsDic = ["": [2, 3, 5, 7, 11, 13], "B": [1, 1, 2, 3, 5, 8], "c": [1, 4, 9, 16, 25],] for (key, numbers) in numsDic {for number in numbers {// number }}


3. while
var n = 2while n < 100 {    n = n * 2}var m = 2do {    m = m * 2} while m < 100


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.