gpt4 book ai didi

ios - 来自 AnyClass 或 dynamicType 的数组,Swift

转载 作者:搜寻专家 更新时间:2023-11-01 05:38:06 25 4
gpt4 key购买 nike

有没有办法在 Swift 2.1 中实现这一点

class Apple
{
}

class Fruit
{
let apples = [Apple]();
}

let fruit = Fruit();

let appleType: AnyClass = Apple.self;
let applesType: Any = fruit.apples.self.dynamicType;

let properties = Mirror(reflecting: fruit).children;
for property in properties
{
let magicalApples = property.value;

let array = Array<appleType>(); // creating array of Apple from type

let apples = magicalApples as! Array<appleType> // typecasting to array of Apple from type
let moreApples = magicalApples as! applesType // typecasting to [Apple] from type
let anyApples = magicalApples as! Array<AnyObject> // error SIGABRT, is this a bug?
}

评论目标抛出错误,“使用未声明的类型”。

我的目标是了解存储在 var 中的类型 appleType 是否可以用作 Type

最佳答案

不,你不能分配一个新类型,因为它是一个 let。
Cannot assign to immutable expression of type 'ClassName.Type'

-- 更新--

let anyApples = magicalApples as! Array<AnyObject>应该是
let anyApples = magicalApples as! Array<Any>

-- 更新 2 --

查看references

关于ios - 来自 AnyClass 或 dynamicType 的数组,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34460875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com