Swift 2 Playground(Xcode 7 Beta 4) - 数组中的多种类型仅在导入 UIKit 时有效?
Swift 2 Playground(Xcode 7 Beta 4) - Multiple types in an array only work when UIKit imported?
我在 Playground 中尝试 Swift 数组时,发现了这种奇怪的行为
下面的代码工作正常。
import UIKit
var array = [1,2,3,"Booyaa"]
但是,一旦我从 Playground 中删除 "import UIKit" 行,我就会收到以下错误
Playground execution failed: /var/folders/tx/tvyf1r314wj9371f491qx8wjbqbgsr/T/./lldb/11708/playground71.swift:2:17: error: 'Int' is not convertible to 'IntegerLiteralConvertible'
var array = [1, 2, 3, "Booyaa"]
为什么会这样?
对于高级编程语言,这需要 lexical structure
来检查和比较复杂类型作为您的问题。它是在 Foundation
框架中实现的。
还 UIKit
实现了一些 UI
api 作为 UIView
等...在 UIKit
框架中需要使用 Foundation
框架。
顺便说一句:Foundation
框架是核心 Swift
,Objective-C
语言。
希望对您有所帮助!
正确答案在这里,是对我在 Apple Dev 论坛上发布的相同问题的回应 => https://forums.developer.apple.com/message/35389
It's because Swift Arrays can only contain objects that are the same
type. Your array has integers and a string. When you import UIKit the
objects in the array become objects of type NSObject by virtue of
Swift's inference engine. To see for yourself, add the line after the
array definition, you'll see the type as Swift.array
import UIKit
var array = [1,2,3,"Swift2"]
array.dynamicType
我在 Playground 中尝试 Swift 数组时,发现了这种奇怪的行为
下面的代码工作正常。
import UIKit
var array = [1,2,3,"Booyaa"]
但是,一旦我从 Playground 中删除 "import UIKit" 行,我就会收到以下错误
Playground execution failed: /var/folders/tx/tvyf1r314wj9371f491qx8wjbqbgsr/T/./lldb/11708/playground71.swift:2:17: error: 'Int' is not convertible to 'IntegerLiteralConvertible' var array = [1, 2, 3, "Booyaa"]
为什么会这样?
对于高级编程语言,这需要 lexical structure
来检查和比较复杂类型作为您的问题。它是在 Foundation
框架中实现的。
还 UIKit
实现了一些 UI
api 作为 UIView
等...在 UIKit
框架中需要使用 Foundation
框架。
顺便说一句:Foundation
框架是核心 Swift
,Objective-C
语言。
希望对您有所帮助!
正确答案在这里,是对我在 Apple Dev 论坛上发布的相同问题的回应 => https://forums.developer.apple.com/message/35389
It's because Swift Arrays can only contain objects that are the same type. Your array has integers and a string. When you import UIKit the objects in the array become objects of type NSObject by virtue of Swift's inference engine. To see for yourself, add the line after the array definition, you'll see the type as Swift.array
import UIKit
var array = [1,2,3,"Swift2"]
array.dynamicType