Fast arrays can contain only one type. When you announced:
let arr = [1, "a", 2.88]
Swift made arrtype [NSObject]. You can check this by Option-clicking on arrto see its type. This only works because you imported Foundation (your import UIKitimport Foundation). Try to delete import UIKit.
1 2.88 NSNumber "a" NSString, [NSObject], Int s, String s, Double NSObjects. NSNumber NSString NSObject. Swift . [1, true, 2.88], [NSNumber].
NSNumber , , . Int Double. , , is. "" : " , ".
import Foundation
let n: NSNumber = 3.14
print(n is Int) // "true"
print(n is Double) // "true"
print(n is Bool) // "true"
print(n as! Int) // "3"
print(n as! Double) // "3.14"
print(n as! Bool) // "true"
. , arr [Any], , :
let arr:[Any] = [1, "a", 2.88]
let last = arr.last
if last is Int {
print("The last is Int.")
} else {
print("The last is not Int.") // "The last is not Int."
}
Swift Any , ( , , ). , , [Any].
, Swift [NSObject], Foundation , Cocoa Cocoa Touch API. API NSArray, [1, "a", 2.88] [NSNumber(integer: 1), NSString(string: "a"), NSNumber(double: 2.88)].