分类 Swift 下的文章

Swift学习笔记

数据类型

1、直接声明变量或者常量,数据类型隐世定义

let MyConst = 10
var MyVarible = 12
MyVarible = 30

2、显式声明变量的数据类型

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

3、数据类型转换需要显示声明

let label = "This width is "
let width = 45
let widthlabel = label + String(width)
var implicitDouble1 = implicitInteger + Int(implicitDouble)

4、一个简单的技巧在字符串中包含变量, 将变量放置在小括号中"()", 同时在小括号前加反斜线"\"

let apples = 3
let oranges = 5
let appleSummary = "I have (apples) apples"
let fruitSummary = "I have (apples + oranges) piecies of fruite."

5、创建数组和字典用方括号"[]", 通过在方括号中放索引或者键值来访问它们的子元素。最后一个元素后边可以放置逗号*/

var ShoppingList = ["catfish", "water", "tulips", "bluepaint",]
ShoppingList[1] = "bottle of water"
var str1: String = ShoppingList[1]

print(ShoppingList)

var occupations = ["Malcolm":"Captain", "Kaylee":"Mechanic"]
print(occupations)

var arrayTest = [1, 2, 3, 4, "5"]//数据类型为NSObject
//var int1: Int = arrayTest[0]//wrong
//var str1:String = arrayTest[1]//wrong

6、可以用如下方式初始化一个空数组或者空字典

var emptyArray1 = String
var emptyDictionary2 = String: Float
//当类型可以推断出的时候可以用如下方式初始化, 例如为变量设置新值或者为函数传递参数的时候
var emptyArray2 = []
var emptyDictory2 = [:]

流程控制

let individualScores = [71, 32, 43, 74, 55]
var teamScore = 0;
for score in individualScores {
if(score > 50) {//if条件必须是Boolean值, 如果不和0显式的比较而使用if(score)是错误的
teamScore += 3;
} else {
teamScore += 1;
}
}
print(teamScore)

You can use if and let together to work with values that might missing. These values are represented as optionals. An optional value either contains a value or contains nil to indicate that a value is missing.
Write a question make(?) after that type of value to mark the value as optional.

var optionString: String? = "hello"
print(optionString == nil)
var optionalName: String? = "John Appleseed"//if set nil and "if" condition is false
var greeting = "Hello!"
if let name = optionalName {//const name only available inside the block
greeting = "hello, (name)"
} else {
greeting = "Oh"
}
//Optional values has questiong
//let nickName: String? = nil
let nickName: String? = "Smith"
let fullName: String = "John Appleseed"
let infomalGreeting = "Hi (nickName ?? fullName)"

9、Switches support any kind of data and a wide variety of comparision operations-they aren't limited to integers and tests for equality

let vegetable = "red pepper"
switch vegetable {
case "celery"://case 不需要使用break退出
print("Add some raisins and make ants on a log.")
case "cucumber", "watercress"://多个选项用逗号隔开
print("That would make a good tea sandwich.")
case let x where x.hasSuffix("pepper"):
print("Is it a spicy (x)")
default://default 选项不能省去
print("Everything tastes good in soup.")
}

Notice how let can be used in a pattern to assign the value that matched the pattern to a constant.

10、词典是无序的

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

11、While循环

var n = 2;
while n < 100 {
n = n * 2
}
print(n)
var m = 128
repeat {
m *= 2
} while m < 100
print(m)

12、Use ..< make a range that omits its upper value, and use ... to make a range that includes both values.

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