iOS-swift-基础篇1

一.swift是啥?答:百度。

二.swift基础知识。

1.输出函数:print

print("Hello, world!")

2.简单数据类型

 变量声明:var

  常量声明:let   

    var myVariable = 42
    myVariable = 50
    let myConstant = 42

如果没有变量或者常量的类型(如下1、2行),定义的变量或者常量的类型是在第一次赋值时自动判定的。

如果声明了类型(如下第3行),则必须赋值指定类型。

    let implicitInteger = 70
    let implicitDouble = 70.0
    let explicitDouble: Double = 70

变量转换:

1     let label = "The width is "
2     let width = 94
3     let widthLabel = label + String(width)

字符串拼接

字符串插入变量方法:\()

将变量放入括号里面就ok了

1     let apples = 3
2     let oranges = 5
3     let appleSummary = "I have \(apples) apples."
4     let fruitSummary = "I have \(apples + oranges) pieces of fruit."

数组和字典:

数组和字典对象都放在 [] 里面,用 ","分隔,并且最后一个对象后面可以加 逗号。

创建有对象的字典和数组:

1     var shoppingList = ["catfish", "water", "tulips", "blue paint"]
2     shoppingList[1] = "bottle of water"
3      
4     var occupations = [
5         "Malcolm": "Captain",
6         "Kaylee": "Mechanic",
7     ]
8     occupations["Jayne"] = "Public Relations"

创建空数组和字典:

1     let emptyArray = [String]()
2     let emptyDictionary = [String: Float]()

或者

1     shoppingList = []
2     occupations = [:]

3.控制流(Control Flow)

条件控制:if 和 switch

循环控制:for-in,for,while 和 repeat-while

用于条件判断的语句的圆括号是可选的。

for-in:

 1     let individualScores = [75, 43, 103, 87, 12]
 2     var teamScore = 0
 3     for score in individualScores {
 4         if score > 50 {
 5             teamScore += 3
 6         } else {
 7             teamScore += 1
 8         }
 9     }
10     print(teamScore)

if:

1     var optionalString: String? = "Hello"
2     print(optionalString == nil)
3      
4     var optionalName: String? = "John Appleseed"
5     var greeting = "Hello!"
6     if let name = optionalName {
7         greeting = "Hello, \(name)"
8     }

类型后面加“?”表示 可选类型(optional value)。

if 和 let 一起使用判断值是否为nil,如果是nil,那么就会跳过这个模块。
使用 if let 可以操作可选类型。

?,??:

?? 后面是 默认值

1     let nickName: String? = nil
2     let fullName: String = "John Appleseed"
3     let informalGreeting = "Hi \(nickName ?? fullName)"

使用  ?? 也可以操作可选类型。如果 可选类型值为nil 那么就使用 ?? 后面的默认值。

 

switch:

switch 支持多种类型的数据类型和比较运算。 不仅仅数值类型和相等判断。

 1     let vegetable = "red pepper"
 2     switch vegetable {
 3     case "celery":
 4         print("Add some raisins and make ants on a log.")
 5     case "cucumber", "watercress":
 6         print("That would make a good tea sandwich.")
 7     case let x where x.hasSuffix("pepper"):
 8         print("Is it a spicy \(x)?")
 9     default:
10         print("Everything tastes good in soup.")
11     }

程序执行完 switch 的某个 case 后,会跳出switch,无需添加 break。

 

for-in:

for-in 遍历字典:

 1     let interestingNumbers = [
 2         "Prime": [2, 3, 5, 7, 11, 13],
 3         "Fibonacci": [1, 1, 2, 3, 5, 8],
 4         "Square": [1, 4, 9, 16, 25],
 5     ]
 6     var largest = 0
 7     for (kind, numbers) in interestingNumbers {
 8         for number in numbers {
 9             if number > largest {
10                 largest = number
11             }
12         }
13     }
14     print(largest)

 

while 和 repeat-while:

 1     var n = 2
 2     while n < 100 {
 3         n *= 2
 4     }
 5     print(n)
 6      
 7     var m = 2
 8     repeat {
 9         m *= 2
10     } while m < 100
11     print(m)

 

..< 和 ... 

循环起止边界判断,使用 ..< 是包含最小值而不包含最大值。使用 ... 是最小值和最大值都包含。

1     var total = 0
2     for i in 0..<4 {
3         total += i
4     }
5     print(total)

 

学习资源来自swift官方文档: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-ID1

 

posted @ 2017-05-26 11:20  孟栋sky  阅读(243)  评论(0编辑  收藏  举报