Deep_Dev
article thumbnail
[Swift] stride ( for๋ฌธ ์ผ์ • ์ˆซ์ž๋งŒํผ ์ฆ๊ฐ€ )

๐Ÿ“š stride for๋ฌธ ์ผ์ • ์ˆซ์ž๋งŒํผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ• stride ๋ฐ˜๋ชฉ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋‹ค ๋ณด๋ฉด 1์”ฉ ์ฆ๊ฐ€๊ฐ€ ์•„๋‹Œ 2 ํ˜น์€ 3 ๋“ฑ ์›ํ•˜๋Š” ์ˆซ์ž๋งŒํผ ์ฆ๊ฐ€์‹œ์ผœ์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ๊ทธ ๋•Œ, stride๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธํ˜•ํƒœ 2๊ฐ€์ง€ stride(from: , to: , by: ) " 3 6 9 "๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ถœ๋ ฅ 12 ์ถœ๋ ฅ X stride(from: , through: , by: ) " 3 6 9 12 "๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ถœ๋ ฅ ๐Ÿ’ก ์ฆ‰, to๋Š” to์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๋ฒ”์œ„์ด๊ณ , through๋Š” through์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์„ ํฌํ•จํ•œ ๋ฒ”์œ„์ด๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์˜ˆ์‹œ ๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/181886 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”..

[Swift] enumerated ( ๋ฐฐ์—ด์˜ index ๊ฐ’ )

๐Ÿ“š enumerated ๋ฐฐ์—ด์˜ index๊ฐ’ ์ฐพ๊ธฐ enumerated ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๊ทธ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๊ฐ’์„ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ enumerated()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค. ์˜ˆ์‹œ var array : [String] = ["A","B","C","D","E'] for (index, value) in arr.enumerated() { print("\index) : \(value)" } // 0 : A // 1 : B // 2 : C // 3 : D // 4 : E ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ˆ์‹œ ๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/181886 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋ก..

article thumbnail
[Swift] ์™œ Swift์—์„œ ๋ฌธ์ž์—ด์€ index๋กœ ์กฐํšŒํ•˜์ง€ ๋ชปํ• ๊นŒ?

๐Ÿ“š Swift ์—์„œ ๋ฌธ์ž์—ด index ์ ‘๊ทผ์— ๋Œ€ํ•˜์—ฌ Swift ์—์„œ ๋ฌธ์ž์—ด์€ ์–ด๋–ป๊ฒŒ ์กฐํšŒํ• ๊นŒ ? swift์—์„œ ๋ฌธ์ž์—ด์„ ์ •์ˆ˜ index๋กœ ์กฐํšŒํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด Swift์—์„œ๋Š” ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์กฐํšŒํ•ด์•ผ ํ• ๊นŒ ? String.Index๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ ‘๊ทผํ•œ๋‹ค. // ์„ ์–ธ let str = "hello world!" // ์กฐํšŒ str[str.startIndex] // 'h' str[str.endIndex] // '!'๊ฐ€ ๋‚˜์˜ฌ์ง€ ์•Œ์•˜๊ฒ ์ง€๋งŒ ์—๋Ÿฌ ๋ฐœ์ƒ var idx = str.index(str.startIndex, offsetBy: 6) str[idx] // 'w' // ๋ฐ˜๋ณต๋ฌธ์„ ์ธ๋ฑ์Šค์ฒ˜๋Ÿผ ๋ฝ‘์•„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด indicies ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ for i in str.indices { print(str[i]) // ..

article thumbnail
[Swift] ReplacingOccurrences ( ๋ฌธ์ž์—ด ์น˜ํ™˜ )

๐Ÿ“š ๋ฌธ์ž์—ด ๋‚ด ํŠน์ •๋ฌธ์ž ๋ณ€๊ฒฝ (์น˜ํ™˜) ReplacingOccurrences ๋ฌธ์ž์—ด์„ ๋Œ€์ฒดํ•˜์—ฌ ์ƒˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. - of target : ๋ฐ”๊ฟ€ ๋ฌธ์ž์—ด - with replacement : ๋Œ€์ฒดํ•  ๋ฌธ์ž์—ด ์‚ฌ์šฉ์˜ˆ์‹œ let str = "Bye Nice to meet you" let result = str.replacingOccurrences(of: "Bye", with: "Hello") print(result) // "Hello Nice to meet you" of: ์— ์žˆ๋Š” "Bye"๋ฅผ "Hello"๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค.

article thumbnail
[Swift] Split , Components

๐Ÿ“š ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ split components Split split๋ฉ”์„œ๋“œ๋Š” ๋‹ค์–‘ํ•œ parameter๋ฅผ ์ง€์›ํ•œ๋‹ค. ๊ฐ๊ฐ์˜ ์—ญํ• ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. - separater : ์ชผ๊ฐœ๋ ค๋Š” ๋ฌธ์ž ๋‹จ์œ„ - maxSplits : ์ง€์ •ํ•œ ๋ฌธ์ž ๋‹จ์œ„๋กœ ์–ผ๋งˆ๋‚˜ ์ชผ๊ฐค์ง€ - omittingEmptySubsequences : Bool๊ฐ’์œผ๋กœ ๊ฒฐ๊ณผ๊ฐ’์—์„œ ๋นˆ ์‹œํ€€์Šค์˜ ํฌํ•จ ์œ ๋ฌด๋ฅผ ์„ค์ • ์ฐธ๊ณ  : https://zeddios.tistory.com/232 split์€ ๋ง ๊ทธ๋Œ€๋กœ ๋ฌธ์ž์—ด์„ ํŠน์ • ๋ฌธ์ž ๊ธฐ์ค€์œผ๋กœ ์ชผ๊ฐค ์ˆ˜ ์žˆ๋‹ค. ๋ฐ˜ํ™˜ํ˜•์€ Stringํƒ€์ž…์„ [Substring]์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค. ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜๋ฐ›๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋“ค์–ด๊ฐ€์žˆ๋‹ค. ๋•Œ๋ฌธ์— ๋ฐ”๋กœ import Foundation์„ ํ•  ํ•„์š”์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. let ..

[Swift] Map, Filter, Reduce

๐Ÿ“š ๊ณ ์ฐจํ•จ์ˆ˜ Map Filter Reduce ๊ณ ์ฐจํ•จ์ˆ˜๋ž€ ? ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ์ธ์ž๋กœ ๋ฐ›๊ฑฐ๋‚˜ ํ•จ์ˆ˜์‹คํ–‰์˜ ๊ฒฐ๊ณผ๋ฅผ ํ•จ์ˆ˜๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ ( = ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•จ์ˆ˜๋ฅผ ๊ฐ–๋Š” ํ•จ์ˆ˜ ) Map ( ๋ณ€ํ˜• ) ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ˜•ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ˜•ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ, ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋Š” ๋ณ€ํ˜•๋˜์ง€ ์•Š๋Š”๋‹ค. Map์€ ๊ธฐ์กด์˜ for-in ๊ตฌ๋ฌธ๊ณผ ํฐ ์ฐจ์ด๊ฐ€ ์—†์ง€๋งŒ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์ ์ด ์žˆ๋‹ค. ์ฝ”๋“œ์˜ ๊ฐ„๊ฒฐ์„ฑ ์žฌ์‚ฌ์šฉ ์šฉ์ด ์ปดํŒŒ์ผ๋Ÿฌ ์ตœ์ ํ™” ์„ฑ๋Šฅ ์ข‹์Œ map์˜ ๊ธฐ๋ณธ ํ˜•ํƒœ array.map(transform:T throws -> T) Tํƒ€์ž…์˜ transform์„ ๋ฐ›์•„ ์ƒˆ๋กœ์šด Txใ…์ž…์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๋Š” ๋ง์ด๋‹ค. * ๊ธฐ์กด for - in ๊ตฌ๋ฌธ let numArray = [1,3,5,7,9] var multiArra..

article thumbnail
[Swift] prefix suffix

๐Ÿ“š prefix suffix prefix ์ปฌ๋ ‰์…˜์˜ ์ดˆ๊ธฐ ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ง€์ •๋œ ์ตœ๋Œ€ ๊ธธ์ด๊นŒ์ง€ ํ•˜์œ„ ์‹œํ€€์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ ์š”์†Œ ์ˆ˜๋ฅผ ์ดˆ๊ณผํ•˜๋Š” ๊ฒฝ์šฐ ๊ฒฐ๊ณผ์—๋Š” ์ปฌ๋ ‰์…˜์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํฌํ•จ๋œ๋‹ค. let numbers = [1, 2, 3, 4, 5] print(numbers.prefix(2)) // Prints "[1, 2]" print(numbers.prefix(10)) // Prints "[1, 2, 3, 4, 5]" ๊ด€๋ จ ๋ฌธ์ œ prefix ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‘ผ๋‹ค๋ฉด ? import Foundation func solution(_ my_string:String, _ n:Int) -> String { return String(my_string.prefix(n)) } ๐Ÿ’ก my_string:prefix(n) ์•ž์—..

article thumbnail
[Swift] String ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜ ์ •๋ฆฌ

๐Ÿ“š String ๊ตฌ์กฐ์ฒด์ด๊ณ  ๋‹ค๋ฅธ ์–ธ์–ด์™€ ๊ฐ™์ด character๊ฐ€ ๋ชจ์ธ ์ง‘ํ•ฉ Array() : ๋ฌธ์ž์—ด -> ๋ฐฐ์—ด ๋ฌธ์ž์—ด์˜ ์š”์†Œ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•. let name: String = "SWIFT" let arrName = Array(name) print(arrName) // ["S","W","I","F","T"] count() , empty() : ๋ฌธ์ž์—ด์˜ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ let string: "A Unicode string value that is a collection of characters." print(string.count) // 58 let empty = "" print(empty.isEmpty) // true print(string.isEmpty) // false append() : ..