ungchun๋์ ๋ฒจ๋ก๊ทธ๋ฅผ ์ฐธ์กฐํ๋ฉด์ ์ ๋ํ ์ฝํ ์ ์ค์ํํธ๋ฅผ ์ค๋นํ๋ฉด์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฒ๋ค์ ์ ๋ฆฌํด๋ณด๋ คํฉ๋๋ค.
[iOS] swift ์๊ณ ๋ฆฌ์ฆ, ์ฝ๋ฉํ ์คํธ์ ํ์ํ tip ์ ๋ฆฌ
swift ์๊ณ ๋ฆฌ์ฆ, ์ฝ๋ฉํ ์คํธ tip ์ ๋ฆฌ
velog.io
๊ธฐ๋ณธ ์ ๋ ฅ ๋ฐ๊ธฐ
// return String
var input = readLine()!
// return Int
var input = Int(readLine()!)!
"1 2 3 4 5"์ ๊ฐ์ ๊ณต๋ฐฑ ์๋ ์ซ์ ์ ๋ ฅ ๋ฐ๊ธฐ
// split - return: [SubString]
var nums = readLine()!.split(separator: " ").map {Int($0)!}
// components - return: [String] -> import Foundation ํ์
var nums = readLine()!.components(separatedBy: " ").map {Int($0)!}
// FileIO, ์
๋ ฅ๋ฐ์ผ๋ฉด์ ๋ฆฌ์คํธ์ ๋ฐ๋ก ์ถ๊ฐํ๊ธฐ
array.append((file.readInt(), file.readInt()))
"12345"์ ๊ฐ์ ๊ณต๋ฐฑ ์๋ ์ซ์๋ฅผ ๋ฐฐ์ด๋ก ์ ๋ ฅ ๋ฐ๊ธฐ
// Int
var nums = Array(readLine()!).map {Int(String($0))!}
// String
var nums = Array(readLine()!).map {String($0)}
// FileIO
let num = [file.readInt(), file.readInt()]
์ ๋ ฅ๊ฐ ์๊ฐ์ด๊ณผ -> fread์ swift ๋ฒ์ (๋ผ์ด๋ ธ๋ FilelO)
https://gist.github.com/JCSooHwanCho/30be4b669321e7a135b84a1e9b075f88
psํ ๋ ์ ๋ ฅ์ ํ๊บผ๋ฒ์ ๋ฐ๊ธฐ ์ํ ์ ํธ๋ฆฌํฐ ํด๋์ค. fread์ swift ๋ฒ์ .
psํ ๋ ์ ๋ ฅ์ ํ๊บผ๋ฒ์ ๋ฐ๊ธฐ ์ํ ์ ํธ๋ฆฌํฐ ํด๋์ค. fread์ swift ๋ฒ์ . GitHub Gist: instantly share code, notes, and snippets.
gist.github.com
์ ์ถ๋ ฅ์ด 10~20๋ง์ค์ด ๋์ด๊ฐ๋ฉด ์ฐ๋ผ๋๋ฐ ๊ทธ๋ฐ ๊ฒฝ์ฐ๊ฐ ์๋๋ด์ฌ ใทใท ๋์ค์ ์ ๋ ๊ฒฝํํ๋ฉด ์จ๋ณด๊ฒ ์ต๋๋ค.
xcode ํฐ๋ฏธ๋์์ enter๊ฐ ์๋ eof๋ฅผ ์ ๋ ฅํด์ผ ์ ๋ ฅ์๋ฃ๊ฐ ๋ฉ๋๋ค.
ํฐ๋ฏธ๋์์ ๊ฐ์ ์ ๋ ฅํ ๋ค ์ ์ผ ๋ง์ง๋ง์ ctrl+d๋ฅผ ์ ๋ ฅํ๋ฉด ์ถ๋ ฅ๊ฐ์ด ๋์ต๋๋ค.
prefix / suffix
let strData = "hello์ ๋ก!!"
// [prefix: ์์ ๊ธฐ์ค์ผ๋ก ์ง์ ๋ ๋ฌธ์ ๊ฐ์ ์ถ๋ ฅ]
let startData = strData.prefix(5)
print(startData) // hello
// [suffix: ์ข
๋ฃ ๊ธฐ์ค์ผ๋ก ์ง์ ํ ๋ฌธ์ ๊ฐ์ ์ถ๋ ฅ]
let endData = strData.suffix(4)
print(endData) // ์ ๋ก!!
let numbers = [1, 2, 3, 4, 5]
print(numbers.prefix(2))
// Prints "[1, 2]"
print(numbers.prefix(10))
// Prints "[1, 2, 3, 4, 5]"
let numbers = [1, 2, 3, 4, 5]
print(numbers.suffix(2))
// Print "[4, 5]"
print(numbers.suffix(10))
// Prints "[1, 2, 3, 4, 5]"
split(whereSeparator: {})
// ๊ธฐ์ค์ด ๋๋ ๋ฌธ์๋ฅผ ์ ์ธํ ์์๋ฅผ ๋ฐฐ์ด๋ก ๋ง๋ค์ด ๋ฆฌํด
let dartResult = "1S2D*3T"
var scores = dartResult.split(whereSeparator: {!$0.isNumber}).map {Int($0)}
print(scores)
// Prints "[1, 2, 3]"
let letters = dartResult.split(weherSeparator: {$0.isNumber})
print(letters)
// Prints "["S", "D*", "T"]"
subString
// 3๋ฒ์งธ๋ถํฐ ๋๊น์ง
let startIdx: String.Index = str.index(str.startIndex, offsetBy: 3)
var result = String(str[startIdx...])
// ์ฒ์๋ถํฐ 3๋ฒ์งธ๊น์ง
let endIdx: String.Index = str.index(str.startIndex, offsetBy: 3)
var result = String(str[...endIdx])
๋ฌธ์์ด ๋ฐ๋ณต
let str = String(repeating: "ใ
", count: 10)
print(str) // "ใ
ใ
ใ
ใ
ใ
ใ
ใ
ใ
ใ
ใ
"
๋ฌธ์์ด์ ํน์ ๋ฌธ์ ๋ฐ๊พธ๊ธฐ
// import Foundation ํ์
let str = "ze!ro!"
let str2 = str.replacingOccurrences(of: "!", with: "?")
print(str2) // "ze?ro?"
๊ฐํ์๋ print
print("Hello", terminator: "")
forEach
let nums: [Int] = [1, 2, 3, 4]
nums.forEach {
print($0) // 1 2 3 4
}
stride
// 3 6 9
for i in stride(from: 3, to: 12, by: 3) {
print(i)
}
// 3 6 9 12
for i in stride(from 3, through 12, by: 3) {
print(i)
}
์ ๋๊ฐ
// 11
abs(-11)
์ ๊ณฑ
let value = 3.0
// 9.0
pow(value, 2)
์ ๊ณฑ๊ทผ ๊ตฌํ๊ธฐ
let value = 9.0
// 3.0
sqrt(value)
์๋ฆฟ์ ๋ํ๊ธฐ
// n = 123, return = 6
String(n).map{String($0)}.reduce(0){$0 + Int($1)!}
๋ฐฐ์ด ์์์ ์ ๊ทผํ๊ธฐ
var array = [1, 2, 3, 4, 5]
// [2, 3, 4]
array[1...3]
๋ฐฐ์ด์ ์์ ์ถ๊ฐํ๊ธฐ
var array = [1, 2, 3]
array.append(4) // [1, 2, 3, 4]
array.append(contentsOf: [5, 6, 7] // [1, 2, 3, 4, 5, 6, 7]
๋ฐฐ์ด์ ์ํ๋ ๊ฐ index ์ฐพ์์ ์ญ์ ํ๊ธฐ
if let index = array.firstIndex(where: {$0 == value}) {
array.remove(at: index)
}
1์ฐจ์ ๋ฐฐ์ด ์ ์ธ
let arr = [Int]()
let arr = Array(repeating: 0, count: 5)
let arr = [Int](repeating: 0, count: 5)
2์ฐจ์ ๋ฐฐ์ด ์ ์ธ
let arr = [[Int]]()
let arr = Array(repeating: Array(repeating:0, count: 3), count: 5)
let arr = [[Int]](repeating: [Int](repeating: 0, count: 3), count: 5)
map
var string = ["1", "2", "3", "4", "5"]
// [1, 2, 3, 4] ๊ฐ ์์๋ฅผ ์ ๋ถ Int๋ก ๋งตํ
string.map { Int($0)! }
filter
var array = [1, 2, 3, 4]
// [2, 4] ์กฐ๊ฑด์ ๋ง๋ ์๋ง ๋ฝ์๋
array.filter { $0 % 2 == 0 }
reduce
// ์ด๊ธฐ๊ฐ์ด 3์ด๊ณ ์ ์ ๋ฐฐ์ด์ ๋ชจ๋ ๊ฐ์ ๋ํ๋ ์ฝ๋
let numbers: [Int] = [1, 2, 3]
var sumFromThree: Int = numbers.reduce(3) {
print("\($0) + \($1)")
// 3 + 1
// 4 + 2
// 6 + 3
return $0 + $1
}
print(sumFromThree) // 9
// ๊ฐ๋จํ๊ฒ ์ด๋ ๊ฒ๋ ๊ฐ๋ฅํจ
var array = [1, 2, 3, 4]
array.reduce(0, +)
// ๋ฌธ์์ด ํฉ์น๊ธฐ๋ joined()๋ฅผ ์ฌ์ฉ. reduce๊ฐ ํจ์ฌ ๋๋ฆผ
joined()
joined๋ ๋ฐฐ์ด์ ๋ฌธ์์ด๋ค์ ํ๋์ ๋ฌธ์์ด๋ก ๋ฐํํ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด์ ์์๋ค์ด ๋ฌธ์์ด์ด์ฌ์ผ ํฉ๋๋ค
var brands = ["Dell", "HP", "Apple"]
// DellHPApple
var result1 = brands.joined()
// Dell HP Apple
var result2 = brands.joined(separator: " ")
// Dell, HP, Apple
var result3 = brands.joined(separator: ", ")
// ๋ง์ฝ ๋ฌธ์์ด์ด ์๋๋ผ๋ฉด ๋ฌธ์์ด๋ก ๋ฐ๊พธ๊ณ ์งํํด์ผํจ
var result: [Int] = [1, 2, 3, 4]
result.map{String($0)}.joined() // [1, 2, 3, 4] -> 1234
result.map{String($0)).joined(separator: ", ") // [1, 2, 3, 4] -> 1, 2, 3, 4
flatMap(), compactMap()
let array1 = [1, nil, 3, nil, 5, 6, 7]
let flatMapTest1 = array1.flatMap { $0 }
let compactMapTest1 = array1.compactMap { $0 }
// flatMapTest1 : [1, 3, 5, 6, 7]
// compactMapTest1 : [1, 3, 5, 6, 7]
- 1์ฐจ์ ๋ฐฐ์ด์์๋ ๋ ๋ค ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ๋ํ๋ด์ง๋ง
- Swift 4.1 ๋ถํฐ๋ 1์ฐจ์ ๋ฐฐ์ด์์ nil์ ์ ๊ฑฐํ๊ณ ์ต์ ๋ ๋ฐ์ธ๋ฉ์ ํ๊ณ ์ถ์๋๋ flatMap๋ง๊ณ compactMap์ ์ฌ์ฉํ๋ผ๊ณ ํ๋ค.
let array2: [[Int?]] = [[1, 2, 3], [nil, 5], [6, nil], [nil, nil]]
let flatMapTest2 = array2.flatMap { $0 }
let compactMapTest2 = array2.compactMap { $0 }
// flatMapTest2 : [Optional(1), Optional(2), Optional(3), nil, Optional(5), Optional(6), nil, nil, nil]
// compactMapTest2 : [[Optional(1), Optional(2), Optional(3)], [nil, Optional(5)], [Optional(6), nil], [nil, nil]]
- flatMap๊ณผ compactMap์ nil์ ์ ๊ฑฐํ์ง ์๊ณ 1์ฐจ์ ๋ฐฐ์ด์ผ๋๋ง nil์ ๊ฑฐ
- flatMap์ 2์ฐจ์ ๋ฐฐ์ด์ 1์ฐจ์ ๋ฐฐ์ด๋ก flattern ํ๊ฒ ๋ง๋ค์ด์ฃผ๋ ๋ฐ๋ฉด,
- compactMap์ 1์ฐจ์ ๋ฐฐ์ด๋ก ๋ง๋ค์ง ์์ต๋๋ค.
- 2์ฐจ์ ๋ฐฐ์ด์ 1์ฐจ์์ผ๋ก ๋ง๋ค๋๋ flatMap ์ฌ์ฉ
let array2: [[Int?]] = [[1, 2, 3], [nil, 5], [6, nil], [nil, nil]]
let flatMapTest2 = array2.flatMap { $0}.compactMap { $0 }
// flatMapTest2 : [1, 2, 3, 5, 6]
- flatMap์ผ๋ก flatternํ๊ฒ ๋ง๋ค๊ณ compactMap์ผ๋ก ์ฒ๋ฆฌํ๋ฉด ํธํ๊ฒ 2์ฐจ์ ๋ฐฐ์ด์ ์ต์ ๋ ๋ฐ์ธ๋ฉํ 1์ฐจ์ ๋ฐฐ์ด๋ก ์ถ๋ ฅ๊ฐ๋ฅ
let array3 = [[[1], [2, 3], [4, 5, 6], [7, 8, 9, 10]]]
let flatMapTest3 = array3.flatMap { $0 }
// [[1], [2, 3], [4, 5 ,6], [7, 8, 9, 10]]
let doubleflatMapTest3 = flatMapTest3.flatMap { $0 }
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- 3์ฐจ์ ๋ฐฐ์ด๋ flatMap 2๋ฒ ์ฌ์ฉํ๋ฉด flattern ํ๊ฒ ์ฒ๋ฆฌ ๊ฐ๋ฅ
Set
- ์ ๋ ฌ๋์ง ์์ ์ปฌ๋ ์
- ์ค๋ณต ํ์ฉ X
- ํด์๋ฅผ ํตํด ๊ฐ์ ์ ์ฅํด์ ๋ฐฐ์ด์ ๋นํด ๊ฒ์ ์๋๊ฐ ๋น ๋ฅด๋ค.
- ์ ์ฅ๋๋ ์๋ฃํ์ Hashable ํ๋กํ ์ฝ์ ์ค์ํด์ผํจ
// ์์ฑ
var tempSet: Set<Int> = []
var tempSet: Set<Int>()
var tempSet: Set<Int> = [1, 2, 5, 0]
let count: Int = tempSet.count // Set ๊ฐ์ ํ์ธ: 4
let isEmpty: Bool = tempSet.isEmpty // Set ๋น์ด์๋์ง ํ์ธ: false
tempSet.contains(1) // true
tempSet.contains(10) // true
// insert: ๊ฐ์ ์ถ๊ฐํ๊ณ , ์ถ๊ฐ๋ ๊ฒฐ๊ณผ๋ฅผ ํํ๋ก ๋ฆฌํด (์ค๋ณต์ด๋ฉด false, ์ถ๊ฐ๋ ๊ฐ)
tempSet.insert(1) // (false, 1)
tempSet.insert(10) // (true, 10)
// update: ๊ฐ์ด ์กด์ฌํ์ง ์์ผ๋ฉด ์ถ๊ฐ ํ nil ๋ฆฌํด, ์กด์ฌํ ๊ฒฝ์ฐ ๋ฎ์ด์ฐ๊ธฐ ํ ๋ฎ์ด์ฐ๊ธฐ ์ ๊ฐ ๋ฆฌํด
tempSet.update(with: 1) // Optional(1)
tempSet.update(with: 120) // nil
// remove: ํ๊ฐ์ง ์์ ์ญ์ ํ ๋ ์ฌ์ฉ, ์ญ์ ํ ์ญ์ ํ ๊ฐ return (์๋ ์์ ์ญ์ ์ nil ๋ฆฌํด)
tempSet.remove(1) // Optional(1)
tempSet.remove(10) // nil
// removeAll(): ์ ์ฒด ์์ ์ญ์
tempSet.removeAll()
dictionary
// ์์ฑ
var dict: [String: Int] = [:]
var dict = [String: Int]()
// ๊ฐ์ ํ์ธ
let count: Int = dict.count
let isEmpty: Bool = dict.isEmpty
var dict = ["height": 165, "age": 100]
let height = dict["height"] // Optional(165)
let weight = dict["weight"] // nil
dict["weight"] = 100 // ํด๋น Key๊ฐ ์๋ค๋ฉด, ์ถ๊ฐ (insert)
dict["weight"] = 200 // ํด๋น Key๊ฐ ์๋ค๋ฉด, Value ๋ฎ์ด์ฐ๊ธฐ (update)
dict["weight"] = nil // ํด๋น Key๊ฐ ์์ด๋ ์๋ฌ ์๋จ
dict["weight"] = nil // ํด๋น Key๊ฐ ์๋ค๋ฉด, ํด๋น Key-Value ์ญ์
dict.removeAll() // ์ ์ฒด ์ญ์
dict.keys // "height", "age"
dict.keys.sorted() // "age", "height"
dict.values // 165, 100
dict.values.sorted() // 100, 165
dict.sorted(by: {$0.value > $1.value})
dict.filter(condition) // ํด๋น ํด๋ก์ ๋ฅผ ๋ง์กฑํ๋ ์์๋ง ๋ชจ์์ ์ ๋์
๋๋ฆฌ๋ก ๋ฆฌํด
var v = [[Int]]()
var xDic = [Int: Int]()
xDic[v[i][0]]
let x = xDic.filter { $0.value < 2 }.keys
ans.append(contentsOf: xDic.values.filter { $0 < 2 })
2์ฐจ์ ๋ฐฐ์ด ์ ๋ ฌํ๊ธฐ
var arr = [[0, 3], [1, 9], [2, 6]]
let sortedArray = arr.sorted(by: { $0[1] < $1[1] }) // ๊ฐ ๋ฐฐ์ด ๋๋ฒ์งธ ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์
let sortedArray = arr.sorted(by: { $0.1 < $1.1 }) // ๊ฐ์ ๊ฒฐ๊ณผ
print(sortedArray) // [[0, 3], [2, 6], [1, 9]]
init (repeating: count:)
// ์๋ฐ์๋ฐ์๋ฐ
let watermelon4 = String(repeating: "์๋ฐ", count: 4)
print(watermelon4)
// [false, false, false, false, false]
let liar = Array(repeating: false, count: 5)
print(liar)
- ๋ฐฐ์ด์์ ๋ด๊ธด ์ซ์๋งํผ ์ผ์ผ์ด printํ๋ฉด ๋งค๋ฒ printํ๋ ๊ฒ๋ ๋๋ฆฌ๊ธฐ ๋๋ฌธ์ ์๊ฐ์ด๊ณผ ๋ฐ์
- String - init (repeating: count: ) ๋ก ํ๋ฒ์ ๋ง๋ค์ด์ String์ ์ ์ฅํด์ ์ถ๋ ฅํ๋ฉด ํด๊ฒฐํ ์์๋ค
var answer = ""
// \n ์ด์ฉํด์ ํ ์ค์ ํ๋์ฉ ์ถ๋ ฅ
answer += String(repeating: "\(value)\n", count: countValue)
// ๊ณต๋ฐฑ ์ด์ฉํด์ ํ ์ค์ ๋์ด์ ์ถ๋ ฅ
answer += String(repeating: "\(value)", count: countValue)
// ํ ์ค์ ๋ถ์ฌ์ ์ถ๋ ฅ
answer += String(repeating: "\(value)", count" countValue)
print(answer)โ
์ต๋๊ณต์ฝ์, ์ต์๊ณต๋ฐฐ์
// ์ต๋๊ณต์ฝ์
func GCD(_ a: Int, _ b: Int) -> Int {
let mod: Int = a % b
return 0 == mod ? min(a, b) : GCD(b, mod)
}
// ์ต์๊ณต๋ฐฐ์
func LCM(_ a: Int, _ b: Int) -> Int {
return a * b / GCD(a, b)
}
heap / ์ฐ์ ์์ ํ
Swift์ ํ์ด ์๋ค๊ณ ํ๋ค.... ์คํ, ํ, ์กฐํฉ ๋ฑ๋ฑ ์๋ค๊ณ ํ๋ค...
https://developer-p.tistory.com/190
์ค์ํํธ | "(์ต์)ํ ๊ตฌํ"ํ๊ธฐ (Swift5 | Heap - MinHeap)
์ฝํ ์๊ณ ๋ฆฌ์ฆ ๊ณต๋ถ๋ฅผ ํ๋ ์ค, ๋ฐฑ์ค 1927๋ฒ์ ํ๊ธฐ ์ํด์ ์ต์ํ(MinHeap) ๊ตฌํ์ด ํ์ํ์ต๋๋ค. Swift์ ํ์ด ์์ต๋๋ค. ๋ฌผ๋ก ํ, ๋ฑ, ์์ด, ์กฐํฉ ๋ฑ๋ฑ ๋ค ์์ต๋๋ค... ์ต๋ ํ์ ๋ํด์ ์๋ ๋งํฌ์
developer-p.tistory.com
์์ด๊ณผ ์กฐํฉ
์ด๊ฑฐ๋ฅผ ์ธ์ผ์ด ๊ณง ์๊ธฐ๋ฉด ์์ ํด๋ณด๊ฒ ์ต๋๋ค...
https://velog.io/@qwer15417/Swift-%EC%88%9C%EC%97%B4%EA%B3%BC-%EC%A1%B0%ED%95%A9
[Swift] ์์ด๊ณผ ์กฐํฉ
swift๋ ์์ด๊ณผ ์กฐํฉ์ ์ง์ ๊ตฌํํด์ฃผ์ด์ผ ํ๋ค. ๋ฐ๋ผ์ ์ด๋ฒ ์๊ฐ์๋ ์์ด๊ณผ ์กฐํฉ์ Stack๊ณผ ์ฌ๊ท๋ฅผ ์ด์ฉํ์ฌ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด๋๋ก ํ์.
velog.io
'๐ Daily Loaf > ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ธ๋ก ์ฆ2][Swift]๋ฐฑ์ค10813 (0) | 2024.10.07 |
---|---|
[๋ธ๋ก ์ฆ3][Swift]๋ฐฑ์ค10810(Array) (0) | 2024.10.05 |
[๋ธ๋ก ์ฆ5][Swift]๋ฐฑ์ค10951(while/EOF) (1) | 2024.10.04 |
[๋ธ๋ก ์ฆ5][Swift]๋ฐฑ์ค10952(while) (0) | 2024.10.04 |
[๋ธ๋ก ์ฆ5][Swift]๋ฐฑ์ค10950(ํ ์คํธ์ผ์ด์ค) (0) | 2024.10.04 |
[๋ธ๋ก ์ฆ5][Swift]๋ฐฑ์ค2739(for) (0) | 2024.10.03 |
[๋ธ๋ก ์ฆ5][Swift]๋ฐฑ์ค1008 (0) | 2024.10.02 |
[๋ธ๋ก ์ฆ5][Swift]๋ฐฑ์ค1001 (1) | 2024.10.02 |