[Swift๐Ÿฆฉ] #3 ๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž, #4 ์ฝœ๋ ‰์…˜ ํƒ€์ž…

๋˜์ƒยท2022๋…„ 3์›” 21์ผ
0

iOS

๋ชฉ๋ก ๋ณด๊ธฐ
17/42
post-thumbnail

3. ๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž

String, Character

// Chararcter ๋ฐฐ์—ด -> String
let catCharacters: [Character] = ["C", "a", "t", "!", "๐Ÿฑ"]
let catString = String(catCharacters)

Foundation ์˜ NSString ๊ณผ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์„œ, bridging ํ•˜์—ฌ NSString์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

1. ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด, isEmpty

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) ์ถœ๋ ฅ ๊ฐ€๋Šฅ")

2. ์—ฐ์‚ฐ

"๋ฌธ์ž์—ด" + "๋ฌธ์ž์—ด"

var s = "๋ฌธ์ž์—ด"
s += "์—ฐ์‚ฐ"

3. value type

๋ฌธ์ž์—ด์€ ๊ฐ’ ํƒ€์ž…์ด๋ฏ€๋กœ ํ•จ์ˆ˜์— ์ธ์ž๋กœ ์ „๋‹ฌ๋  ๋•Œ, ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜์— ํ• ๋‹น๋  ๋•Œ ๋ณต์‚ฌ๋œ๋‹ค.

4. for ๋ฌธ

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])

5. Index, insert, remove โญ๏ธโญ๏ธ

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)

6. Substring โญ๏ธ

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 ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ณต์œ ํ•ด์„œ ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€, ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ๋”ฐ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋ณต์‚ฌํ•ด์„œ ์ƒˆ๋กœ์šด ์žฅ์†Œ์— ์ €์žฅํ•œ๋‹ค.

7. ๋น„๊ต (== != hasPrefix hasSuffix)

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
  • UTF-8, UTF-16, Unicode scala ํ‘œํ˜„.

๐Ÿ“• uppercased(), lowercased()

ํŒŒ์ด์ฌ์˜ upper, lower ์™€ ๊ฐ™์Œ.

"ABCD".lowercased()
"Asdf".uppercased()

๐Ÿ“• ํŠน์ˆ˜๋ฌธ์ž

"์ค„๋ฐ”๊ฟˆ\n ๋ฐฑ์Šฌ๋ž˜์‹œ \\" // ์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ,
print(#"๋ฐฑ์Šฌ๋ž˜์‹œ \\n\\"#) // # ์„ ์•ž๋’ค๋กœ ๋ถ™์ด๋ฉด ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์•ˆ์”€. ์ง„์งœ ๊ณ ๋Œ€๋กœ ๋‚˜์˜ด.
let number: Int = 100
#"๋ฐฑ์Šฌ๋ž˜์‹œ \#(number)"# // # ์„ ์•ž๋’ค๋กœ ๋ถ™์ด๋ฉด ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์•ˆ์”€.



4. ์ฝœ๋ ‰์…˜ ํƒ€์ž…

  • ๋ฐฐ์—ด (Array) : ์ฝœ๋ ‰์…˜ ๊ฐ’์ด๋ฉฐ ์ˆœ์„œ๋ฅผ ๊ฐ€์ง.
  • ์ง‘ํ•ฉ (Set) : ์ค‘๋ณต์ด ์—†๋Š” ๊ฐ’์—, ์ˆœ์„œ๊ฐ€ ์—†๋Š” ์ฝœ๋ ‰์…˜ ํƒ€์ž….
  • ๋”•์…”๋„ˆ๋ฆฌ (Dictionary) : key-value ์Œ์˜ ์ˆœ์„œ๊ฐ€ ์—†๋Š” ์ฝœ๋ ‰์…˜ ํƒ€์ž….
var arr: [String] = ["a", "b", "c"] // ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ - ์•„์ดํ…œ ์ถ”๊ฐ€, ์‚ญ์ œ, ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ.
let dict: [Int: String] = [1 : "one", 2 : "two", 3 : "three"] // ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ.

1. Array โญ๏ธ

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, ๋ฐฐ์—ด ์š”์†Œ ์Œ์œผ๋กœ ์ˆœํšŒ ๊ฐ€๋Šฅ.
}

2. Set

NSSet ๊ณผ ์—ฐ๊ฒฐ.

  • ์ง‘ํ•ฉ์— ๊ฐ’์„ ์ €์žฅํ•˜๋ ค๋ฉด ๊ฐ’์˜ ํƒ€์ž…์ด hashable ์ด์–ด์•ผ ํ•œ๋‹ค. (ํ•ด์‹œ๊ฐ’์ด ๊ฐ™์œผ๋ฉด ํ•ด๋‹น ๊ฐ’๋„ ๊ฐ™๊ณ , ํ•ด์‹œ๊ฐ’์ด ๋‹ค๋ฅด๋ฉด ๊ฐ’๋„ ๋‹ค๋ฆ„)
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 ๊ฐ€ ์„œ๋กœ์†Œ์ง‘ํ•ฉ์ธ๊ฐ€?

3. Dictionary โญ๏ธ

ํŠน์ • ํƒ€์ž…์˜ ํ‚ค : ํŠน์ • ํƒ€์ž…์˜ ๊ฐ’
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)

๐Ÿ“• randomElement(), shuffled()

  • ๋ฐฐ์—ด์€ shuffle ๋กœ ์„ž์„ ์ˆ˜ ์žˆ๊ณ 
  • randomElement ๋Š” ๋ฐฐ์—ด, ๋”•์…”๋„ˆ๋ฆฌ, ์ง‘ํ•ฉ ๋ชจ๋‘์— ์‚ฌ์šฉ ๊ฐ€๋Šฅ!
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())

์‹ค์ œ ๊ฐœ๋ฐœํ•  ๋•Œ๋Š” ๋งŽ์ด ์•ˆ์ผ๋Š”๋ฐ, ์ฝ”ํ…Œ ํ’€ ๋•Œ ๋งŽ์ด ํ‘ธ๋Š” ๋Š๋‚Œ์˜ ์ฑ•ํ„ฐ์˜€๋‹ค.

profile
0๋…„์ฐจ iOS ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€