斯威夫特:如何使用 sizeof? [英] Swift: How to use sizeof?

查看:30
本文介绍了斯威夫特:如何使用 sizeof?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在使用 Swift 时与 C API 集成,我需要使用 sizeof 函数.在 C 中,这很容易.在 Swift 中,我陷入了类型错误的迷宫.

In order to integrate with C API's while using Swift, I need to use the sizeof function. In C, this was easy. In Swift, I am in a labyrinth of type errors.

我有这个代码:

var anInt: Int = 5
var anIntSize: Int = sizeof(anInt)

第二行有错误'NSNumber' 不是'T.Type' 的子类型".这是为什么,我该如何解决?

The second line has the error "'NSNumber' is not a subtype of 'T.Type'". Why is this and how do I fix it?

推荐答案

使用sizeof如下:

Use sizeof as follows:

let size = sizeof(Int)

sizeof 使用类型作为参数.

如果您想要anInt 变量的大小,您可以将dynamicType 字段传递给sizeof.

If you want the size of the anInt variable you can pass the dynamicType field to sizeof.

像这样:

var anInt: Int = 5
var anIntSize: Int = sizeof(anInt.dynamicType)

或者更简单(由 user102008 指出):

Or more simply (pointed out by user102008):

var anInt: Int = 5
var anIntSize: Int = sizeofValue(anInt)

这篇关于斯威夫特:如何使用 sizeof?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆