swift 2.2 数组的使用

2016-12-21 18:03:18 +08:00
 maiziedu

本文和大家分享的主要是 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/

1591 次点击
所在节点    程序员
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/329216

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX