简体   繁体   English

Swift如何知道Any数组中元素的地址?

[英]How does Swift know the address of an element in an array of Any?

How does Swift know the address of an element in an array of Any? Swift如何知道Any数组中元素的地址? I can't find any explanation.我找不到任何解释。 For example:例如:

var array: [Any] = [0, "1"]
print(array[1]) // print “1”

Usually, in other programming languages like C/C++, we can only declare an array of a specified type, therefore we can calculate the address of an element by: start + index * Element.size .通常,在其他编程语言如 C/C++ 中,我们只能声明一个指定类型的数组,因此我们可以通过start + index * Element.size来计算元素的地址。 How to calculate the address of an element of array with a specified index if we don't have a fixed size of elements in the array?如果数组中没有固定大小的元素,如何计算具有指定索引的数组元素的地址?

UPDATE: Apparently I'm not expecting Swift to behave like C.更新:显然我不希望 Swift 表现得像 C。 We certainly know how to access an array in Swift.我们当然知道如何访问 Swift 中的数组。 I'm wondering how Swift or other language implements accessing an array of heterogenous types.我想知道 Swift 或其他语言如何实现访问异构类型数组。

Alejandro Alonso has answered this question in the official Swift forum. Alejandro Alonso 在官方 Swift 论坛中回答了这个问题。 FYI:供参考:

https://forums.swift.org/t/how-does-swift-know-the-address-of-an-element-in-an-array-of-any/45415/3 https://forums.swift.org/t/how-does-swift-know-the-address-of-an-element-in-an-array-of-any/45415/3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM