iOS π/Swift
Swift-02 κΈ°λ³Έ λ°μ΄ν°νμ
yongmin.Lee
2021. 1. 26. 09:48
λ³μ ν€μλ
var λ³μμ΄λ¦: λ°μ΄ν°νμ = κ°
ex) var varName: Int = 2
μμν€μλ
let λ³μμ΄λ¦: λ°μ΄ν°νμ = κ°
ex) let varName: String = "abc"
λ°μ΄ν°νμ
- Int or UInt − This is used for whole numbers. More specifically, you can use Int32, Int64 to define 32 or 64 bit signed integer, whereas UInt32 or UInt64 to define 32 or 64 bit unsigned integer variables. For example, 42 and -23.
- Float − This is used to represent a 32-bit floating-point number and numbers with smaller decimal points. For example, 3.14159, 0.1, and -273.158.
- Double − This is used to represent a 64-bit floating-point number and used when floating-point values must be very large. For example, 3.14159, 0.1, and -273.158.
- Bool − This represents a Boolean value which is either true or false.
- String − This is an ordered collection of characters. For example, "Hello, World!"
- Character − This is a single-character string literal. For example, "C"
- Optional − This represents a variable that can hold either a value or no value.
- Tuples − This is used to group multiple values in single Compound Value.
Any, AnyObject, nil
- Any : Swiftμ λͺ¨λ νμ μ μ§μΉνλ ν€μλ
- AnyObject : λͺ¨λ ν΄λμ€ νμ μ μ§μΉνλ νλ‘ν μ½
- nil : μμμ μλ―Ένλ ν€μλ
Collection, 컬λ μ νμ
Array : μμκ° μλ 리μ€νΈ 컬λ μ
Dictionary : ν€μ κ°μ μμΌλ‘ μ΄λ£¨μ΄μ§ 컬λ μ
Set : μμκ° μκ³ , λ©€λ²κ° μ μΌν 컬λ μ
1. Array
// λΉ Int Array μμ±
var integers: Array<Int> = Array<Int>()
// var integers: Array<Int> = [Int]()
// var integers: Array<Int> = []
// var integers: [Int] = Array<Int>()
// var integers: [Int] = [Int]()
var integers: [Int] = []
// var integers = [Int]()
integers.append(1)
integers.append(100)
// Int νμ
μ΄ μλλ―λ‘ Arrayμ μΆκ°ν μ μμ΅λλ€
//integers.append(101.1)
print(integers) // [1, 100]
// λ©€λ² ν¬ν¨ μ¬λΆ νμΈ
print(integers.contains(100)) // true
print(integers.contains(99)) // false
// λ©€λ² κ΅μ²΄
integers[0] = 99
// λ©€λ² μμ
integers.remove(at: 0)
integers.removeLast()
integers.removeAll()
// λ©€λ² μ νμΈ
print(integers.count)
2. Dictionary
// Keyκ° String νμ
μ΄κ³ Valueκ° AnyμΈ λΉ Dictionary μμ±
var anyDictionary: Dictionary<String, Any> = [String: Any]()
// κ°μ νν
// var anyDictionary: Dictionary <String, Any> = Dictionary<String, Any>()
// var anyDictionary: Dictionary <String, Any> = [:]
var anyDictionary: [String: Any] = Dictionary<String, Any>()
var anyDictionary: [String: Any] = [String: Any]()
var anyDictionary: [String: Any] = [:]
// var anyDictionary = [String: Any]()
// ν€μ ν΄λΉνλ κ° ν λΉ
anyDictionary["someKey"] = "value"
anyDictionary["anotherKey"] = 100
print(anyDictionary) // ["someKey": "value", "anotherKey": 100]
// ν€μ ν΄λΉνλ κ° λ³κ²½
anyDictionary["someKey"] = "dictionary"
print(anyDictionary) ["someKey": "dictionary", "anotherKey": 100]
// ν€μ ν΄λΉνλ κ° μ κ±°
anyDictionary.removeValue(forKey: "anotherKey")
anyDictionary["someKey"] = nil
print(anyDictionary)
//ν€μ ν΄λΉνλ κ°μ λ€λ₯Έ λ³μλ μμμ ν λΉνκ³ μ ν λλ μ΅μ
λκ³Ό νμ
μΊμ€ν
ννΈμμ λ€λ£Έ
3. Set
// λΉ Int Set μμ±
var integerSet: Set<Int> = Set<Int>()
// Set μμ μ½μ
integerSet.insert(1)
integerSet.insert(100)
integerSet.insert(99)
integerSet.insert(99)
integerSet.insert(99)
print(integerSet) // [100, 99, 1]
print(integerSet.contains(1)) // true
print(integerSet.contains(2)) // false
integerSet.remove(100)
integerSet.removeFirst()
print(integerSet.count) // 1
// Setλ μ§ν© μ°μ°μ κ½€ μ μ©ν©λλ€
let setA: Set<Int> = [1, 2, 3, 4, 5]
let setB: Set<Int> = [3, 4, 5, 6, 7]
// ν©μ§ν©
let union: Set<Int> = setA.union(setB)
print(union) // [2, 4, 5, 6, 7, 3, 1]
// ν©μ§ν© μ€λ¦μ°¨μ μ λ ¬
let sortedUnion: [Int] = union.sorted()
print(sortedUnion) // [1, 2, 3, 4, 5, 6, 7]
// κ΅μ§ν©
let intersection: Set<Int> = setA.intersection(setB)
print(intersection) // [5, 3, 4]
// μ°¨μ§ν©
let subtracting: Set<Int> = setA.subtracting(setB)
print(subtracting) // [2, 1]