Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// CheckNSNumberTests.swift
// CheckNSNumberTests
//
import XCTest
class CheckNSNumberTests: XCTestCase {
func test_objCTypeFromCFNumber() {
let objCType: (NSNumber) -> UnicodeScalar = { number in
return UnicodeScalar(UInt8(number.objCType.pointee))
}
let types: [CFNumberType] = [.sInt8Type, .sInt16Type, .sInt32Type, .sInt64Type, .float32Type, .float64Type, .charType, .shortType, .intType, .longType, .longLongType, .floatType, .doubleType, .cfIndexType, .nsIntegerType, .cgFloatType, CFNumberType(rawValue: 17)!]
for type in types {
var value = 0
let number = CFNumberCreate(nil, type, &value) as NSNumber
print("\(type): \(objCType(number))")
}
}
}
extension CFNumberType: CustomStringConvertible {
public var description: String {
switch rawValue {
case CFNumberType.sInt8Type.rawValue: return "sInt8Type"
case CFNumberType.sInt16Type.rawValue: return "sInt16Type"
case CFNumberType.sInt32Type.rawValue: return "sInt32Type"
case CFNumberType.sInt64Type.rawValue: return "sInt64Type"
case CFNumberType.float32Type.rawValue: return "float32Type"
case CFNumberType.float64Type.rawValue: return "float64Type"
case CFNumberType.charType.rawValue: return "charType"
case CFNumberType.shortType.rawValue: return "shortType"
case CFNumberType.intType.rawValue: return "intType"
case CFNumberType.longType.rawValue: return "longType"
case CFNumberType.longLongType.rawValue: return "longLongType"
case CFNumberType.floatType.rawValue: return "floatType"
case CFNumberType.doubleType.rawValue: return "doubleType"
case CFNumberType.cfIndexType.rawValue: return "cfIndexType"
case CFNumberType.nsIntegerType.rawValue: return "nsIntegerType"
case CFNumberType.cgFloatType.rawValue: return "cgFloatType"
case 17: return "sInt128Type"
default: return "\(rawValue)"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment