在回答另一個問題的過程中,我遇到了遊樂場中的一個奇怪的錯誤。我要測試下面的代碼,如果對象是一個Array
,Dictionary
或Set
:遊樂場和項目之間的差異
import Foundation
func isCollectionType(value : AnyObject) -> Bool {
let object = value as! NSObject
return object.isKindOfClass(NSArray)
|| object.isKindOfClass(NSDictionary)
|| object.isKindOfClass(NSSet)
}
var arrayOfInt = [1, 2, 3]
var dictionary = ["name": "john", "age": "30"]
var anInt = 42
var aString = "Hello world"
println(isCollectionType(arrayOfInt)) // true
println(isCollectionType(dictionary)) // true
println(isCollectionType(anInt)) // false
println(isCollectionType(aString)) // false
代碼和預期一樣,當我把它變成一個斯威夫特項目或在命令行中運行它。然而遊樂場不會進行編譯,並給我就垂頭喪氣以下錯誤NSObject
:
Playground execution failed: Execution was interrupted, reason: EXC_BAD_ACCESS (code=2, address=0x7fb1d0f77fe8).
* thread #1: tid = 0x298023, 0x00007fb1d0f77fe8, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x7fb1d0f77fe8)
* frame #0: 0x00007fb1d0f77fe8
frame #1: 0x000000010ba46e12 libswiftCore.dylib`Swift._EmptyArrayStorage._withVerbatimBridgedUnsafeBuffer (Swift._EmptyArrayStorage)<A>((Swift.UnsafeBufferPointer<Swift.AnyObject>) -> A) -> Swift.Optional<A> + 50
構建平臺是OS X在所有三種情況。有誰知道如何讓Playground玩?
Xcode 6.3.2。 Swift 1.2。 OS X 10.10.3約塞米蒂
值得注意的是,這會影響基元數組以及CoreFoundation類型。 – Pranav