2.Swift - 变量和常量

2020-07-12  本文已影响0人  IOXusu
//var声明一个变量
var a = 42
a = 50
//let声明一个常量,注意,b =50这句会报错,因为你不能给一个常量再次赋值
let b = 30
//b = 50
//你还可以声明变量的类型,而不是使用类型推断
var c:Int = 20

2.1字符串输出的几种形式:
(1). +

let lable = "This width is "
let width = 94
let widthLabel = lable + String(width)
print(widthLabel)
> This width is 94

(2).( )

let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
print(appleSummary)

(3)."""

let quotation = """
abcd
efg
       hijk
"""
print(quotation)

2.2数组和字典

var shoppingList = ["catfish", "water", "tulips"]
print(shoppingList[0])
var occupations = [
    "Malcolm":"Captain",
    "Kaylee":"Mechanic",
]
print(occupations["Malcolm"])
//增加数据
shoppingList.append("blue paint")
上一篇下一篇

猜你喜欢

热点阅读