Focus On Develop ๐ŸคŸ๐ŸคŸ

[Swift] ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋”ฉ์‹œ ์œ ์šฉํ•œ Tip๋“ค! ๋ณธ๋ฌธ

iOS [Swift]/๊ธฐ์ดˆ๋ฅผ ํƒ„ํƒ„ํžˆ!

[Swift] ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋”ฉ์‹œ ์œ ์šฉํ•œ Tip๋“ค!

๋ˆ„๋ฆฌ๋‹ฌ์ดํ‹€ 2021. 1. 5. 17:32

์ด ํŽ˜์ด์ง€๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ ์ž์ฃผ(?) ์“ฐ์ด๋Š” ์Šคํ‚ฌ(?) ๊ฐ™์€ ๊ฒƒ๋“ค์„ ๊ฐ„๋žตํ•˜๊ฒŒ ์ •๋ฆฌํ•ด๋‘˜ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์Œ..? root๊ฐ’ ์–ด๋–ป๊ฒŒ ๊ตฌํ•˜์ง€.. ์ง„๋ฒ•๋ณ€ํ™˜ ์–ด๋–ป๊ฒŒ ํ•˜์ง€.. ๋“ฑ๋“ฑ! (๋งค๋ฒˆ ๊ตฌ๊ธ€๋งํ•˜๊ธฐ ๋„˜ ๊ท€์ฐฎ..๐Ÿ˜”) ์ •~๋ง ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉ๋ฒ•๋งŒ ์ •๋ฆฌํ•  ์˜ˆ์ •์ด๋ž๋‹ˆ๋‹ค ๐Ÿ˜Ž

๋ฌธ์ œ ํ’€๋ฉด์„œ ๋˜ ์ž์ฃผ ๋ณด์ด๋Š” ๊ฒƒ์€ ์ด ํŽ˜์ด์ง€์— ์ถ”๊ฐ€ํ•ด๋‚˜๊ฐˆ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค ๐Ÿ‘๐Ÿ‘

 


 

- ๋ฐฐ์—ด ์ดˆ๊ธฐ๊ฐ’ ๋„ฃ์–ด์„œ ๋งŒ๋“ค๊ธฐ

var array = Array(1...5) // [1,2,3,4,5]

 

- ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ง„ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ

var arr = Array(repeating: 0, count: 3) // [0,0,0]

 

- 2์ฐจ์› ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ

let array: [[Int]] = Array(repeating:Array(repeating:1, count: 4), count: 4) // ์•ˆ์ชฝ count๊ฐ€ ํ–‰, ๋ฐ”๊นฅ count๊ฐ€ ์—ด

 

map, filter, reduce ์ž์„ธํžˆ ๋ณด๊ธฐ ๐Ÿ‘‰ 2020/12/15 - [iOS [Swift]/๊ธฐ์ดˆ๋ฅผ ํƒ„ํƒ„ํžˆ!] - [Swift] map, filter, reduce

 

- map

var string = ["1","2","3","4"]
string.map { Int($0)! } // [1,2,3,4] ๊ฐ ์›์†Œ๋ฅผ ์ „๋ถ€ Int๋กœ ๋งตํ•‘ (๋ฐ˜๋Œ€๋กœ Int -> String๋„ ๊ฐ€๋Šฅ)

 

- filter

var array = [1,2,3,4]
array.filter { $0 % 2 == 0 } // [2,4] ์กฐ๊ฑด์— ๋งž๋Š” ์ˆ˜๋งŒ ๋ฝ‘์•„๋ƒ„

 

- reduce

var array = [1,2,3,4]
array.reduce(0, +) // ์ˆซ์ž ํ•ฉ์ด ๋‚˜ํƒ€๋‚จ. ๋ฌธ์ž์—ด ํ•ฉ์น˜๊ธฐ๋„ ๊ฐ€๋Šฅ

 

- ๊ฑฐ๋“ญ์ œ๊ณฑ

pow(2, 3)  // 8. pow(๋ฐ‘, ์ง€์ˆ˜), ๋ฐ‘์€ Decimal Type

 

- Decimal -> Int

(decimal as NSDecimalNumber).intValue

 

- Root

let num: Double = 25
sqrt(num) // 5. Double ํƒ€์ž…์ด ๋“ค์–ด๊ฐ€์•ผํ•จ

 

- ์ง„๋ฒ• ๋ณ€ํ™˜ / ์ž์„ธํžˆ ๋ณด๊ธฐ ๐Ÿ‘‰ 2020/12/16 - [iOS [Swift]/๊ธฐ์ดˆ๋ฅผ ํƒ„ํƒ„ํžˆ!] - [Swift] String Radix๋ฅผ ํ†ตํ•œ ์ง„๋ฒ•๋ณ€ํ™˜

// 10์ง„์ˆ˜ -> N์ง„๋ฒ•
String(integer, radix: n)

let binary = "111"
let decimal = Int(binary, radix: 2)! // 7

// N์ง„๋ฒ• -> 10์ง„์ˆ˜
Int("110101", radix: n)!

let decimal = 7
let binary = String(decimal, radix: 2) // "111"

 

- ๋ฌธ์ž ASCII์ฝ”๋“œ

Character("a").asciiValue! // 97

 

- ์ ˆ๋Œ“๊ฐ’

abs(-5) // 5

 

- for๋ฌธ ํŠน์ • ์ฃผ๊ธฐ๋กœ ๋ฐ˜๋ณต

stride(from:1, to:5, by:2)       // open range..5 ๋ถˆํฌํ•จ 1,3 ๊นŒ์ง€๋งŒ
stride(from:1, through:5, by:2)  // closed range..5 ํฌํ•จ 1,3,5๊นŒ์ง€

ex)
for even in stride(from:2, through:100, by:2) // ์ง์ˆ˜๋งŒ

 

- String์— ๊ณ„์‚ฐ์‹์„ ๋ฐ”๋กœ ๊ณ„์‚ฐ

let mathExpression = NSExpression(format: "3+4+2-1*6")
let mathValue = mathExpression.expressionValue(with: nil, context: nil) as! Int // result : 3

 

 

 

์ฐธ๊ณ  : Emily๋‹˜ blog

Comments