简体   繁体   English

尝试在Swift游乐场中执行Xcode冻结?

[英]Xcode freezes when trying to execute this in a Swift playground?

I was simply experimenting with Swift, so I threw together this in my playground: 我只是在尝试Swift,所以我把它放在操场上:

// Playground - noun: a place where people can play

import Cocoa

func printCarInfo(car:Car?) -> Void {
    if let _car = car {
        println("This is a " + _car.make + " " + _car.model + " from \(_car.year). It's worth $" + _car.price + ".")
    }
}

class Car {
    init(make:String, model:String, year:UInt, color:NSColor, price:UInt) {
        self.make   = make
        self.model  = model
        self.year   = year
        self.color  = color
        self.price  = price
    }

    var make    : String
    var model   : String
    var year    : UInt
    var color   : NSColor
    var price   : UInt

    func isNewCar() -> Bool {
        let _formatter = NSDateFormatter()
        _formatter.dateFormat = "yyyy"
        let _currentYear = _formatter.stringFromDate(NSDate())
        return (_currentYear == String(self.year))
    }


}

let chevy = Car(make: "Chevrolet", model: "Camaro ZL1", year: 2014, color: NSColor.redColor(), price: 55355)

printCarInfo(chevy)

Very straight forward code, there's nothing complicated about it. 非常简单的代码,没有什么复杂的。 But Xcode doesn't execute it. 但是Xcode不会执行它。 The little loading indicator in the bottom right keeps spinning, my macbook gets hot, the fans spin up, and nothing's happening. 右下角的小负载指示灯持续旋转,我的Macbook变热,风扇旋转,什么也没发生。 If I modify the println command in the printCarInfo(car:Car?) -> Void function to something like this: 如果我将printCarInfo(car:Car?) -> Void函数中的println命令修改为如下所示:

println("Ok")

Then it's totally fine. 那就很好了 But as soon as I put this line in: 但是,只要我输入以下内容:

println("This is a " + _car.make + " " + _car.model + " from \(_car.year). It's worth $" + _car.price + ".")

Xcode freezes like I described above. Xcode像我上面描述的那样冻结。 I tried saving the playground and reopening it, but the same thing happens every time. 我尝试保存操场并重新打开它,但是每次都发生相同的事情。

I don't think there's anything wrong with the code. 我认为代码没有任何问题。 Is is just a bug in Xcode beta? 仅仅是Xcode beta中的错误吗? Can anyone try to paste this into a playground and see what happens? 有人可以尝试将其粘贴到操场上,看看会发生什么吗? It's an OS X playground and I use Xcode beta 5. Also, I'm on 10.10 DP5. 这是OS X游乐场,我使用Xcode beta5。此外,我使用的是10.10 DP5。

Definitely a bug in Xcode. 绝对是Xcode中的错误。 There seems to be an issue with Compiler concatenating more than 5 strings in a single line execution. 编译器在单行执行中连接5个以上的字符串似乎存在问题。 Below is my analysis: 以下是我的分析:

Currently you can concatenate maximum of 5 or 6 strings in a single line execution (It is not a feature, it's a bug). 当前,您可以在一行执行中最多连接5个或6个字符串(这不是功能,这是一个错误)。 For eg Try this in Playground: 例如,在游乐场中尝试:

var tstr = "A" + "B" + "C" + "D" + "E" + "F" + "G"

or 要么

println("A" + "B" + "C" + "D" + "E" + "F");

This execution hangs the Xcode and increases the Mac's CPU usage to 200%. 此执行会挂起Xcode,并将Mac的CPU使用率提高到200%。

Here's a strange fact. 这是一个奇怪的事实。 Now try this: 现在尝试这个:

var tstr = "A" + "B" + "C" + "D" + "E"
tstr = tstr + "F" + "G"
println(tstr) //Correct output: ABCDEFG

So that seems to be the way around solution to this issue. 因此,这似乎是解决此问题的方法。 Break and Concatenate . 断开并连接

But your code also has one minor bug. 但是您的代码也有一个小错误。 You are concatenating UInt and a string. 您正在串联UInt和一个字符串。 _car.price is of UInt type and you cannot directly concatenate it with a string. _car.priceUInt类型,您不能直接将其与字符串连接。 So either you use String conversion or use String interpolation. 因此,您可以使用字符串转换或使用字符串插值。 Below is how your above code will work: 以下是上述代码的工作方式:

with UInt to String conversion UInt到String的转换

var str = "This is a " + _car.make + " "
str = str + _car.model + " from \(_car.year). It's worth $" + String(_car.price) +  "."
println(str) //Prints: This is a Chevrolet Camaro ZL1 from 2014. It's worth $55355.

or 要么

with String Interpolation 带字符串插值

var str = "This is a " + _car.make + " "
str = str + _car.model + " from \(_car.year). It's worth $ \(_car.price)."
println(str)//Same output: This is a Chevrolet Camaro ZL1 from 2014. It's worth $55355.
 println("This is a \(car.make) \(car.model) from \(car.year). Itsworth \(car.price)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM