Arrays
Arrays
1. Declare:
var someInts = [Int]()
var someStrings = [String]()
OR
var someInts: [Int] = [6, 8]
2. Add a value:
someInts.append(4)
someStrings.append("ABC")
someInts += [6, 8]
3. Count the number of items in the array
print(someInts.count)
print(someStrings.count)
4. Get value out of array, based on index:
print(someInts[2])
//This prints the third value
Notes:
- Indexes start with 0
- "/\n" means to print a new line.
5. Splitting a string into an array using a separator:
fullNameArr = fullName.components(separatedBy: " ")
New Lines:
let text = "line1\nline2"
let array = text.components(separatedBy: CharacterSet.newlines)
1. Declare:
var someInts = [Int]()
var someStrings = [String]()
OR
var someInts: [Int] = [6, 8]
2. Add a value:
someInts.append(4)
someStrings.append("ABC")
someInts += [6, 8]
3. Count the number of items in the array
print(someInts.count)
print(someStrings.count)
4. Get value out of array, based on index:
print(someInts[2])
//This prints the third value
Notes:
- Indexes start with 0
- "/\n" means to print a new line.
5. Splitting a string into an array using a separator:
fullNameArr = fullName.components(separatedBy: " ")
New Lines:
let text = "line1\nline2"
let array = text.components(separatedBy: CharacterSet.newlines)
Comments
Post a Comment