import UIKit
// Array (배열)
// 배열선언
// => var 변수명: Array<데이터형> = [내용]
var names: Array<String> = ["yagom", "chulsoo", "younghee", "yagom" ]
//var names: [String] = ["yagom", "chulsoo", "younghee", "yagom" ]
print(names)
/// ["yagom", "chulsoo", "younghee", "yagom"]
// 빈 배열 생성하기()
var emptyArray: [Any] = [Any]()
//var emptyArray: [Any] = Array<Any>()
// 타입을 정확히 명시하는 경우 []로도 빈 배열 생성 가능
//var emptyArray: [Any] = []
print(emptyArray.isEmpty) /// true
print(names.count) /// 4
print(names[2]) /// younghee
names[2] = "jenny"
print(names[2]) /// jenny
//print(names[4]) // ERROR
names.append("elsa")
print(names) /// -> ["yagom", "chulsoo", "jenny", "yagom", "elsa"]
names.append(contentsOf: ["john", "max"])
print(names) /// -> ["yagom", "chulsoo", "jenny", "yagom", "elsa", "john", "max"]
names.insert("happy", at: 2)
print(names) /// -> ["yagom", "chulsoo", "happy", "jenny", "yagom", "elsa", "john", "max"]
names.insert(contentsOf: ["jinhee", "minsoo"], at: 5)
print(names)
/// -> ["yagom", "chulsoo", "happy", "jenny", "yagom", "jinhee", "minsoo", "elsa", "john", "max"]
print(names[4]) // -> yagom
// firstIndex: 해당요소의 인덱스 알아내기
print(names.firstIndex(of: "yagom")) /// Optional(0)
print(names.firstIndex(of: "christal")) /// nil
print(names.first) /// Optional("yagom")
print(names.last) /// Optional("max")
print(names.count) /// 10
let firstItem: String = names.removeFirst()
print(names.count) /// 9
let lastItem: String = names.removeLast()
let indexZeroItem: String = names.remove(at: 0)
print(names.count) /// 7
print(names[1...3]) /// ["jenny", "yagom", "jinhee"]
import UIKit
// Array (배열)
// 배열선언
// => var 변수명: Array<데이터형> = [내용]
var names: Array<String> = ["yagom", "chulsoo", "younghee", "yagom" ]
//var names: [String] = ["yagom", "chulsoo", "younghee", "yagom" ]
print(names)
/// ["yagom", "chulsoo", "younghee", "yagom"]
// 빈 배열 생성하기()
var emptyArray: [Any] = [Any]()
//var emptyArray: [Any] = Array<Any>()
// 타입을 정확히 명시하는 경우 []로도 빈 배열 생성 가능
//var emptyArray: [Any] = []
print(emptyArray.isEmpty) /// true
print(names.count) /// 4
print(names[2]) /// younghee
names[2] = "jenny"
print(names[2]) /// jenny
//print(names[4]) // ERROR
names.append("elsa")
print(names) /// -> ["yagom", "chulsoo", "jenny", "yagom", "elsa"]
names.append(contentsOf: ["john", "max"])
print(names) /// -> ["yagom", "chulsoo", "jenny", "yagom", "elsa", "john", "max"]
names.insert("happy", at: 2)
print(names) /// -> ["yagom", "chulsoo", "happy", "jenny", "yagom", "elsa", "john", "max"]
names.insert(contentsOf: ["jinhee", "minsoo"], at: 5)
print(names)
/// -> ["yagom", "chulsoo", "happy", "jenny", "yagom", "jinhee", "minsoo", "elsa", "john", "max"]
print(names[4]) // -> yagom
// firstIndex: 해당요소의 인덱스 알아내기
print(names.firstIndex(of: "yagom")) /// Optional(0)
print(names.firstIndex(of: "christal")) /// nil
print(names.first) /// Optional("yagom")
print(names.last) /// Optional("max")
print(names.count) /// 10
let firstItem: String = names.removeFirst()
print(names.count) /// 9
let lastItem: String = names.removeLast()
let indexZeroItem: String = names.remove(at: 0)
print(names.count) /// 7
print(names[1...3]) /// ["jenny", "yagom", "jinhee"]
'모바일앱 > Swift' 카테고리의 다른 글
structures and Classes (+ enum) (0) | 2021.12.09 |
---|---|
Loops (반복문) (0) | 2021.12.08 |
조건문(IF, Switch) (0) | 2021.12.08 |
Enumeration (열거형) (0) | 2021.12.08 |
Day11_extra_Tuple (0) | 2021.12.07 |
Day10 closure (0) | 2021.12.03 |
Day09 generic (0) | 2021.12.03 |
Day08 function parameter (0) | 2021.12.03 |