尝试在Swift游乐场中执行此操作时,Xcode冻结吗? [英] Xcode freezes when trying to execute this in a Swift playground?

查看:86
本文介绍了尝试在Swift游乐场中执行此操作时,Xcode冻结吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在尝试Swift,所以我把它们放到了操场上

I was simply experimenting with Swift, so I threw together this in my playground:

// 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)

非常简单的代码,没有什么复杂的.但是Xcode不会执行它.右下角的小负载指示器持续旋转,我的Macbook变热,风扇旋转,什么也没发生.如果我将printCarInfo(car:Car?) -> Void函数中的println命令修改为这样的话:

Very straight forward code, there's nothing complicated about it. But Xcode doesn't execute it. The little loading indicator in the bottom right keeps spinning, my macbook gets hot, the fans spin up, and nothing's happening. If I modify the println command in the printCarInfo(car:Car?) -> Void function to something like this:

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像我上面描述的那样冻结.我尝试保存操场并重新打开它,但是每次都会发生相同的事情.

Xcode freezes like I described above. I tried saving the playground and reopening it, but the same thing happens every time.

我认为代码没有任何问题.仅仅是Xcode beta中的错误吗?任何人都可以尝试将其粘贴到操场上,看看会发生什么吗? 这是一个OS X游乐场,我使用Xcode beta5.此外,我使用的是10.10 DP5.

I don't think there's anything wrong with the code. Is is just a bug in 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.

推荐答案

肯定是Xcode中的错误.编译器在单行执行中连接5个以上的字符串似乎存在问题.以下是我的分析:

Definitely a bug in Xcode. There seems to be an issue with Compiler concatenating more than 5 strings in a single line execution. Below is my analysis:

当前,您可以在一行执行中最多连接5或6个字符串(这不是功能,这是一个错误).例如在Playground中尝试一下:

Currently you can concatenate maximum of 5 or 6 strings in a single line execution (It is not a feature, it's a bug). For e.g. Try this in Playground:

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

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

此执行会挂起Xcode,并将Mac的CPU使用率提高到200%.

This execution hangs the Xcode and increases the Mac's CPU usage to 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.

但是您的代码也有一个小错误.您正在连接UInt和一个字符串. _car.priceUInt类型,您不能直接将其与字符串连接.因此,您可以使用String转换或使用String插值.以下是上述代码的工作方式:

But your code also has one minor bug. You are concatenating UInt and a string. _car.price is of UInt type and you cannot directly concatenate it with a string. So either you use String conversion or use String interpolation. Below is how your above code will work:

具有UInt到字符串的转换

with UInt to String conversion

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.

具有字符串插值

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.

这篇关于尝试在Swift游乐场中执行此操作时,Xcode冻结吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆