I'm a Swift beginner and I have a question.
我是一个快速的初学者,我有一个问题。
I created a new playground file in Xcode6-Beta3 , and erased
all default snippets generated automatically. And I wrote following
one line only,
我在Xcode6-Beta3中创建了一个新的游乐场文件,并删除了自动生成的所有默认代码片段。我只写了一行,
var x:Int = (1.234 as Int)
And then, this line caused a compile error with message saying that
然后,这一行产生了一个编译错误,消息是这样的
Playground execution failed:
error: <EXPR>:1:20: error:
'Double' is not convertible to
'Int'
var x:Int = (1.234 as Int)
^
in the Console Output. I can understand this error because 1.234 can not be casted as Int.
在控制台输出。我可以理解这个错误,因为1.234不能被转换为Int。
Next, I added one more line import UIKit
before var x:Int = (1.234 as Int)
,
so the codes were following:
接下来,在var x:Int =(1.234作为Int)之前,我又添加了一个行导入UIKit,所以代码如下:
import UIKit
var x:Int = (1.234 as Int)
Then, the error message above disappeared. But I can't understand
the reason that adding import UIKit
caused disappearing
the error message claiming that'Double' is not convertible to 'Int'
above.
然后,上面的错误消息消失了。但是我不能理解为什么添加导入UIKit导致了错误信息的消失,声称“Double”不能转换到上面的“Int”。
Please teach me this reason or some references to understand about it.
请告诉我这个原因或一些参考资料来了解它。
Regards.
的问候。
1 个解决方案
#1
1
The compiler will always seek a way to make your code compiling as possible as it can. With import UIKit
, your code is something equivalent to
编译器总是会寻找一种方法使您的代码尽可能地进行编译。使用导入UIKit,你的代码是等价的
var _x = 1.234 as NSNumber
var x:Int = (_x as Int)
The compiler can turn to NSNumber
when have UIKit
imported.
当有UIKit导入时,编译器可以转到NSNumber。
#1
1
The compiler will always seek a way to make your code compiling as possible as it can. With import UIKit
, your code is something equivalent to
编译器总是会寻找一种方法使您的代码尽可能地进行编译。使用导入UIKit,你的代码是等价的
var _x = 1.234 as NSNumber
var x:Int = (_x as Int)
The compiler can turn to NSNumber
when have UIKit
imported.
当有UIKit导入时,编译器可以转到NSNumber。