Swift 4中的过滤字典在Xcode中失败,但在Playground中成功 [英] Filtering dictionary in Swift 4 fails in Xcode, but succeeds in Playground

查看:80
本文介绍了Swift 4中的过滤字典在Xcode中失败,但在Playground中成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift 4 Playground中,此代码:

In a Swift 4 Playground this code:

let time = 1234
let description: String? = nil

let keyed: [String : Any?] = [
    "time": time,
    "description": description
    ]

let filtered: [String : String] = keyed
    .filter{ _, value in value != nil }
    .mapValues { value in return String(describing: value!) }

print(keyed)
print(filtered)

产生此输出:

["description": nil, "time": Optional(1234)]
["time": "1234"]

这正是我想要的(仅原始值不是nil的键值对,其中的值已解包并转换为字符串).但是,在Xcode 9(测试版3)中,我的构建失败,并显示'filter' is unavailable.这是beta ¯\_(ツ)_/¯之类的东西,还是我错过了什么?

Which is exactly what I want (only key-value pairs where the original value is not nil, with the value unwrapped and converted to a string). However in Xcode 9 (beta 3) my build fails with 'filter' is unavailable. Is this a beta ¯\_(ツ)_/¯ kind of thing, or am I missing something?

推荐答案

我尝试通过几种方法解决此问题,包括重新启动Xcode,清除DerivedData等,但当时没有任何效果.几天后,我回到该项目,发现以前无法构建的相同代码现在已成功构建(没有做任何相关更改).因此,我将此归咎于Xcode 9 beta的怪癖.或者,可能是某个地方出现了某种东西,然后Xcode最终清除了缓存或类似性质的东西. ¯\_(ツ)_/¯

I tried to resolve this a few ways, including rebooting Xcode, clearing DerivedData etc., but at the time nothing worked. I came back to the project several days later and found that the same code which had previously failed to build now built without issue (without my having made any relevant changes). So I'm blaming this on a quirk of the Xcode 9 beta. Or perhaps something was just gummed up somewhere and Xcode eventually cleared a cache or something of that nature. ¯\_(ツ)_/¯

这篇关于Swift 4中的过滤字典在Xcode中失败,但在Playground中成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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