Swift教程之NSArray

Swift教程之NSArray,第1张

概述//MARK:-------数组------------------/* 数组与字典 使用[] *** 作符声明数组(array)和字典(dictionary)*///数组//Demo1: //声明空数组//let emptyArray1: Array = []//var emptyArray2 = Int[]()let emptyArray1 = []var emptyA
//MARK:-------数组------------------/*    数组与字典    使用[] *** 作符声明数组(array)和字典(dictionary)*///数组//Demo1: //声明空数组//let emptyArray1: Array = []//var emptyArray2 = Int[]()let emptyArray1 = []var emptyArray2 = [Int]()let emptyArray3: Array<String> = []//Demo2:var shopPingList = ["芒果","橘子","水","葡萄","香蕉"]//-------------增加-------------//数组增加元素shopPingList.append("苹果")print(shopPingList)//等价于//shopPingList += ["柚子"]//print(shopPingList)shopPingList += ["西瓜","木瓜"]print(shopPingList)//数组插入元素shopPingList.insert("苹果",atIndex: 2)print(shopPingList)//----------------------删除-------------shopPingList.removeLast()print(shopPingList)//需要index < countshopPingList.removeAtIndex(4)print(shopPingList)//shopPingList.removeAll()//print(shopPingList)//--------------修改-----------//修改第一个元素的值shopPingList[0] = "哈密瓜"print(shopPingList)//把下标为4、5、6、7的元素替换成后面的"Bananas","Apples", 值变了,count减少了shopPingList[1...4] = ["Bananas","Apples"]print(shopPingList)//---------查询---------//常用方法//数组的个数print(shopPingList.count)//数组的容量,值为大于count的 最小的2的n次方的数,比如2、4、8、16print(shopPingList.capacity)//判断是否为空print(shopPingList.isEmpty)//-----------数组遍历----------for item in shopPingList{    print(item)}//数组元素的下标和值for (index,value) in shopPingList.enumerate(){    print("Item \(index + 1): \(value)")}//Demo3://----------数组与数组相加---------var threeDoubles = [Double](count: 3,repeatedValue: 0.0)       //[0.0,0.0,0.0]print(threeDoubles)var anotherThreeDoubles = Array(count: 3,repeatedValue: 2.5)   //[2.5,2.5,2.5]print(anotherThreeDoubles)var sixDoubles = threeDoubles + anotherThreeDoubles             //[0.0,2.5]print(sixDoubles)
总结

以上是内存溢出为你收集整理的Swift教程之NSArray全部内容,希望文章能够帮你解决Swift教程之NSArray所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1086637.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存