iOS

[ iOS ] Raw String

Forest Yun 2024. 12. 26. 12:12
728x90

 

Raw String

Raw String์€ escape ๋ฌธ์ž์˜ ๋ถˆํŽธํ•จ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Swift5๋ถ€ํ„ฐ ์ง€์›ํ•˜๋Š” ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ๊ตฌ๋ถ„ ๊ธฐํ˜ธ์ž…๋‹ˆ๋‹ค.

escape ๋ฌธ์ž๋Š” ์œ ์šฉํ•˜๊ณ  ํ•„์ˆ˜์ ์ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์ง€๋งŒ, escape ์‹œํ€€์Šค๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์ž์—ด์€ ๊ฐ€๋…์„ฑ์ด ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ๋Š” “raw”, ์ฆ‰ ์›์‹œ ๊ทธ๋Œ€๋กœ์˜ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ๊ตฌ๋ฌธ์„ ๋Œ€์ฒด์ œ๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ raw string ๋ฆฌํ„ฐ๋Ÿด์€ escape ์—†์ด ๋ฐฑ์Šฌ๋ž˜์‹œ(\)์™€ ๋”๋ธ” ์ฟผํ„ฐ(”)๋ฅผ ํ†ตํ•ฉํ•˜์—ฌ “raw”ํ•œ ํ…์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

 

Raw String ์ง€์› ๋ฐฐ๊ฒฝ

 

swift-evolution/proposals/0200-raw-string-escaping.md at main · swiftlang/swift-evolution

This maintains proposals for changes and user-visible enhancements to the Swift Programming Language. - swiftlang/swift-evolution

github.com

 

Swift online forum์—์„œ๋Š” escape ๋ฌธ์ž์˜ ๋‹จ์ ๊ณผ raw string์˜ ์žฅ์ ์„ ์ด์œ ๋กœ Swift์—์„œ๋„ raw string์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์„ ์ œ์•ˆํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”๋ถˆ์–ด Swift๋Š” simpleํ•˜๊ณ  cleanํ•œ ๊ฒƒ์„ ํŠน์ง•์œผ๋กœ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์—, ๋‹จ์ผ ๋ฌธ์žฅ๊ณผ ๋‹ค์ค‘ ๋ฌธ์žฅ ๋ชจ๋‘ ๋‚ด์šฉ ์ƒ๊ด€์—†์ด ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์ด ์ง€์›๋˜๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ๋””์ž์ธ์ด ์ œ๊ณต๋˜์–ด์•ผ ํ•œ๋‹ค๊ณ  ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ Swift 5๋ถ€ํ„ฐ raw string๋ฅผ ์ง€์›ํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

 

์—ฌ๋Ÿฌ ์–ธ์–ด์—์„œ raw string์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€๋งŒ, Swift์˜ raw string์€ Swift์— ์ ํ•ฉํ•œ Rust์˜ ๊ตฌ๋ฌธ์„ ์ฐจ์šฉํ•˜์—ฌ ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค. ๋”ํ•ด์„œ ์กฐ๊ธˆ ๋” swiftyํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด # ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ฒฐ์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

Syntax Language Possibile in Swift Swifty?
r#"Hello, world!"# Rust O Would need to drop the opening r and maybe change the delimiter from #.

 

 

 

 

 

 

 

Swift์˜ raw string ์‚ฌ์šฉ ์˜ˆ์‹œ

var content = "Hello, playground"

var escapeVersion = "She said, \\"Hello, playground\\"."
var rawstringVersion1 = #"She said, "HEllo, Playground"."#      // ์•ž๋’ค๋กœ #
var rawstringVersion2 = ###"She said, "HEllo, Playground"."###  // ๊ฐœ์ˆ˜ ์ƒ๊ด€ x
var rawstringVersion3 = #"She said, "\\#(content)"."#            // ๋ฌธ์ž์—ด ๋ณด๊ฐ„๋ฒ•์—๋„ # ์‚ฌ์šฉ

print(escapeVersion)
print(rawstringVersion1)
print(rawstringVersion2)
print(rawstringVersion3)

/*
 ๋™์ผํ•˜๊ฒŒ ์ถœ๋ ฅ๋จ
 She said, "Hello, playground".
 She said, "HEllo, Playground".
 She said, "HEllo, Playground".
 She said, "Hello, playground".
 */

 

 

 

728x90