๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“Œ Language/Swift

[Swift] ๋ฐฐ์—ด์—์„œ ํŠน์ •๊ฐ’์„ ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ(Change Specific Values to Something in Array )

by Fomagran ๐Ÿ’ป 2021. 2. 22.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜ ์•Œ์•„๋ณผ๊ฑด ๋ฐฐ์—ด์—์„œ ํŠน์ •๊ฐ’์„ ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ์ธ๋ฐ์š”.

 

์ œ๋ชฉ์—์„œ ํ•œ์ค„๋กœ ํ‘œํ˜„ํ•˜๊ธฐ๊ฐ€ ํž˜๋“œ๋„ค์š”...


Preview


Example

 

์‰ฝ๊ฒŒ ํ‘œํ˜„ํ•˜๋ฉด ๊ณผ์ผ์ด ๋‹ด๊ฒจ์žˆ๋Š” ๋ฐฐ์—ด์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ• ๊ฒŒ์š”

 

[๋ฐ”๋‚˜๋‚˜,์‚ฌ๊ณผ,์ˆ˜๋ฐ•,ํฌ๋„,๋ฐ”๋‚˜๋‚˜,๋ฐ”๋‚˜๋‚˜] <- (์ด๋ ‡๊ฒŒ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •)

 

์—ฌ๊ธฐ์„œ ๋ฐ”๋‚˜๋‚˜๋ฅผ ๋ชจ๋‘ ๋”ธ๊ธฐ๋กœ ๋Œ€์ฒดํ•˜๊ณ  ์‹ถ์„๋• ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”?

 

๋จผ์ € ๋ฐ”๋‚˜๋‚˜๊ฐ€ ์–ด๋Š ์œ„์น˜์— ์žˆ๋Š”์ง€ ์•Œ์•„์•ผ ํ•˜๊ฒ ์ฃ ? (ํŠน์ •๊ฐ’์˜ ์ธ๋ฑ์Šค ํŒŒ์•…)

 

๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ ๊ทธ ์œ„์น˜์— ์žˆ๋Š” ๊ฐ’์„ ๋ชจ๋‘ ๋”ธ๊ธฐ๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ ํ• ๊ฒƒ์ž…๋‹ˆ๋‹ค. (ํŠน์ • ์ธ๋ฑ์Šค๋ฅผ ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟˆ,)

 

์ด๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ์‰ฝ๊ฒŒ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ค๊นŒ์š”?

 

๋ฐ”๋กœ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค~


Indices

 

๊ฐ€์žฅ ๋จผ์ € ์•Œ์•„์•ผํ• ๊ฑด "indices" ์ž…๋‹ˆ๋‹ค.

 

์• ํ”Œ ๋ฌธ์„œ์— ๋‚˜์™€์žˆ๋Š”๊ฑธ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด์„ํ•˜๋ฉด ์ปฌ๋ ‰์…˜์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์ด๋ผ๊ณ  ํ•˜๋„ค์š”!

 

์•„๋ž˜์™€ ๊ฐ™์ด index๋“ค์„ ๋‚˜ํƒ€๋‚ด์ฃผ๋Š” ๊ฒƒ์ด์—์š”!

var fruits = ["๋ฐ”๋‚˜๋‚˜","์‚ฌ๊ณผ","์ˆ˜๋ฐ•","ํฌ๋„","๋ฐ”๋‚˜๋‚˜","๋ฐ”๋‚˜๋‚˜"]

let fruitsIndices = fruits.indices	//0..<6

Filter

 

๊ทธ๋ฆฌ๊ณ  ๋ฐ”๋‚˜๋‚˜๊ฐ€ ์–ด๋Š ์œ„์น˜์— ์žˆ๋Š”์ง€ filter ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ์–ด์„œ ์›ํ•˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ๊ฑธ๋Ÿฌ๋ƒ…๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๋‚˜๋‚˜๊ฐ€ ์žˆ๋Š” ์ธ๋ฑ์Šค๋“ค์ด ํ•„ํ„ฐ๋ง ๋œ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์ฃ ?

var fruits = ["๋ฐ”๋‚˜๋‚˜","์‚ฌ๊ณผ","์ˆ˜๋ฐ•","ํฌ๋„","๋ฐ”๋‚˜๋‚˜","๋ฐ”๋‚˜๋‚˜"]

let fruitsIndices = fruits.indices

let filteredFruits = fruitsIndices.filter{fruits[$0] == "๋ฐ”๋‚˜๋‚˜"}	//[0, 4, 5]

 

 

๊ทธ๋ฆฌ๊ณค ์ด๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ๋ฐ”๊ฟ€๊นŒ์š”?๐Ÿค”


ForEach

 

 

๋ฐ”๋กœ foreach๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋Š” ๊ฒƒ์ธ๋ฐ์š”.

 

ForEach๋Š” ๊ฑฐ์˜ for๋ฌธ์ธ๋ฐ ๋ฐฐ์—ด์— ์ง์ ‘ ์ ‘๊ทผํ•ด์„œ ์ˆœํšŒํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด filteredFruits์— forEach๋ฅผ ์‚ฌ์šฉํ•ด์„œ fruits์˜ ๋ฐ”๋‚˜๋‚˜ ์ธ๋ฑ์Šค๋ฅผ ๋ชจ๋‘ ๋”ธ๊ธฐ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

var fruits = ["๋ฐ”๋‚˜๋‚˜","์‚ฌ๊ณผ","์ˆ˜๋ฐ•","ํฌ๋„","๋ฐ”๋‚˜๋‚˜","๋ฐ”๋‚˜๋‚˜"]

let fruitsIndices = fruits.indices

let filteredFruits = fruitsIndices.filter{fruits[$0] == "๋ฐ”๋‚˜๋‚˜"}

filteredFruits.forEach{fruits[$0] = "๋”ธ๊ธฐ"}

//fruits = ["๋”ธ๊ธฐ", "์‚ฌ๊ณผ", "์ˆ˜๋ฐ•", "ํฌ๋„", "๋”ธ๊ธฐ", "๋”ธ๊ธฐ"]

 

๋งˆ์ง€๋ง‰์œผ๋กœ ์ด๊ฒƒ์„ ๋ชจ๋‘ ํ•œ์ค„๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

var fruits = ["๋ฐ”๋‚˜๋‚˜","์‚ฌ๊ณผ","์ˆ˜๋ฐ•","ํฌ๋„","๋ฐ”๋‚˜๋‚˜","๋ฐ”๋‚˜๋‚˜"]

fruits.indices.filter{fruits[$0] == "๋ฐ”๋‚˜๋‚˜"}.forEach{fruits[$0] = "๋”ธ๊ธฐ"}

//fruits = ["๋”ธ๊ธฐ", "์‚ฌ๊ณผ", "์ˆ˜๋ฐ•", "ํฌ๋„", "๋”ธ๊ธฐ", "๋”ธ๊ธฐ"]

 

์ €๋ ‡๊ฒŒ ํ•œ ์ค„๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์€ ๋ฌด์กฐ๊ฑด ์˜ณ์€ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ์ง€๋งŒ ๋ญ”๊ฐ€ ์ด๋Ÿฐ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์“ฐ๋ฉด ๊ณ ์ˆ˜๊ฐ™์€ ๋Š๋‚Œ์ด ๋“ค์ž–์•„์š”..?ใ…Žใ…Ž 

 

์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ ํ•œ์ค„๋กœ ๋ฐฐ์—ด์˜ ํŠน์ •๊ฐ’์€ ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ๋ฐ”๊พธ๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

๊ถ๊ธˆํ•˜์‹ ์ ์ด ์žˆ์œผ๋ฉด ์–ธ์ œ๋“  ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์„ธ์š”!!

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€