WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Confusion about unterminated string literal error (Blind developer)

Forums > Swift

I'm a blind developer learning Swift so I can port an existing project over to iOS. I’ve encountered my first error I can’t solve, and was curious if anyone could take a look at the code below. XCode is a nightmare to use with Voiceover (Apple’s screen-reading software), and all I can find-out is that there’s an “unterminated string literal on line 4, which is where I declare the realtor variable in the protocol. I’m not seeing what the issue is. Note: This project is just an XCode Playground.

protocol Building { var roomCount:Int {get set} var cost:Int {get set} var realtor:String {get set}

func describe () }

struct House: Building { var realtor = "Karen" var roomCount = 4 var cost = 700_000 func describe () {

print("I just bought a house from \realtor . It has \roomCount rooms, and it only cost \cost .") } }

   

I ran your code in a playground in the Mac version of Swift Playgrounds. I'm wrapping the code in code blocks so other people reading this will have an easier time reading the code.

protocol Building { 
    var roomCount:Int {get set} 
    var cost:Int {get set} 
    var realtor:String {get set}

    func describe() 

}

struct House: Building { 
    var realtor = "Karen" 
    var roomCount = 4 
    var cost = 700_000 

    func describe() {
        print("I just bought a house from \realtor . It has \roomCount rooms, and it only cost \cost .") 
    } 

}

I don't see any unterminated strings in your code. The "Karen" string is terminated.

When I ran the playground, I got an "Invalid escape sequence in literal" error in the print statement.

The cause of the error is the way you print the variables in the print statement. You must wrap the variable name in parentheses after the backslash character. The following changes in the print statement fixed the error.

func describe() {
    print("I just bought a house from \(realtor). It has \(roomCount) rooms, and it only cost \(cost).")       
} 

   

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.