Two swift functions increasing compiling time dramatically

落花浮王杯 提交于 2019-12-06 11:21:47

Your problem is the chained-plus. (It's always chained-plus; ok, not always but always…)

ageLabel.text = String(Int(round(minAgeSlider.value / 1))) + "-" + String(Int(round(maxAgeSlider.value / 1))) + " Years"

Replace this with:

ageLabel.text = "\(Int(round(minAgeSlider.value)))-\(Int(round(maxAgeSlider.value))) Years"

I'm fairly certain the /1 isn't helping you here. round + Int should do everything you meant.

The most likely cause of the slowdown is the way that you're concatenating strings with +. For some reason that I haven't been able to figure out, it's much faster (at least in Swift 2/3) to append strings with "\(string1) \(string2)" than to use string1 + string2. Same goes for arrays and their + operator. We saw a 100x decrease in compile time when we changed how we're doing concatenation.

Have you tried removing the division by 1 and casting your values to Double instead? They may already be Doubles, mind you. You may just need to call round().

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!