swift 2.2数组的使用

本文和大家分享的主要是swift2.2中的数组相关用法,希望通过本文的分享,对大家有所帮助,一起来看看吧。

什么是数组

1.抽象数据类型的一种;

2.是一串有序的由相同类型元素构成的集合;

3.swift数组使用有序存储同一类型的多个值。相同的值可以多次出现在一个数组中的不同位置中;

4.swift数组会强制检测元素的类型,如果类型不同会报错,遵循像Array < Element >;这样的形式,其中Element是这个数组中唯一允许存在的数据类型;

5.数组常量不可修改元素值和大小;数组变量可任意进行增删改。

swift数组相关操作

1.创建数组;

2.访问数组;

3.修改数组;

4.遍历数组;

5.合并数组;

6.COUNT,isEmpty属性。

swift集合

1.swift提供Array,Set,Dictonary三种集合类型来存储集合数据;

2.swift数组Array是有序数据的集。

实例

//: Playground - noun: a place where people can play

import UIKit

var emptyArray = [Int]()

emptyArray.dynamicType

var someInts = [Int](count:3, repeatedValue:0)

var anotherInts = [10, 20, 30]

var copyInts = anotherInts

var addInts = anotherInts + copyInts

anotherInts[0]

anotherInts[1]

anotherInts[2]

// 0 1 2

anotherInts.append(40)

anotherInts

// +=

anotherInts += [50, 60]

anotherInts.insert(-10, atIndex: 0)

anotherInts[0] = 0

anotherInts

let range = 0...3

anotherInts[range] = [-1,-2,-3,-4]

anotherInts

let constantArr = [Int]()

for item in anotherInts {

print(item)

}

for (index, item) in anotherInts.enumerate() {

print(index, item)

}

anotherInts.count

emptyArray.count

anotherInts.isEmpty

emptyArray.isEmpty


原文链接:http://www.maiziedu.com/wiki/swift/array/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile丽语阅读 9,256评论 0 6
  • 53.计算字符 在字符串中获取字符值的数量, 可以使用字符串字符属性中的计数属性: let unusualMena...
    无沣阅读 4,853评论 0 4
  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young阅读 9,452评论 1 10
  • 一一别天童十余年,百年佛砖滋绿萝;翠竹有根修上意,黑池无波放生何?总因佛容金无量,却惹八戒筑圈多;春花或...
    柳枫林阅读 3,405评论 0 2
  • 云中谁寄简书来。 买了马克飞象的会员配合印象笔记用。但是这个简书好像有更强的分享功能。 在这里,希望以后能当成自己...
    chenwenyouxiang阅读 754评论 0 0