Double Number Type - Int in Swift

// Swift
import UIKit
let arr = [1, "a", 2.88]
let last = arr.last
if last is Int {
    print("The last is Int.")    // The last is Int.
} else {
    print("The last is not Int.")
}

I can not understand the result printed.
Why he prints "Last is Int."

// Swift
import UIKit
let arr = [1, "a", -2]
let last = arr.last
if last is Double {
    print("The last is Double.")    // The last is Double.
} else {
    print("The last is not Double.")
}

And this print is "The Last Double." Why? Can anyone help me out?
What are you changing a lot.

+4
source share
4 answers

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)].

+10

arr,

let arr = [1, "a", 2.88] // arr is [NSObject]

let arr = [1, "a", 2.88]
let last = arr.last // last is a NSObject?
if last is Int { // is Int or is Double will get the same result
    print("The last is Int.") // because the number of NSNumber is larger than the number of NSString
} else {
    print("The last is not Int.")
}

:

let arr = [1, "a", "b"]
let last = arr.last
if last is Int {
    print("The last is Int.")   
} else {
    print("The last is not Int.") // Will print because the number of NSString is larger than NSNumber.
}
0

[Any], :

let arr: [Any] = [1, "a", 2.88]
arr.last is Int // false
0

Foundation.framework, : , , , NSNumber. Foundation (.. Swift), let arr = [1, "a", 2.88] ( , , let arr = [1,2] - [Int]), let arr:[Any] = [1, "a", 2.88] - NSNumber (NSNumber ), - .

NSNumber - , () , as is Swift NSNumber , . , :

var a = NSNumber(bool: true)
print("\(a is Bool)")   // prints "true"
print("\(a is Int)")    // prints "true"
print("\(a is Double)") // prints "true"
print("\(a is Float)")  // prints "true"

, , - NSNumber, CoreFoundation CFNumber:

extension NSNumber {
    var isBoolean:Bool {
        return CFNumberGetType(self as CFNumber) == CFNumberType.CharType
    }

    var isFloatingPoint:Bool {
        return CFNumberIsFloatType(self as CFNumber)
    }

    var isIntegral:Bool {
        return CFNumberGetType(self as CFNumber).isIntegral
    }
}

extension CFNumberType {
    var isIntegral:Bool {
        let raw = self.rawValue
        return (raw >= CFNumberType.SInt8Type.rawValue && raw <= CFNumberType.SInt64Type.rawValue)
                || raw == CFNumberType.NSIntegerType.rawValue
                || raw == CFNumberType.LongType.rawValue
                || raw == CFNumberType.LongLongType.rawValue
    }
}

- .

, :

... , ...

.

0

All Articles