[Swift ๋ฌธ๋ฒ•] 02. Swift ์˜ ๋ณ€์ˆ˜ ๋ช…๋ช…๋ฒ•๊ณผ ์ฝ˜์†” ๋กœ๊ทธ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

2021. 1. 6. 10:27ใ†๐Ÿง‘๐Ÿป‍๐Ÿ’ป iOS ๊ฐœ๋ฐœ/Swift

ํ•ด๋‹น ๊ธ€์€ ์•ผ๊ณฐ๋‹˜์˜ ์Šค์œ„ํ”„ํŠธ ๊ฐ•์˜์™€ ๊ฐœ์ธ์ ์ธ ๊ณต๋ถ€๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.


1. ๋ณ€์ˆ˜ ๋ช…๋ช…๋ฒ• (Naming) 

์ด๋ฒˆ์—๋Š” Swift์–ธ์–ด์˜ ๊ธฐ๋ณธ ๋ช…๋ช… ๊ทœ์น™์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค, ๋ช…๋ช… ๊ทœ์น™์€ ํ”„๋ ˆ์ž„ ์›Œํฌ๋‚˜ ์ฝ”๋”ฉ ํ™˜๊ฒฝ ๋˜๋Š” ํšŒ์‚ฌ๋งˆ๋‹ค ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  ๊ทธ๋ž˜๋„ ์• ํ”Œ์—์„œ ์ง์ ‘ ๊ถŒ์œ ํ•˜๋Š” ๊ฐ€์ด๋“œ๋ผ์ธ์ด ์žˆ์œผ๋‹ˆ ์งš๊ณ  ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ์ฃ ? 

  •   Camel Case๋ž€?

       ๋จผ์ € ์นด๋ฉœ ์ผ€์ด์Šค๋ž€ ๋‹จ์–ด๊ฐ€ ํ•ฉ์ณ์ง„ ๋ถ€๋ถ„๋งˆ๋‹ค ๋งจ ์ฒ˜์Œ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ทธ ๋ชจ์–‘์ด ๋‚™ํƒ€(Camel)๋ฅผ ๋‹ฎ์•˜๋‹ค๊ณ  ํ•ด์„œ ๊ทธ๋ ‡๊ฒŒ ๋ถ€๋ฅด๋Š” ๊ฒƒ์ด์ฃ .

       EX) CamelCase, PrintName, BlogJoa

  •   Lower Camel Case : ํ•จ์ˆ˜, ๋ฉ”์„œ๋“œ, ์ธ์Šคํ„ด์Šค ( ๋ณ€์ˆ˜, ์ƒ์ˆ˜ ..)

      ์นด๋ฉœ ์ผ€์ด์Šค๋ฅผ ์ดํ•ดํ•˜์…จ๋‹ค๋ฉด, Lower Camel Case๋Š” ๊ฑฐ๊ธฐ์„œ ๋งจ ์•ž๊ธ€์ž๋งŒ ์†Œ๋ฌธ์ž๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
      
      EX) camelCase, printName, blogJoa

  •   Upper Camel Case : ํƒ€์ž… ( ๊ตฌ์กฐ์ฒด, ํด๋ž˜์Šค, ์ต์Šคํ…์…˜, ํ”„๋กœํ† ์ฝœ, ์—ด๊ฑฐํ˜• )

      Upper Camel Case๋Š” ๋งจ ์•ž๊ธ€์ž๋งŒ ๋Œ€๋ฌธ์ž๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
      
      EX) CamelCase, PrintName, BlogJoa

โ€ป ํƒ€์ž…๋“ค๊ณผ ํ”„๋กœํ† ์ฝœ๋“ค๋งŒ ์ œ์™ธํ•˜๊ณ  ๋‹ค Lower Camel Case๋ผ๊ณ  ๊ธฐ์–ตํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค โ€ป

// ๋ช…๋ช…๋ฒ•(Naming)

// Lower Camel Case: fucntion, method, variable, constant 4 ๊ฐ€์ง€

var someInt: Int = -100
let someString: String = "extramilejin"
func printName(name: String) -> Void {print("I'm \(name).")}

// Upper Camel Case: type (class, struct, enum, extension ...)

class Person {}
struct Student {}
enum WeekDays {}
    

2. ์ฝ˜์†” ๋กœ๊ทธ (Console Log)

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋กœ๊ทธ๋ž€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ƒํƒœ, ๋‚ด๋ถ€ ๋กœ์ง์˜ ํ๋ฆ„์„ ๊ด€์ฐฐํ•  ์ˆ˜ ์žˆ๋„๋ก ์ถœ๋ ฅํ•œ ์ •๋ณด์ž…๋‹ˆ๋‹ค. ์ฝ˜์†” ๋กœ๊ทธ๋Š” ์ฝ˜์†”์— ๋ณด์—ฌ์ค„ ๋กœ๊ทธ๋ผ๋Š” ๊ฒƒ์ด์ฃ . Swift์—์„œ๋Š” ๋‘ ๊ฐ€์ง€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ˜์†” ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • print() ํ•จ์ˆ˜: ๊ธฐ๋ณธ์ ์œผ๋กœ ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•œ ๋’ค ์ค„ ๋ฐ”๊ฟˆ์ด ๋ฉ๋‹ˆ๋‹ค. (์ค„ ๋ฐ”๊ฟˆ ๋ฌธ์ž(\n)๋ฅผ ์ž๋™์œผ๋กœ ์‚ฝ์ž…)

  • dump() ํ•จ์ˆ˜: print() ํ•จ์ˆ˜๋ณด๋‹ค ์ถœ๋ ฅํ•˜๋ ค๋Š” ์ธ์Šคํ„ด์Šค์˜ ์ž์„ธํ•œ ๋‚ด์šฉ๊นŒ์ง€ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค. 

// Console Log : print()ํ•จ์ˆ˜์™€ dump()ํ•จ์ˆ˜

import Swift

class Person {
    var name: String = "extramilejin"
    var age: Int = 28
}

let jin: Person = Person()
print("# print -------------")
print(jin)
print("# dump -------------")
dump(jin)


3. ๋ฌธ์ž์—ด ๋ณด๊ฐ„๋ฒ• (String Interpolation)

๋ฌธ์ž์—ด ๋ณด๊ฐ„๋ฒ•์€ Swift์—์„œ ๋ณ€์ˆ˜ ๋˜๋Š” ์ƒ์ˆ˜ ๋“ฑ์˜ ๊ฐ’์„ ๋ฌธ์ž์—ด์—์„œ ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

\(๋ณ€์ˆ˜ ์ด๋ฆ„)์˜ ํ˜•ํƒœ๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค.

// ๋ฌธ์ž์—ด ๋ณด๊ฐ„๋ฒ• (String Interpolation)

import Swift

let age: Int = 20
var name: String = "extramilejin"

print("์•ˆ๋…•ํ•˜์„ธ์š”! ์ €๋Š” \(age+8)์‚ด ์ž…๋‹ˆ๋‹ค.")
print("์ œ ์ด๋ฆ„์€ \(name)์ž…๋‹ˆ๋‹ค."

4. ๋งˆ๋ฌด๋ฆฌ

์ด๋ฒˆ ํฌ์ŠคํŠธ๋„ ์ดํ•ด๊ฐ€ ์ž˜ ๋˜์…จ๋‚˜์š”? ์ €๋„ ์ด์ œ ๋ง‰ Swift ๊ณต๋ถ€๋ฅผ ์‹œ์ž‘ํ•ด์„œ ์•„์ง ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ๊ธฐ์ดˆ์ ์ธ ๋ฌธ๋ฒ• ๋ถ€๋ถ„๋“ค์„ ๋‹ค๋ฃฐ ํ…Œ๋‹ˆ ์ž์ฃผ ์™€์ฃผ์„ธ์š”! (Ctrl + D) ๊ฐ™์ด ์—ด์‹ฌํžˆ ๊ณต๋ถ€ํ–ˆ์œผ๋ฉด ํ•˜๋„ค์š”! ^^  ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์ด๋‚˜ ๊ถ๊ธˆํ•˜์‹  ์  ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.