i want to get the Length of an Array with "sizeof". I tried everything. This is the error message: "[Int32] is not convertible to T.Type"
我想用“sizeof”获得数组的长度。我尝试了一切。这是错误消息:“[Int32]不可转换为T.Type”
The Array has to be Int32.
数组必须是Int32。
var testArray: [Int32] = [2000,400,5000,400]
var arrayLength = sizeof(testArray)
2 个解决方案
#1
19
You can get the number of elements in an array simply with
您只需使用即可获得数组中的元素数
let count = testArray.count
and the total number of bytes of its elements with
和其元素的总字节数
var arrayLength = testArray.count * sizeof(Int32)
// Swift 3:
var arrayLength = testArray.count * MemoryLayout<Int32>.size
sizeof
is used with types and sizeofValue
with values, so both
sizeof与types和sizeofValue一起使用,因此两者都有
var arrayLength = sizeof([Int32])
var arrayLength = sizeofValue(testArray)
would compile. But that gives you the size of the struct Array
, not the size of the element storage.
会编译。但是这会给你struct array的大小,而不是元素存储的大小。
#2
1
In Xcode 8 with Swift 3 beta 6 there is no function sizeof (). But if you want, you can define one for your needs. The good news are, that this new sizeof function works as expected with your array.
在带有Swift 3 beta 6的Xcode 8中,没有函数sizeof()。但如果您愿意,可以根据需要定义一个。好消息是,这个新的sizeof函数与您的数组一起工作。
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] = [2000,400,5000,400]
var arrayLength = sizeof(testArray) // 16
You need all versions of the sizeof function, to get the size of a variable and to get the correct size of a data-type and of an array.
您需要sizeof函数的所有版本,以获取变量的大小并获得数据类型和数组的正确大小。
If you only define the second function, then sizeof(UInt8.self) and sizeof(Bool.self) will result in "8". If you only define the first two functions, then sizeof(testArray) will result in "8".
如果只定义第二个函数,那么sizeof(UInt8.self)和sizeof(Bool.self)将导致“8”。如果只定义前两个函数,那么sizeof(testArray)将导致“8”。
#1
19
You can get the number of elements in an array simply with
您只需使用即可获得数组中的元素数
let count = testArray.count
and the total number of bytes of its elements with
和其元素的总字节数
var arrayLength = testArray.count * sizeof(Int32)
// Swift 3:
var arrayLength = testArray.count * MemoryLayout<Int32>.size
sizeof
is used with types and sizeofValue
with values, so both
sizeof与types和sizeofValue一起使用,因此两者都有
var arrayLength = sizeof([Int32])
var arrayLength = sizeofValue(testArray)
would compile. But that gives you the size of the struct Array
, not the size of the element storage.
会编译。但是这会给你struct array的大小,而不是元素存储的大小。
#2
1
In Xcode 8 with Swift 3 beta 6 there is no function sizeof (). But if you want, you can define one for your needs. The good news are, that this new sizeof function works as expected with your array.
在带有Swift 3 beta 6的Xcode 8中,没有函数sizeof()。但如果您愿意,可以根据需要定义一个。好消息是,这个新的sizeof函数与您的数组一起工作。
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] = [2000,400,5000,400]
var arrayLength = sizeof(testArray) // 16
You need all versions of the sizeof function, to get the size of a variable and to get the correct size of a data-type and of an array.
您需要sizeof函数的所有版本,以获取变量的大小并获得数据类型和数组的正确大小。
If you only define the second function, then sizeof(UInt8.self) and sizeof(Bool.self) will result in "8". If you only define the first two functions, then sizeof(testArray) will result in "8".
如果只定义第二个函数,那么sizeof(UInt8.self)和sizeof(Bool.self)将导致“8”。如果只定义前两个函数,那么sizeof(testArray)将导致“8”。