6.Swift 中的Array

在Objective-C当中, 我们知道有NSArray, 在Swift中, 也有对应的数据, 叫做Array, 在Swfit中的Array比Objective-C可以更加的简洁易操作, 下面让我们来看看.


1.数组初始化

// 1.隐式声明
var array1: [AnyObject]?

// 2.初始化声明
var array2 = [AnyObject]()

// 3.带有默认值的声明
var array3 = [AnyObject](count: 3, repeatedValue: 1)

PS: 第三种声明方法, 是根据数组里的类型来进行判断, 数组里的是什么类型, repeatedValue参数里就是什么类型.


2.数组的增删改查

// 1.声明一个带有默认值的数组
var firstNameArray = [String](count: 3, repeatedValue: "Chen")

// 2.使用bool值属性判断数组是否为空
if firstNameArray.isEmpty {
    print(firstNameArray.isEmpty)
    print("The first name list is empty")
} else {
    print(firstNameArray.isEmpty)
    print("The first name list is not empty")
}

// 3.往数组里添加元素
firstNameArray.append("Zhang")
firstNameArray += ["Li"]

// 4.更改数组范围里的元素
firstNameArray[0...3] = ["Luo"]

// 5.插入元素到数组内
firstNameArray.insert("Hu", atIndex: firstNameArray.endIndex)

PS: 这里面的append, += 效果是一样的, 都是用来添加元素到数组的末尾.


3.数组的遍历

// 1.直接遍历数组内的元素
for item in firstNameArray {
    print(item)
}

// 2.遍历数组内的元素索引以及元素内容
for (index, value) in firstNameArray.enumerate() {
    print("Item \(String(index + 1)): \(value)")
}

这次就讲到这里, 下次继续

接着分析 (result (type_ident (component id='Bool' bind=Swift.(file).Bool))) (brace_stmt range=[re.swift:1:59 - line:14:1] (pattern_binding_decl range=[re.swift:2:5 - line:2:33] (pattern_named type='[UInt8]' 'b') Original init: (call_expr type='[UInt8]' location=re.swift:2:19 range=[re.swift:2:13 - line:2:33] nothrow (constructor_ref_call_expr type='(String.UTF8View) -> [UInt8]' location=re.swift:2:19 range=[re.swift:2:13 - line:2:19] nothrow (declref_expr implicit type='(Array<UInt8>.Type) -> (String.UTF8View) -> Array<UInt8>' location=re.swift:2:19 range=[re.swift:2:19 - line:2:19] decl=Swift.(file).Array extension.init(_:) [with (substitution_map generic_signature=<Element, S where Element == S.Element, S : Sequence> (substitution Element -> UInt8) (substitution S -> String.UTF8View))] function_ref=single) (argument_list implicit (argument (type_expr type='[UInt8].Type' location=re.swift:2:13 range=[re.swift:2:13 - line:2:19] typerepr='[UInt8]')) )) (argument_list (argument (member_ref_expr type='String.UTF8View' location=re.swift:2:29 range=[re.swift:2:21 - line:2:29] decl=Swift.(file).String extension.utf8 (declref_expr type='String' location=re.swift:2:21 range=[re.swift:2:21 - line:2:21] decl=re.(file).check(_:_:).encoded@re.swift:1:14 function_ref=unapplied))) )) Processed init: (call_expr type='[UInt8]' location=re.swift:2:19 range=[re.swift:2:13 - line:2:33] nothrow (constructor_ref_call_expr type='(String.UTF8View) -> [UInt8]' location=re.swift:2:19 range=[re.swift:2:13 - line:2:19] nothrow (declref_expr implicit type='(Array<UInt8>.Type) -> (String.UTF8View) -> Array<UInt8>' location=re.swift:2:19 range=[re.swift:2:19 - line:2:19] decl=Swift.(file).Array extension.init(_:) [with (substitution_map generic_signature=<Element, S where Element == S.Element, S : Sequence> (substitution Element -> UInt8) (substitution S -> String.UTF8View))] function_ref=single) (argument_list implicit (argument (type_expr type='[UInt8].Type' location=re.swift:2:13 range=[re.swift:2:13 - line:2:19] typerepr='[UInt8]')) )) (argument_list (argument (member_ref_expr type='String.UTF8View' location=re.swift:2:29 range=[re.swift:2:21 - line:2:29] decl=Swift.(file).String extension.utf8 (declref_expr type='String' location=re.swift:2:21 range=[re.swift:2:21 - line:2:21] decl=re.(file).check(_:_:).encoded@re.swift:1:14 function_ref=unapplied))) ))) (var_decl range=[re.swift:2:9 - line:2:9] "b" type='[UInt8]' interface type='[UInt8]' access=private readImpl=stored writeImpl=stored readWriteImpl=stored)
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值