In Xcode 8 con Swift 3 beta 6 non v'è alcuna funzione sizeof(). Ma se vuoi, puoi definirne uno per le tue esigenze. Questa nuova funzione sizeof funziona come previsto con un array. Questo non era possibile con la vecchia funzione sizeof integrata.
let bb: UInt8 = 1
let dd: Double = 1.23456
func sizeof <T> (_ : T.Type) -> Int
{
return (MemoryLayout<T>.size)
}
func sizeof <T> (_ : T) -> Int
{
return (MemoryLayout<T>.size)
}
func sizeof <T> (_ value : [T]) -> Int
{
return (MemoryLayout<T>.size * value.count)
}
sizeof(UInt8.self) // 1
sizeof(Bool.self) // 1
sizeof(Double.self) // 8
sizeof(dd) // 8
sizeof(bb) // 1
var testArray: [Int32] = [1,2,3,4]
var arrayLength = sizeof(testArray) // 16
avete bisogno di tutte le versioni della funzione sizeof, per ottenere la dimensione di una variabile e per ottenere la dimensione corretta di un tipo di dati e di un array.
Se si definisce solo la seconda funzione, quindi sizeof (UInt8.self) e sizeof (Bool.self) risulteranno in "8". Se definisci solo le prime due funzioni, sizeof (testArray) risulterà in "8".
fonte
2016-08-19 12:27:12
o 'let anIntSize = sizeofValue (anInt)' – user102008