๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/iOS Application

[iOS] ์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ค€๋น„ํ•˜๊ธฐ - Command Line Tool, LeadLine, Split, Components, map, print ์‚ฌ์šฉํ•˜๊ธฐ

Swift๋กœ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ์ค€๋น„, ์„ธํŒ…ํ•˜๊ธฐ
Swift ์ž…๋ ฅํ•˜๊ธฐ

 

Swift๋กœ ์ฝ”ํ…Œ๋ฅผ ์ค€๋น„ํ•˜๋ ค๊ณ  ํ•˜๋‹ˆ! ์–ด๋–ป๊ฒŒ ์ž…๋ ฅ์„ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•˜์ง€...? 

Swift๋กœ ์ž…๋ ฅ์„ ๋ฐ›๊ธฐ ์œ„ํ•ด์„  project ์ƒ์„ฑ์‹œ, playground, app์ด ์•„๋‹Œ ์ƒˆ๋กœ์šด ํƒฌํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค!

์ฝ”ํ…Œ๋ฅผ ์œ„ํ•œ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ๋ฒ•, Swift์—์„œ ์ž…์ถœ๋ ฅ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! ๐Ÿ‘ป


1. ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ

๋จผ์ €, playground๋Š” sandbox์ด๊ธฐ ๋•Œ๋ฌธ์— commandline input์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค!

(*์ƒŒ๋“œ๋ฐ•์Šค(sandbox) : ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ๋“ค์–ด์˜จ ํ”„๋กœ๊ทธ๋žจ์ด ๋ณดํ˜ธ๋œ ์˜์—ญ์—์„œ ๋™์ž‘ํ•ด ์‹œ์Šคํ…œ์ด ๋ถ€์ •ํ•˜๊ฒŒ ์กฐ์ž‘๋˜๋Š” ๊ฒƒ์„ ๋ง‰๋Š” ๋ณด์•ˆ ํ˜•ํƒœ)

๋”ฐ๋ผ์„œ, project๋ฅผ ์ƒ์„ฑ ์‹œ ํ…œํ”Œ๋ฆฟ์„ Command Line Tool๋กœ ์„ ํƒํ•˜์—ฌ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค!

xcode File > New > proj > command tool

Language๋Š” Swift๋กœ ํ•ด์ฃผ์‹œ๊ณ ~ Next!!

์งœ์ž”~ ๋‹ค์Œ๊ณผ ๊ฐ™์ด main.swift ํŒŒ์ผ์ด ํฌํ•จ๋œ ํ”„๋กœ์ ํŠธ๊ฐ€ ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค!

์ด๊ณณ์—์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์—ฐ์Šตํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค~๐Ÿ‘๐Ÿป

 


2. ์ž…๋ ฅ ๋ฐ›๊ธฐ

์ฝ”ํ…Œ์—์„œ๋Š” ๋ณดํ†ต ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฌธ์ œ๋“ค์ด ๋งŽ๊ณ , ์ž…๋ ฅ๋ฐ›๋Š” ๊ฐ’๋“ค์ด ๋‹ค์–‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์—!

LeadLine, Split, Components, map์„ ์‚ฌ์šฉํ•œ Swift์—์„œ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฒ•๋“ค์„ ์†Œ๊ฐœํ• ๊ฒŒ์š”~๐Ÿ‘ป


1) ๊ธฐ๋ณธ ์ž…๋ ฅ๋ฐ›๊ธฐ

๋จผ์ €, Swift์—์„œ ์ž…๋ ฅ์€ readLine() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค.

func readLine(strippingNewline: Bool = true) -> String?

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ ๊ฐ’์ด ์˜ต์…”๋„์ด๊ธฐ ๋•Œ๋ฌธ์—, ์•ˆ์ „ํ•˜๊ฒŒ ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๊ฑฐ๋‚˜, 

var input = readLine()

if let input = input{
    print("input : \(input)")
}

๊ฐ•์ œ ์ถ”์ถœํ•ด์ค๋‹ˆ๋‹ค.

//Reading a String
var input = readLine()!
print("input : \(input)")

์œ„ ์ฝ”๋“œ๋กœ ํ…Œ์ŠคํŠธํ•ด๋ณด๋ฉด,

๊ตณ!๐Ÿ‘๐Ÿป

 

readLine์˜ ์ถœ๋ ฅ ๊ฐ’์€ String ํƒ€์ž…์œผ๋กœ Intํ˜•์œผ๋กœ ๋ฐ›๊ณ ์ž ํ•  ๋•Œ๋Š”,

๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ˜• ๋ณ€ํ™˜ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

//Reading an Int
var inputInt = Int(readLine()!)!
//Reading a Double
var inputInt = Double(readLine()!)!

 

readLine์„ ์‚ฌ์šฉํ•˜๋ฉด ์ž…๋ ฅ๊ฐ’์„ ์ž…๋ ฅ ํ›„ Enterํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ๊ทธ๋‹ค์Œ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ, ์ฝ”ํ…Œ ์ค€๋น„ํ•˜์‹  ๋ถ„๋“ค์€ ๋‹ค ์•„์‹œ๊ฒ ์ง€๋งŒ.... 

๊ฐ’์„ ์—ฌ๋Ÿฌ ๊ฐœ ๋ฐ›์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋“ค๋„ ์žˆ๊ณ  String์ด ์•„๋‹Œ ๊ฐ’์œผ๋กœ ๋ฐ›์„ ๋•Œ๋„ ์žˆ๊ณ ... ๋‹ค์–‘ํ•œ ์ž…๋ ฅ๊ฐ’ ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 


2) ๋ฐฐ์—ด๋กœ ๋ฐ›๊ธฐ

์—ฌ๋Ÿฌ ๊ฐ’๋“ค์„ ๊ณต๋ฐฑ ๋‹จ์œ„์˜ ์ž…๋ ฅ์„ ๋ฐ›์•„์„œ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์—๋Š” ํฌ๊ฒŒ components, split ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

components(separatedBy:)

func components(separatedBy separator: String) -> [String]
  • parameter๋กœ String์„ ๋ฐ›์•„ [String] ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • components๋Š” Foundation ํ”„๋ ˆ์ž„ ์›Œํฌ์— ์†ํ•ด์žˆ๊ธฐ ๋•Œ๋ฌธ์—, components๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Foundation์„ import ํ•ด์•ผ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

์˜ˆ์‹œ )

var inputStringArray = str.components(seperatedBy: " ")

 

 


split()

func split(separator: Character, maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true) -> [Substring]
  • parameter
    • separator : ์ชผ๊ฐœ๋ ค๋Š” ๋ฌธ์ž ๋‹จ์œ„
    • maxSplits : ์ง€์ •ํ•œ ๋ฌธ์ž ๋‹จ์œ„๋กœ ์–ผ๋งˆ๋‚˜ ์ชผ๊ฐค์ง€
    • omittingEmptySubsequences : Bool๊ฐ’์œผ๋กœ ๊ฒฐ๊ณผ๊ฐ’์—์„œ ๋นˆ ์‹œํ€€์Šค์˜ ํฌํ•จ ์œ ๋ฌด๋ฅผ ์„ค์ •
  • parameter๋กœ Character์„ ๋ฐ›์•„ [Substring] ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • split์€ Swift ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ import ํ•„์š” X

์˜ˆ์‹œ )

let input = readLine()!.split(separator: " ")

 

 


ํ•˜์ง€๋งŒ ์œ„ ๋‘ ํ•จ์ˆ˜๋Š” ์ฝ”ํ…Œ์˜ ์—ฐ์‚ฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ, ์šฐ๋ฆฌ๋Š” map() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Int๋‚˜ Doubleํ˜•์œผ๋กœ ํƒ€์ž…์„ ๋ณ€๊ฒฝํ•ด์ค๋‹ˆ๋‹ค.

map() ํ•จ์ˆ˜๋Š” ๊ฐ ์š”์†Œ์— ์ ‘๊ทผํ•˜์—ฌ ์ฃผ์–ด์ง„ ํด๋กœ์ ธ์˜ ๋™์ž‘์„ ์ ์šฉ์‹œํ‚ต๋‹ˆ๋‹ค.

map()

func map<T>(_ transform: (Element) throws -> T) rethrows -> [T]

 

์˜ˆ์‹œ) Intํ˜• ๋ฐฐ์—ด๋กœ ์ €์žฅํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

let inputIntArray1 = readLine()!.split(separator: " ").map{Int($0)!}
let inputIntArray2 = readLine()!.components(separatedBy: [" "]).map { Int($0)! }

 

 


3. ์ถœ๋ ฅํ•˜๊ธฐ

์ถœ๋ ฅ์€ print() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

func print(_ items: Any..., separator: String = " ", terminator: String = "\n")
  • Parameters
    • items : ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ
    • separator : items๊ฐ€ 2๊ฐœ ์ด์ƒ์ผ ๋•Œ item ์‚ฌ์ด๋งˆ๋‹ค ์ถœ๋ ฅํ•  ๊ฒƒ. ๋””ํดํŠธ ๊ฐ’์€ ์ŠคํŽ˜์ด์Šค.
    • terminator : items๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•œ ํ›„ ๋งˆ์ง€๋ง‰์— ๋ถ™์ด๋Š” ์ข…๊ฒฐ์ž. ๋””ํดํŠธ ๊ฐ’์€ ๊ฐœํ–‰('\n'). ์ฆ‰ print()๋Š” ์ž๋™ ๊ฐœํ–‰์„ ํ•ด์คŒ.

 

์˜ˆ์‹œ )

let hi = "ํ•˜์ด" // let์€ ์ƒ์ˆ˜๋ž€ ์˜๋ฏธ. swift์—์„œ type๋Š” inferring(์ถ”๋ก )๋จ
print("์•ˆ๋…• ~ \(hi).")
// Prints "์•ˆ๋…• ~ ํ•˜์ด"

print(1.0, 2.0, 3.0, 4.0, 5.0, separator: " ... ")
// Prints "1.0 ... 2.0 ... 3.0 ... 4.0 ... 5.0"

for n in 1...5 {
    print(n, terminator: "")
}
// Prints "12345"

 

728x90
๋ฐ˜์‘ํ˜•