Swift- 数组

Posted 一枚IT女

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift- 数组相关的知识,希望对你有一定的参考价值。

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

 

import UIKit

 

var str = "Hello, playground"

 

//容器类

 

////1.数组 Array

//

////一般形式

//var numbers:[Int] = [0,1,2,3,4,5]

//var vowels:[String] = ["A","E","I","O","U"]

//

////泛型形式

//var numbers1:Array<Int> = [0,1,2,3,4,5]

//var vowels1:Array<String> = ["A","E","I","O","U"]

//

////初始化空数组

//var emptyArray1:[Int] = []

//var emptyArray2:Array<Int> = []

//var emptyArray3 = [Int]()

//var emptyArray4 = Array<Int>()

//

////定义固定大小的数组并初始化为0

//var allZeros = [Int](repeating: 0, count: 5)

//var allZeros2 = Array<Int>(repeating: 1, count: 5)

 

 

////数组的一些属性

//var numbers = [1,2,3,4,5]

//var vowels = ["A","E","I","O","U"]

//var emptyArray = [Int]()

//

//numbers.isEmpty

//emptyArray.isEmpty

//

////数组索引

//vowels[2]

//vowels.count

//vowels.first

//vowels.last

//emptyArray.first

//emptyArray.last

//

//if let firstVowel = vowels.first {

//    print("The first vowel is" + firstVowel)

//}

//

//vowels.first!

//vowels[vowels.count - 1]

//

//numbers.min()

//numbers.max()

//

//numbers[2..<4]

//numbers[2..<numbers.count]

//vowels.contains("A")

//

//vowels.index(of: "E")

//vowels.index(of: "F")

//

////数组遍历

//for number in numbers {

//    print(number)

//}

//

//for (i, vowel) in vowels.enumerated() {

//    print("\(i + 1): \(vowel)")

//}

//

//var oneToFive = [1,2,3,4,5]

//numbers == oneToFive//数组是有序的数据集合

 

//数组的操作

 

//1.

var courses = ["玩儿转Swift","玩儿转Swift第一季","玩儿转Swift第二季"]

 

courses.append("玩儿转Swift第三季")

print(courses)

 

courses += ["玩儿转Swift第四季"]

courses = courses + ["玩儿转Swift面向协议编程","玩儿转Swift面向函数编程"]

 

print(courses)

 

 

courses.insert("玩儿转Swift设计模式", at: 5)//5是索引值,从1开始

 

courses.remove(at: 4)

 

//

//courses.removeSubrange(0..<4)

//print(courses)

 

//

courses[0..<4] = ["玩儿转Swift3"]

print(courses)

 

//二维数组

 

var board:Array<Array<Int>> = [[1024,16,2,0],

             [256,4,2,0],

             [64,2,0,0],

             [2,0,0,0]]

 

board[0]

board[0][0]

 

board.count

board[0].count

 

board[0].append(0)

board

board.append([0,0,0,0])

board += [[2,2,2,2]]//二维数组是添加一个元素(二维数组的元素还是数组)

 

 

//NSArray

//var array1 = []

var array2 = [1,2,3,4,5] as NSArray

var array3:NSArray = [1,"Hello",3.0]

var array4:[NSObject] = [1 as NSObject,"Hello" as NSObject,3.0 as NSObject]

 

 

 

 

 

 

 

 

 

以上是关于Swift- 数组的主要内容,如果未能解决你的问题,请参考以下文章

swift UI数组创建BUTTON

swift学习第六天:数组

Swift初见Swift数组

swift Array 数组

求助关于 swift 数组,NSArray 和 闭包 的问题

Swift:如何在 plist 的数组中读取多个数组?