温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

斯坦福Swift基础二(数组和字典)

发布时间:2020-07-07 01:33:39 来源:网络 阅读:359 作者:好玩的威威 栏目:移动开发

一、数组定义:

var arr = Array<String>()

或者:

var arr = [String]()
let animals = [“Giraffe”, “Cow”, “Doggie”, “Bird”]
						
animals.append(“Ostrich”) // 编译出错,数组不可变(let)
let animal = animals[5] // 崩溃 (超出数组索引)
					
// 枚举数组						
for animal in animals {						
    println(“\(animal)”)						
}					

相关函数:

insert(T,atIndex:Int)  // a.insert(d,atIndex:1) 在指定位置插入一个元素,结果: [a,d,b,c]

splice(Array<T> , atIndex:Int)  // a.splice([d,e] , atIndex:1) 在指定位置插入一个数组

二、字典定义:

let dic = Dictionary<String,Int>()

或者:

let dic = [String:Int]()

赋值:

dic = ["Stanford":1,"Cal":10]
let ranking = dic["Ohio state"] // ranking类型为int? (会返回nil)
//用元组枚举字典
for (key,value) in dic {
    println("\(key)->\(value)")
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI