๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

์ฒ˜์Œ swift๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์ด๋Ÿฐ ์—๋Ÿฌ๋ฅผ ๋ช‡ ๋ฒˆ ๋ณธ ์  ์žˆ๋‹ค.

์–ด๋ผ..ํŒŒ์ด์ฌ์€ ์ด๋Ÿฐ ์—๋Ÿฌ ์•ˆ๋–ด๋Š”๋ฐ..

 

Int๋ฅผ ์ด์šฉํ•ด์„œ String์˜ subscript๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ  ๋Œ€์‹  String.Index๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ํ•œ๋‹ค.

 

์™œ์ง€..?

 

์• ํ”Œ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด String์€ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด ๊ฐ’ ์ด๋ผ๊ณ  ํ•œ๋‹ค.

์œ ๋‹ˆ์ฝ”๋“œ๋Š” ๊ฐ€๋ณ€ ๊ธธ์ด ๋ฌธ์ž ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ˆˆ์— ๋ณด์ด๋Š” ๋ฌธ์ž์—ด์ด ํ•˜๋‚˜๋”๋ผ๋„ ์‹ค์ œ ๊ธธ์ด๋Š” ๋‹ค๋ฅด๋‹ค.

๋”ฐ๋ผ์„œ Int๋กœ๋Š” subscript๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด๋‹ค.

 

๋งŒ์•ฝ์— String์˜ subscript๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด 

 

let str = "hello sun"
let index = str.index(str.startIndex, offsetBy: 2)
print(str[index])

String.Index๋กœ subscript๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 

+ ์ถ”๊ฐ€๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ๋Š” ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ํ˜•๋ณ€ํ™˜์„ ํ•ด ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ํŽธํ•˜๋‹ค.

let str = "hello sun"
let arr = Array(str)
print(arr[3])
profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...