Arrays

Thing to learn

Overview

  • Groups data with square brackets "[]"

  • Indexes items from "0"

  • It might be let or var

  • It should contain one particular data type, whether it is string or number

Data Types

  • String

  • Integer

  • Double

Swift knows the data type of the array.

Arrays cannot mix multiple data types.

arrayName<datatype>(): Creates an empty Array with defined data type

Actions

  • Add: arrayName.append

  • Find: arrayName[indexnumber]

  • Count: arrayName.count

  • Sort: arrayName.sorted()

  • Sort-Reverse: arrayName.reversed() // It dows not work as expected. #learn

  • Check: arrayName.contains("itemName")

Sample Codes

Removing or Appending a record to an Array

var numbers = [1,2,3]. // compile: [1, 2, 3]
numbers.append(4) // compile: [1, 2, 3, 4]
numbers.remove(at: 2) // compile: 3
print(numbers) // compile: "[1, 2, 4]\n"

Counting

var deneme1 = ["ahmet","mehmet"]
deneme1.count // compile: 2

var deneme2 = "ahmet"
deneme2.count // compile: 5
// .count in Array's counts record but normally counts characters

Sources

Videos

Articles / Documents

See Also

Last updated