gpt4 book ai didi

Swift 1.2 将 [AnyObject?] 转换为 [AnyObject]

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

如果你看一下这个函数:

typealias JSONDictionary = [String: AnyObject]

private func allIdentifiersInJson(json: [JSONDictionary], key: String?) -> [AnyObject] {
if let identifier = key {
let identifiers = json.map{ $0[identifier] } //this returns [AnyObject?]
return identifiers as? [AnyObject] ?? [] // this crashes the compiler in Release with Optimizations
}
return []
}

如何将 [AnyObject?] 转换为 [AnyObject]

在启用优化的 Release 配置中,我在编译期间遇到此错误:

Bitcast requires both operands to be pointer or neither
%548 = bitcast i64 %547 to %objc_object*, !dbg !10033
LLVM ERROR: Broken function found, compilation aborted!

最佳答案

一种方法是使用 flatMap:

let l: [AnyObject?] = [1, 2, 3, 4, 5, 6, nil, 7]
let m = l.flatMap { $0 } // Returns [1, 2, 3, 4, 5, 6, 7] as [AnyObject]

(仅在 Swift 2 中测试过)

关于Swift 1.2 将 [AnyObject?] 转换为 [AnyObject],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31710785/

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