//
Viewcontroller.swift
Swift+array
Import UIKit
Class Viewcontroller:uiviewcontroller {
Override Func Viewdidload () {
Super.viewdidload ()
Self.view.backgroundColor = Uicolor.yellow;
Array initialization
var numbers = [1,2,3,4,5,6];
Print (Numbers)
var vowels = ["A", "B", "C", "D"];
Print (vowels)
Let Emptyarray = [Int] ()
Array length
Let num = Vowels.count;
Print (num)
Empty sentence
Print (Numbers.isempty)
Print (Emptyarray.isempty)
Get element
Print (vowels[2])//take care not to make an array out of bounds error
Gets the first element and the last element, and returns the selectable
Let first_vowels = Vowels.first;
Print (First_vowels)
Let last_vowels = Vowels.last;
Print (Last_vowels)
Get minimum value, maximum value
Let min_numbers = Numbers.min ();
Print (min_numbers)
Let Max_vowels = Vowels.max ();
Print (Max_vowels)
Scope of Use
Let numbersss = numbers[2..<4];
Print (NUMBERSSS)
Let Vumberss = Numbers[2..<numbers.count]
Print (VUMBERSS)
Contains
Vowels.contains ("a")
Vowels.contains ("B")
Let letter = "a";
If Vowels.contains (letter) {
Print ("\ (letter) in vowel")
}else{
Print ("\ (letter) not in vowel")
}
Get index, return value is selectable
If Let index = Vowels.index (of: "C") {
Print ("C ise a vowel in position \ (Index + 1)")
}else{
Print ("C is not a vowel")
}
Traverse
For index in 0..<numbers.count {
Print (Numbers[index])
}
For number in numbers {
Print (number)
}
for (index, vowel) in vowels.enumerated () {
Iterate over arrays so and elements
Print ("\ (index) + 1: \ (vowel))")
}
Comparison
Let onetofive = [1,2,3,4,5,6];
if onetofive = = Numbers {
Print (True)
}
Before Swift 3.0, arrays are ordered data sets, and Swift 3.0 is unordered
var courses = ["A", "B", "C"]
adding elements
Courses.append ("D");
Print (courses)
Array constants. Arrays defined with let no content can be changed
Courses + = ["E"]
Print (courses)
Courses + = ["F"]
Print (courses)
+ = must be followed by the previous type
Two arrays added
Courses = courses + ["H", "I"];
Print (courses)
Insert
Courses.insert ("W", at:1)
Print (courses)
Delete
Courses.removelast ()
Print (courses)
Courses.removefirst ()
Print (courses)
Courses.remove (at:1)
Print (courses)
Interval Delete
Courses.removesubrange (0..<2)
Print (courses)
modifying elements
Courses[0] = "Z"
Print (courses)
Scope modification
COURSES[1...3] = ["Q", "W", "E"];
Print (courses)
COURSES[0...3] = ["W"]
Print (courses)
Two-dimensional arrays
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]]
Two-dimensional array get elements
BOARD[0]
BOARD[0][0]
Print (Board[0])
Print (Board[0][0])
Get information about two dimensions of a two-dimensional array
Board.count
Board[0].count
A two-dimensional array in swift, with a different number of elements per dimension
Board[0].append (0)
Print (board)
The element added to the first dimension of a two-dimensional array is an array
Board.append ([0,0,0,0])
Print (board)
board + = [[0,0,0,0]]
Print (board)
Last word Nsarray is a class array is a struct
Additional setup after loading the view, typically from a nib.
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
Dispose of any resources the can be recreated.
}
}
Swift Development Array Details use