Swift array detailed usage resolution

  • 2020-05-15 02:15:42
  • OfStack

1.

The type in the Swift array must be 1, which is different from OC



//  Array initialization 
var numbers = [0,1,2,3,4,5]
var vowels = ["A","E","I","O","U"]

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

//  An empty array 
var emptyArray1:[Int] = []
var emptyArray2:Array<Int> = []
var emptyArray3 = [Int]()
var emptyArray4 = Array<Int>()

//  Create an array with default values ( An array of the same elements )
var allZeros = Array<Int>(repeating: 0, count: 5)
//[0,0,0,0,0]
var allZeros2 = [Int](repeating: 0, count: 5)
//[0,0,0,0,0]

2. Common methods


var numbers = [1,2,3,4,5]
var vowels = ["A","E","I","O","U"]
var emptyArray = [Int]()


//  The length of the array 
vowels.count


//  Sentenced to empty 
numbers.isEmpty
emptyArray.isEmpty


//  Access to elements 
vowels[2]
//  The bounds of the array are 1 A terrible mistake 
//vowels[-1]
//vowels[5]


//  For the first 1 Elements and the end 1 Element, return is selectable 
vowels.first 
vowels.last  //.first and .last All return values are selectable 
emptyArray.first

if let firstVowel = vowels.first{
  print("The first vowel is " + firstVowel)
}

vowels.first!

vowels[vowels.count-1]

//  Get the minimum, the maximum 
numbers.min() //1
vowels.max() //U


//  Using range 
numbers[2..<4] //[3,4]
numbers[2..<numbers.count] //[3,4,5]


//  contains 
vowels.contains("A")
vowels.contains("B")

let letter = "A"
if vowels.contains( letter ){
  print("\(letter) is a vowel")
}
else{
  print("\(letter) is not a vowel")
}


vowels.index(of: "E") // Gets the index and the return value is selectable 

if let index = vowels.index(of: "E"){
  print("E is a vowel in position \(index+1).")
}
else{
  print("E is not a vowel.")
}


//  traverse 
for index in 0..<numbers.count{
  numbers[index]
}

for number in numbers{
  print(number)
}

for (index, vowel) in vowels.enumerated(){
  // Iterate through the array index and elements 
  print("\(index+1): \(vowel)")
}


//  To compare 
var oneToFive = [1,2,3,4,5]
numbers == oneToFive //true

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

//swift 3.0 Previously, arrays were ordered data sets, swift 3.0 After the unordered 

3. Do more


var courses = ["A","B","C"]

//  Add elements 
courses.append("D") //["A","B","C","D"]
print(courses)

//  An array of constant 
// use let A defined array cannot change anything 

courses += ["E"] //+= The back must be the type and the front 1 to  //["A","B","C","D","E"]
print(courses)

//  Add two arrays 
courses = courses + ["F","G"] //+ It has to be an array 
//["A","B","C","D","E","F","G"]
print(courses)

courses.insert("Q", at: 5) 
//["A", "B", "C", "D", "E", "Q", "F", "G"]
print(courses)


//  Remove elements 
courses.removeLast()
//["A", "B", "C", "D", "E", "Q", "F"]
print(courses)

courses.removeFirst()
//["B", "C", "D", "E", "Q", "F"]
print(courses)

courses.remove(at: 4)
//["B", "C", "D", "E", "F"]
//courses.removeAtIndex(10)
print(courses)


// Interval delete operation 
//courses.removeRange(0..<4)
//courses.removeRange(0..<10)
//print(courses)

//courses.removeAll()
//print(courses)


//  Modify the element 
courses[0] = "W"
//["W", "C", "D", "E", "F"]
print(courses)
// Scope changes 
courses[1...3] = ["W","W","W"]
//["W", "W", "W", "W", "F"]
print(courses)

courses[0...3] = ["W"]
//["W", "F"]
print(courses)

4. 2 dimensional array


var board = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]
//var board:[[Int]] = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]
//var board:[Array<Int>] = = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]
//var board:Array<[Int]> = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]
//var board:Array<Array<Int>> = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]


// 2 The dimension array gets the elements 
board[0]
board[0][0]


//  To obtain 2 Dimension array information for two dimensions 
board.count
board[0].count


// Swift In the 2 Dimension array, each 1 The number of elements in a dimension can vary 
board[0].append(0)
board


//  for 2 The dimension of the array 1 The elements added to each dimension are 1 An array 
board.append([0,0,0,0])
board += [ [0,0,0,0] ]
board

5. NSArray

NSArray is a class and Array is a structure


var array1 = [] // The default is NSArray,swift3.0 And then you get rid of that 

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

var array3: NSArray = [1,"hello"]

var array4: [NSObject] = [1 as NSObject,"hello" as NSObject]

Related articles: