String, Character
// Chararcter ๋ฐฐ์ด -> String
let catCharacters: [Character] = ["C", "a", "t", "!", "๐ฑ"]
let catString = String(catCharacters)
Foundation ์ NSString ๊ณผ ์ฐ๊ฒฐ๋์ด ์์ด์, bridging ํ์ฌ NSString์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ธฐ๋ ํ๋ค.
let s: String = "๋ฌธ์์ด"
let c: Character = "C"
let special = "ํน์๋ฌธ์\n\0\t\\\""
let unicode = "\u{1F496}"
// ํน์๋ฌธ์๋ฅผ ๊ทธ๋ฅ ์ถ๋ ฅํ๊ณ ์ถ๋ค๋ฉด?
let str = #"""
๋ฌธ์์ด\n๋ฌธ์์ด -> ๋ฌธ์ ๊ทธ๋๋ก ๋์ด.
๋ฌธ์์ด\#n๋ฌธ์์ด -> ๊ฐํ์ผ๋ก ๋์ด.
\(str) -> ๋ฌธ์ ๊ทธ๋๋ก ๋์ด
\#(str) -> str ๋ณ์์ ํ ๋น๋ ๊ฒ์ด ๋์ด.
"""#
let s1 = ""
let s2 = String()
print( s1.isEmpty && s2.isEmpty ) // true
var num = 132323
print("๋ณ์ \(num) ์ถ๋ ฅ ๊ฐ๋ฅ")
"๋ฌธ์์ด" + "๋ฌธ์์ด"
var s = "๋ฌธ์์ด"
s += "์ฐ์ฐ"
๋ฌธ์์ด์ ๊ฐ ํ์ ์ด๋ฏ๋ก ํจ์์ ์ธ์๋ก ์ ๋ฌ๋ ๋, ์๋ก์ด ๋ณ์์ ํ ๋น๋ ๋ ๋ณต์ฌ๋๋ค.
let s = "Character"
// ์๋ 3๊ฐ์ง๋ ๋ชจ๋ ๊ฐ์ ๋ฌธ์์ด.
for c in s:
print(c)
for i in 0..<s.count:
print(s[i])
for i in s.indices:
print(s[i])
String.Index ์ธ ์ธ๋ฑ์ค ํ์ (index type) ์ ์ด์ฉํ์ฌ ๋ฌธ์์ด ๋ด ํน์ index ์ ๋ฌธ์์ ์ ๊ทผ ๊ฐ๋ฅ.
let greeting = "Guten Tag!"
greeting[greeting.startIndex]
// G
greeting[greeting.index(before: greeting.endIndex)]
// ! - endIndex ๋ ์ฐ๋ฆฌ๊ฐ ์๊ฐํ๋ end index + 1 ์ด๋ผ ์ ๊ทผํ ์ ์๋ค.
greeting[greeting.index(after: greeting.startIndex)]
// u
let index = greeting.index(greeting.startIndex, offsetBy: 7)
greeting[index]
// a
Insert
greeting.insert("โญ๏ธ", at: greeting.endIndex)
// "Guten Tag!โญ๏ธ"
// ๋ฌธ์ ํ๋๊ฐ ์๋๋ผ ๋ฌธ์์ด์ ์ถ๊ฐํ๋ ค๋ฉด contentsOf ์ฌ์ฉ.
welcome.insert(contentsOf: "๐งฝ๐ชฃ", at: welcome.index(before: welcome.endIndex))
// "Guten Tag!๐งฝ๐ชฃโญ๏ธ"
Remove
gretting.remove(at: greeting.index(before: greeting.endIndex))
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
5
welcome.removeSubrange(range)
Swift ์์ prefix ๊ฐ์ ๋ฉ์๋๋ก ์ป๋ ๊ฒ์ String์ด ์๋๋ผ, ์์ ์ ์ํํ๋ ๋์ ์งง์ ์๊ฐ๋์ ์ฌ์ฉํ๋ Substring ์ด๋ค. ๊ฒฐ๊ณผ๊ฐ ์ ์ฅ๋ ๋, Substring ์ String ์ผ๋ก ๋ณํํ๋ค. String ๊ณผ SubString ์ ๋ชจ๋ StringProtocol ์ ๋ฐ๋ฅด๊ธฐ ๋๋ฌธ์ Substring ์์๋ String ์์ ์ฌ์ฉํ ์ ์๋ ๋๋ถ๋ถ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
let snsd = "์ง๊ธ์ ์๋
์๋, ์์ผ๋ก๋ ์๋
์๋, ์์ํ ์๋
์๋."
let index = snsd.firstIndex(of: ",") ?? snsd.endIndex
let now = snsd[0..<index] // ์ง๊ธ์ ์๋
์๋
let now = String(now) // string ์ผ๋ก ์ฐ๋ ค๋ฉด ๋ณํ ํ์.
์??? ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ญ๋นํ์ง ์๊ธฐ ์ํด์!! ์๋ ์ฌ์ง์ฒ๋ผ ์๋ณธ String ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ณต์ ํด์ ์ฌ์ฉํ๋ค๊ฐ, ํด๋น ๋ฌธ์์ด์ ๋ฐ๋ก ์ฌ์ฉํ๊ฒ ๋๋ฉด ๋ณต์ฌํด์ ์๋ก์ด ์ฅ์์ ์ ์ฅํ๋ค.
let s1 = "Girls' Generation"
let s2 = "Girls' Generation TTS"
let s3 = "Girls' Generation Oh!GG"
s1 != s2 // true
// hasPrefix, hasSuffix ๋ ๋ณดํต ์ด๋ ๊ฒ ๋จ๋
์ผ๋ก ์ฌ์ฉํ๊ธฐ ๋ณด๋ค๋,
// ๋ฌธ์ ๋ฐฐ์ด์์ for ๋ฌธ์ผ๋ก ๊ฐ ๋ฌธ์์ด์ prefix, suffix ๊ฐ ์๋์ง ํ์ธํ๋ค.
s1.hasPrefix("Girls' Generation") // true
s3.hasSuffix("TTS") // false
ํ์ด์ฌ์ upper, lower ์ ๊ฐ์.
"ABCD".lowercased()
"Asdf".uppercased()
"์ค๋ฐ๊ฟ\n ๋ฐฑ์ฌ๋์ \\" // ์ด๋ฐ์์ผ๋ก ์ฌ์ฉํ๊ฒ ๋๋๋ฐ,
print(#"๋ฐฑ์ฌ๋์ \\n\\"#) // # ์ ์๋ค๋ก ๋ถ์ด๋ฉด ํน์๋ฌธ์๋ฅผ ์์. ์ง์ง ๊ณ ๋๋ก ๋์ด.
let number: Int = 100
#"๋ฐฑ์ฌ๋์ \#(number)"# // # ์ ์๋ค๋ก ๋ถ์ด๋ฉด ํน์๋ฌธ์๋ฅผ ์์.
var arr: [String] = ["a", "b", "c"] // ๋ณ๊ฒฝ ๊ฐ๋ฅ - ์์ดํ
์ถ๊ฐ, ์ญ์ , ๋ณ๊ฒฝ ๊ฐ๋ฅ.
let dict: [Int: String] = [1 : "one", 2 : "two", 3 : "three"] // ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ.
NSArray ์ bridging ๊ฐ๋ฅ.
// ์๋ 3๊ฐ์ง๋ ๋ชจ๋ ๊ฐ์ ๊ฒฐ๊ณผ.
var arr1: [Int] = []
var arr2: [Int] = [Int]()
var arr3: Array<Int> = Array<Int>()
var d1 = Array(repeating: 0.0, count: 3) // ๊ธฐ๋ณธ ๊ฐ์ ๋ฐ๋ณตํ์ฌ ๋ฐฐ์ด ์์ฑ ๊ฐ๋ฅ.
var d2 = [1.3, 2.312, 1.2]
print(d1 + d2) // ๋ํ๊ธฐ ๊ฐ๋ฅ. [0.0, 0.0, 0.0, 1.3, 2.312, 1.2]
// ๋ฌธ์์ด๊ณผ ๊ฐ์ด isEmpty, count ๋ณ์ ์์.
arr1.isEmpty // true
d1.count // 3
var shoppingList = ["iPhone", "iPad"]
shoppingList.append("MacBook")
shoppingList += ["iMac", "MacPro"]
print(shoppingList[4]) // subscript ๊ตฌ๋ฌธ์ผ๋ก get, set ๋ชจ๋ ๊ฐ๋ฅ.
shoppingList[2] = "iPad Pro"
shoppingList.insert("Lightening-C cable", at: 1) // insert
shoppingList.remove(at: 3)
shoppingList.removeLast()
for element in shoppingList {
// ๋ฐฐ์ด ์ํ ๊ฐ๋ฅ.
}
for (index, value) in enumarted(shoppingList) {
// index, ๋ฐฐ์ด ์์ ์์ผ๋ก ์ํ ๊ฐ๋ฅ.
}
NSSet ๊ณผ ์ฐ๊ฒฐ.
var onlyOne = Set<Character>()
var onlyOne: Set<Character> = ["b", "d", "z"]
onlyOne.insert("a")
onlyOne.<isEmpty // false
onlyOne.remove("z")
onlyOne.contains("d")
// ์์๊ฐ ์์ด์ ์ด๋ป๊ฒ ๋์ฌ์ง ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์, ์ ํด์ง ์์๋ก ์ฌ์ฉํ๊ณ ์ถ์ผ๋ฉด
for char in onlyOne.sorted() {
// sorted ๋ฅผ ์ฌ์ฉํ์ฌ ์ํํ์.
}
a.intersection(b) // a, b์ ๊ต์งํฉ A n B
a.symmetricDifference(b) // (A U B) - (A n B)
a.union(b) // ํฉ์งํฉ A U B
a.subtracting(b) // ์ฐจ์งํฉ A - B
a.isSubset(of: b) // a ๊ฐ b ์ ๋ถ๋ถ์งํฉ์ธ๊ฐ?
a.isSuperset(of: b) // b ๊ฐ a์ ๋ถ๋ถ์งํฉ์ธ๊ฐ?
a.isStrictSubset(of: b) // a ๊ฐ b์ ์ง๋ถ๋ถ์งํฉ์ธ๊ฐ?
a.isStrictSuperSet(of: b) // b ๊ฐ a์ ์ง๋ถ๋ถ์งํฉ์ธ๊ฐ?
a.isDisjoint(with: b) // a ์ b ๊ฐ ์๋ก์์งํฉ์ธ๊ฐ?
ํน์ ํ์
์ ํค : ํน์ ํ์
์ ๊ฐ
set ์ด hashable ํด์ผํ๋ ๊ฒ ์ฒ๋ผ dict ์ key ๊ฐ๋ hashable ํด์ผํ๋ค.
ํค๋ฅผ ์ด์ฉํด ๊ฐ์ ์ฐพ์ ๋ ๋์ ๋๋ฆฌ ์ฌ์ฉ.
var dict = Dictionary<Key, Value>()
var dict = [Key: Value]()
var airports: [String: String] = [<"YYZ": "Toronto Pearson", "DUB": "Dublin", "ICN": "Incheon", "CJU": "Jeju"]
airports.isEmpty // false
airports["LHR"] = "London" // ์ด๋ฏธ ์๋ ํค์ ๊ฐ์ ํ ๋นํ๊ฑฐ๋ ์๋ก์ด ํค์ ๊ฐ์ ๋ฃ์ ์ ์์.
// func updateValue(newValue, forKey: key) -> oldValue?
// ๋ง์ฝ ๋์
๋๋ฆฌ์ ํค๊ฐ ์์ผ๋ฉด nil์ ๋ฐํ.
if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
print("The old value for DUB was \(oldValue).")
}
airports["CJU"] = nil // ๋์
๋๋ฆฌ ๋ด์ ๊ฐ์ ์ญ์ .
airports.removeValue(forKey: "DUB")
for (airportCode, airportName) in airports {
// key, value ๋ก ์ํ ๊ฐ๋ฅ.
}
// key ๋ value ๋ง ๋ฐ๋ก ๋ฝ์์ ๋ฐฐ์ด๋ก ์ฌ์ฉํ ์ ์๋ค.
let airportCodes = [String](airports.keys)
let airportNames = [String](airports.values)
let arr = [1, 2, 2, 3, 3, 3]
print(arr.shuffled())
print(arr.randomElement())
let dict = [1: "ํ์ฐ", 2: "ํฐํ๋", 3: "์ํ"]
print(dict.rabdomElement())
let s: Set<String> = [1, 2, 3, 4, 5]
print(s.randomElement())
์ค์ ๊ฐ๋ฐํ ๋๋ ๋ง์ด ์์ผ๋๋ฐ, ์ฝํ ํ ๋ ๋ง์ด ํธ๋ ๋๋์ ์ฑํฐ์๋ค.