Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode is throwing me an error in swift

Tags:

xcode

ios

I'm following a tutorial(http://youtube.com/watch?v=xvvsG9Cl4HA 19 min 20sec) and to make his code look neat he puts some on a ew line like this

if let myPlacement = myPlacements?.first
        {

            let myAddress = "\(myPlacement.locality) \
            (myPlacement.country) \
            (myPlacement.postalCode)"
        }

. But when I try I get an error

unterminated string literal

and

consecutive statements on a line must be seperated by a ';'

but the guy in the tutorial has done it the exact same way. What's going on? I'm using the latest swift and and latest xcode 7.2 any help would be apreciated

if I write everything on the same line like this

if let myPlacement = myPlacements?.first
        {

            let myAddress = "\(myPlacement.locality) \(myPlacement.country) \(myPlacement.postalCode)"
        }

it works fine though

like image 789
losee Avatar asked Jan 10 '16 03:01

losee


People also ask

How do I fix Xcode errors?

Conversation. Xcode tip #5: If you have a Swift file with many errors, you can use Xcode's fix-its to try to correct them one by one. But a faster way is to go to the Editor menu and choose Fix All Issues to apply fix-its all at once. (Make sure and double-check which changes were made!)

What is error handling in Swift?

Error handling is the process of responding to and recovering from error conditions in your program. Swift provides first-class support for throwing, catching, propagating, and manipulating recoverable errors at runtime. Some operations aren't guaranteed to always complete execution or produce a useful output.

How does Swift handle fatal errors?

According to Swift Error Handling Rationale, the general advice is: Logical error indicates that a programmer has made a mistake. It should be handled by fixing the code, not by recovering from it. Swift offers several APIs for this: assert() , precondition() and fatalError() .


2 Answers

if I write everything on the same line like this

Well, there's your answer. You are not permitted to break up a string literal into multiple lines as you are doing in your first example. There are languages that permit this, but Swift is not one of them. This is not legal:

let s = "hello
there"

There is no magic line-continuation character which, placed at the end of the first line, would make that legal.

If the window is narrower than the line, the editor may wrap the line, for display purposes; but you cannot put actual line breaks inside a string literal.

You can work around this by combining (concatenating) multiple string literals, if you think that makes for greater legibility. This, for example, is legal:

let myAddress = "\(myPlacement.locality) " + 
    "\(myPlacement.country) " + 
    "\(myPlacement.postalCode)"
like image 189
matt Avatar answered Oct 12 '22 03:10

matt


I look your video tutorial carefully. You have a misunderstanding here.

enter image description here

You must pay attention to the video, the code in this picture is not break lines because he add a return here, it is because his screen is too narrow.

So, the real code is

let myAddress = "\(myPlacement.locality) \(myPlacement.country) \(myPlacement.postalCode)"

Please watch it carefully.

And you may need know first, \ in \(myPlacement.locality) is a escape character, it means to get the value of myPlacement.locality and put in the string.

like image 30
Tinyfool Avatar answered Oct 12 '22 04:10

Tinyfool