【发布时间】:2017-05-11 02:58:40
【问题描述】:
我在解析和创建来自服务器的 json 数据的一些组合方面进行了大量计算。整个过程需要很多时间,主要是我已经修复了与代码相关的问题,但是时间分析器在一个地方显示了一个我无法弄清楚的特定调用所花费的时间。
在我的处理过程中,我进行了大量的选角。它创建了很多 FlightFare 类型的对象,我从字典中创建它。
所以conveninece init 如下所示,我怎样才能避免它..?
convenience init (dictionary: [String:AnyObject]) {
self.init()
refundType = dictionary["rt"] as! String
if let unwrappedScore = dictionary["r"] as? Double {
score = unwrappedScore
}
if let unwrappedValue = dictionary["t"] as? Int {
taxes = unwrappedValue
}
if let unwrappedValue = dictionary["bf"] as? Int {
baseFare = unwrappedValue
}
if let unwrappedValue = dictionary["f"] as? Int {
fee = unwrappedValue
}
if let unwrappedValue = dictionary["d"] as? Int {
discount = unwrappedValue
}
if let unwrappedValue = dictionary["tf"] as? Int {
fare = unwrappedValue
}
if let unwrappedValue = dictionary["ttf"] as? Int {
totalFare = unwrappedValue
}
if let unwrappedValue = dictionary["hbo"] as? Bool {
hbo = unwrappedValue
}
providerKey = dictionary["pk"] as? String
hbf = dictionary["hbf"] as? [String]
}
【问题讨论】:
-
使用 [String: AnyObject] 而不是 [String: Any] 的任何特殊原因? AnyObject 确实在我在这里做的一些测试中增加了一些开销。
-
@MarcoPompei 没有理由,在 swift 2.3 中使用它,所以保持这种方式。我会试试 Any
-
如果对象的属性是可选的,那么
if let...构造是不必要的开销。只需将条件向下转换直接分配给属性
标签: ios iphone swift profiling