UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 .") } }

2      

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).")       
} 

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.