[Swift ๋ฌธ๋ฒ•] 06. Swift ์ปฌ๋ ‰์…˜ ํƒ€์ž…์˜ Set(์…‹)์„ ๋ฐฐ์›Œ๋ณด์ž.

2021. 1. 8. 14:36ใ†๐Ÿง‘๐Ÿป‍๐Ÿ’ป iOS ๊ฐœ๋ฐœ/Swift

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


์Šค๋งˆํŠธํฐ์œผ๋กœ 15๋…„๊ฐ„ ์‚ฐ์—… ์ƒํƒœ๊ณ„๊ฐ€ ์›€์ง์˜€๋‹ค๋ฉด ๋‹ค์Œ์€ ์Šค๋งˆํŠธ ์ž๋™์ฐจ๊ฐ€ ์•„๋‹๊นŒ์š”? ์‚ฌ๋žŒ์ด ์šด์ „์„ ํ•˜๋Š” ์‹œ๋Œ€๋Š” ๊ณง ์‚ฌ๋ผ์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฐจ์— ํƒ€์„œ ์ด๋™ํ•˜๋Š” ๋™์•ˆ ์‚ฌ๋žŒ๋“ค์ด ์ž ๋งŒ ์ž˜ ๊นŒ์š”? ์• ํ”Œ์นด๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค๋ฉด ๊ทธ ์•ˆ์—์„œ ์†Œ๋น„๋˜๋Š” ๋ชจ๋“  ์ปจํ…์ธ ์™€ ํ”Œ๋žซํผ์€ ์• ํ”Œ์˜ ๊ฐœ๋ฐœ ์–ธ์–ด์ธ Swift๋กœ ์ž‘์„ฑ๋  ํ™•๋ฅ ์ด ๊ต‰์žฅํžˆ ๋†’์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๊ฐ™์ด ๊ณต๋ถ€ํ•ด๋ด…์‹œ๋‹ค!

Swift์—๋Š” ๋งŽ์€ ์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌถ์–ด์„œ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ปฌ๋ ‰์…˜ ํƒ€์ž…์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ปฌ๋ ‰์…˜ ํƒ€์ž…์—๋Š” Array (๋ฐฐ์—ด), Dictionary (๋”•์…”๋„ˆ๋ฆฌ), Set (์„ธํŠธ)๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

์ €๋ฒˆ ํฌ์ŠคํŒ…์—์„œ ์ปฌ๋ ‰์…˜ ํƒ€์ž…์ธ Dictionary๋ฅผ ๋‹ค๋ค˜๋‹ค๋ฉด ์ด๋ฒˆ์—๋Š” Set ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


1. Set ์˜ ํŠน์ง• 

์„ธํŠธ๋Š” ๊ฐ™์€ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์„œ ์—†์ด ํ•˜๋‚˜์˜ ๋ฌถ์Œ์œผ๋กœ ์ €์žฅํ•˜๋Š” ํ˜•ํƒœ์˜ ์ปฌ๋ ‰์…˜ ํƒ€์ž…์ž…๋‹ˆ๋‹ค. ์ค‘ํ•™๊ต ๋•Œ ๋ฐฐ์› ๋˜ ์ง‘ํ•ฉ์ด๋ž‘ ๊ฑฐ์˜ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๋Œ€์‹  ์„ธํŠธ ๋‚ด์˜ ๊ฐ’์€ ๋ชจ๋‘ ์œ ์ผํ•œ ๊ฐ’์œผ๋กœ, ์ค‘๋ณต๋œ ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ํŠน์ง•์ž…๋‹ˆ๋‹ค. ์„ธํŠธ์˜ ์š”์†Œ๋กœ๋Š” ํ•ด์‹œ ๊ฐ€๋Šฅํ•œ ๊ฐ’์ด ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•ด์‹œ ๊ฐ€๋Šฅํ•œ ๊ฐ’์ด๋ž€ Swift ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ Hashable Protocol ๋”ฐ๋ฅธ ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๊ณ  ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ๋ชจ๋‘ ๊ฐ€๋Šฅํ•œ ๊ฐ’์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


2. Set ํƒ€์ž…์˜ ์„ ์–ธ

var [์„ธํŠธ ์ด๋ฆ„]:  Set<ํƒ€์ž…> = Set<ํƒ€์ž…>() ์˜ ๊ธฐ๋ณธ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. Set๋Š” ๋‹ค๋ฅธ ์ปฌ๋ ‰์…˜ ํƒ€์ž…์ธ Array์™€ Dictionary์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ๋™์ผ ํ‘œํ˜„์„ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.  ๋นˆ ์„ธํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๊ณ  Array ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ๊ณผ ๋™์‹œ์— ์ดˆ๊ธฐํ™”๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

// Set ์„ ์–ธ

var country: Set<String> = Set<String>()

let companies: Set<String> = ["samsung","kakao","naver","apple","google"]

let ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ถ”๊ฐ€, ์‚ญ์ œ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•œ ์„ธํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.


3. Set ํƒ€์ž…์˜ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ

  • isEmpty : ๋น„์–ด์žˆ๋Š” ์„ธํŠธ์ธ์ง€ ์•„๋‹Œ์ง€ Bool ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • count : ์„ธํŠธ์— ๋ช‡ ๊ฐœ์˜ ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • insert( ): ์„ธํŠธ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

  • remove( ): ์„ธํŠธ์—์„œ ํ•ด๋‹น ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ๊ทธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. removeAll( )์€ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

  • contains( ): ์„ธํŠธ์—์„œ ํ•ด๋‹น์š”์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด true , ์—†๋‹ค๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • sorted( ): ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์— ์œ ์˜ํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.
var country: Set<String> = Set<String>()
let companies: Set<String> = ["samsung","kakao","naver","apple","google"]

country.insert("korea")
print(companies.count)			// 5
print(companies.contains("lg")) 	// false
print(country.isEmpty)			// false
print(companies.sorted())		// ["apple", "google", "kakao", "naver", "samsung"]


4. Set ์˜ ์ง‘ํ•ฉ ๊ด€๊ณ„ ํ‘œํ˜„ ๋ฉ”์„œ๋“œ 

์„ธํŠธ๋Š” ์ž์‹  ๋‚ด๋ถ€์˜ ๊ฐ’๋“ค์ด ๋ชจ๋‘ ์œ ์ผํ•˜๋ฏ€๋กœ ์ง‘ํ•ฉ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์„ธํŠธ ๊ฐ„์— ํ•ฉ์ง‘ํ•ฉ, ๊ต์ง‘ํ•ฉ, ๋ฐฐํƒ€์  ๋…ผ๋ฆฌํ•ฉ, ์ฐจ์ง‘ํ•ฉ ๋“ฑ์„ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์ž˜ ๊ตฌํ˜„๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

  • union( ) : ๋‘ ์„ธํŠธ๊ฐ„์˜ ํ•ฉ์ง‘ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 

  • symmetricDifference( ) : ๋‘ ์„ธํŠธ๊ฐ„์˜ ๋ฐฐํƒ€์  ๋…ผ๋ฆฌํ•ฉ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

  • substacting( ) : ์ฐจ์ง‘ํ•ฉ

  • intersection( ) : ๊ต์ง‘ํ•ฉ
let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let singleDigitPrimeNumbers: Set = [2, 3, 5, 7]

oddDigits.union(evenDigits).sorted()
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
oddDigits.intersection(evenDigits).sorted()
// []
oddDigits.subtracting(singleDigitPrimeNumbers).sorted()
// [1, 9]
oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted()
// [1, 2, 9]

 

  • isSubset(of: ) :  A.isSubset(of: B) A๊ฐ€ B์— ํฌํ•จ๋˜๋Š” ์ง‘ํ•ฉ์ธ์ง€ Bool ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • isSuperset(of: ) : A.isSuperset(of: B) A ๊ฐ€ B๋ฅผ ํฌํ•จํ•˜๋Š” ์ง‘ํ•ฉ์ธ์ง€ Bool ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 

let houseAnimals: Set = ["๐Ÿถ", "๐Ÿฑ"]
let farmAnimals: Set = ["๐Ÿฎ", "๐Ÿ”", "๐Ÿ‘", "๐Ÿถ", "๐Ÿฑ"]
let cityAnimals: Set = ["๐Ÿฆ", "๐Ÿญ"]

houseAnimals.isSubset(of: farmAnimals)
// true
farmAnimals.isSuperset(of: houseAnimals)
// true
farmAnimals.isDisjoint(with: cityAnimals)
// true

5. ๋งˆ๋ฌด๋ฆฌ 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ ์ปฌ๋ ‰์…˜์˜ ๋งˆ์ง€๋ง‰ ํƒ€์ž…์ธ Set์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜ ์• ํ”Œ๊ณผ ํ˜„๋Œ€๊ฐ€ ํ•ฉ์ž‘ํ•˜์—ฌ ์• ํ”Œ์นด๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ๊ธฐ์‚ฌ๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ํ˜„๋Œ€์ฐจ ์ฃผ๊ฐ€๊ฐ€ ๋ฏธ์นœ ๋“ฏ์ด ์น˜์†Ÿ๋„ค์š”.. ๋ฏธ๋ž˜๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•  ์ˆ˜ ์—†์ง€๋งŒ Lidar ์„ผ์„œ๋ฅผ ๋‹ฌ๊ณ  ๋‚˜์˜ฌ ์• ํ”Œ ๊ธ€๋ž˜์Šค๋‚˜ ์ด๋ฒˆ์˜ ์• ํ”Œ์นด์ฒ˜๋Ÿผ ๋‹ค์Œ 20๋…„์„ ์ด๋Œ์–ด๊ฐˆ ์ƒํƒœ๊ณ„๊ฐ€ ๋‹ค๊ฐ€์˜ค๊ธฐ ์ „์— ๋จผ์ € ๊ณต๋ถ€ํ•˜๊ณ  ์›€์ง์—ฌ์•ผ 30, 40๋Œ€์—๋„ ๊ฒฝ์Ÿ๋ ฅ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์ง€ ์•Š๊ฒ ๋‚˜ ์ƒ๊ฐํ•ด๋ด…๋‹ˆ๋‹ค.  ๊ณ„์† AR/VR ํ”Œ๋žซํผ ๊ฐœ๋ฐœ๊ณผ ๋ฌด์ธ์ž๋™์ฐจ ์†Œํ”„ํŠธ์›จ์–ด ํ”Œ๋žซํผ์— ๋Œ€ํ•ด์„œ ๊ท€๋ฅผ ๊ธฐ์šธ์ด๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

์ฐธ์กฐ: ์• ํ”Œ API ๊ฐ€์ด๋“œ ๋ผ์ธ , ์• ํ”Œ ๊ณต์‹ ๊ฐœ๋ฐœ ๋ฌธ์„œ , Swift 5 ํ”„๋กœ๊ทธ๋ž˜๋ฐ/ ์ง€์€์ด ์•ผ๊ณฐ