swift的字符串和数组定义

2014年06月06日 4294点热度 0人点赞 0条评论

字符串格式化

Swift 使用\(item)的形式迚行字符串格式化:

let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples. by sollyu"
let appleSummary = "I have \(apples + oranges) pieces of fruit."

数组和字典

Swift 使用[]操作符声明数组(array)和字典(dictionary):

var shoppingList = ["catfish", "water", "tulips", "blue paint"]
shoppingList[1] = "bottle of water"
var occupations = [
    "MyBBS": "http://bbs.swift8.org",
    "Kaylee": "Mechanic",
]
occupations["MyBlog"] = "http://www.sollyu.com"

一般使用初始化器(initializer)语法创建空数组空字典:

let emptyArray = String[]()
let emptyDictionary = Dictionary<String, Float>()

如果类型信息已知,则可以使用[]声明空数组,使用[:]声明空字典

预览

swift的字符串和数组定义

Sollyu

保持饥渴的专注,追求最佳的品质