Xcode 迅速向我抛出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34701637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:24:37  来源:igfitidea点击:

Xcode is throwing me an error in swift

iosxcode

提问by losee

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

我正在学习一个教程(http://youtube.com/watch?v=xvvsG9Cl4HA19 min 20sec),为了让他的代码看起来整洁,他把一些放在这样的一行上

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

但是教程中的那个人以完全相同的方式完成了它。这是怎么回事?我正在使用最新的 swift 和最新的 xcode 7.2 任何帮助都会受到赞赏

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

虽然它工作正常

回答by matt

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:

嗯,这就是你的答案。您不能像在第一个示例中那样将字符串文字分解为多行。有些语言允许这样做,但 Swift 不是其中之一。这不合法:

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 editormay wrapthe line, for display purposes; but youcannot 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)"

回答by Tinyfool

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.localityand put in the string.

你可能需要先知道,\in\(myPlacement.locality)是一个转义字符,它的意思是获取 的值myPlacement.locality并放入字符串中。