Swift Dictionary Taking Long Time to Compile
There isn't really a 'best' answer in here since all the answers are very helpful. The fastest way for a large, Swift dictionary to compile is to not let the compiler infer the type of anything. That means you need to define the data type of every item in the dictionary (including the dictionary itself).
The fastest way my dictionary compiled was:
try realm.write {
let dict: [String: Any] = ["itemgroup": item.itemgroup as String,
"itembrand": item.itembrand as String,
"itemtype": item.itemtype as String,
"itemsubtype": item.itemsubtype as String,
"basedescription": item.basedescription as String,
"info": item.info as String,
"upc": item.upc as String,
"upc2": item.upc2 as String,
"upc3": item.upc3 as String,
"upc4": item.upc4 as String,
"upc5": item.upc5 as String,
"baseprice": item.baseprice as Double,
"proprice": item.proprice as Double,
"retailprice": item.retailprice as Double,
"stdprice": item.stdprice as Double,
"caseqty": item.caseqty as Int,
"spord": item.spord as String,
"category": item.category as String,
"categorycode": item.categorycode,
"allowinbc": item.allowinbc as String,
"allowinab": item.allowinab as String]
realm.create(Product.self, value: dict, update: true)
}
Swift compiler speed with array concatenation
Unfortunately, I can't provide any explanation on why the compiler isn't able to evaluate this fairly simple array. However, the swift compiler is often overwhelmed by any big / slightly complex array expressions. I see, therefore, three possible solutions for your case:
- Write the contents to a auxiliary file and read it from there, for example in JSON or CSV format
- manually unravel the contents (i.e. don't write
... + ["A", "B"].unflat()
but[["A"], ["B"]]
- Use a closure to initialize the array like so:
let EnglishLayout: [[String]] = {
var layout: [[String]] = [
["1", "!"],
["2", "@"],
["3", "#"],
["4", "$"],
["5", "%"],
["6", "^"],
["7", "&"],
["8", "*"],
["9", "(", "-", "_"],
["0", ")", "=", "+"],
["Q", "`", "~"]
]
layout += ["W", "E", "R", "T", "Y", "U", "I"].unflat()
layout += [
["O", "[", "{"],
["P", "]", "}"]
]
layout += ["A", "S", "D", "F", "G", "H", "J"].unflat()
layout += [
["K", ";", ":"],
["L", "'", "\""],
["\\", "|"]
]
layout += ["Z", "X", "C", "V", "B", "N", "M"].unflat()
layout += [
[",", "<"],
[".", ">"],
["/", "?"]
]
return layout
}()
Slow compile time in Xcode in specific methods
This expression and the related one are very complicated:
var moveAction = SKAction.sequence([SKAction.move(by: CGVector(dx: (-scene.size.width * direction * 0.26) - (scene.size.width * 0.18 * direction), dy: 0), duration: 0.2), SKAction.move(by: CGVector(dx: scene.size.width * direction * 0.18, dy: 0), duration: 0.1)])
Break these up with intermediate variables. The problem isn't figuring out the final type. The problem is working out all the possibilities for *
and -
. Yes, there is only one valid possibility, but the compiler has to check every possible combination of types to make sure that the expression isn't ambiguous. In particular, it has to do a lot of work deciding whether 0.26
should be a CGFloat
, Float
, or Double
.
Something more along these lines:
for icon in 0...3 {
let currentIcon : SKSpriteNode = pauseIcons[icon]
let moveAction: SKAction
if icon == 3 {
let moveBy1 = CGVector(dx: -scene.size.width * direction, dy: 0)
let moveBy2 = CGVector(dx: scene.size.width * direction * 0.25, dy: 0)
moveAction = SKAction.sequence([
SKAction.move(by: moveBy1), duration: 0.3),
SKAction.move(by: moveBy2, duration: 0.15)])
} else {
let moveBy1 = CGVector(dx: (-scene.size.width * direction * 0.26) - (scene.size.width * 0.18 * direction), dy: 0)
let moveBy2 = CGVector(dx: scene.size.width * direction * 0.18, dy: 0)
moveAction = SKAction.sequence([SKAction.move(by: moveBy1, duration: 0.2),
SKAction.move(by: moveBy2, duration: 0.1)])
}
currentIcon.run(moveAction)
}
Identify slow code to optimize build time
You should break it to smaller chunks, then Swift can do type check more easily. Also the more you tell, the less Swift has to think. So you can help compiler and tell it anything you already know:
func beginFadePositionTitle() -> CGFloat {
let n: CGFloat = 2
let a: CGFloat = self.backgroundImage.frame.height/n
let b: CGFloat = self.contentTitle.frame.height/n
let ab: CGFloat = a - b
let c: CGFloat = self.navbarView.frame.height
let abc: CGFloat = ab - c
return abc/n
}
Instance method 'beginFadePositionTitle()' took 1ms to type-check (limit: 1ms)
This is the result when you tell everything to compiler. See the difference?
Xcode 8.0 Swift 3.0 slow indexing and building
I solved the problem by commenting all files and then removing comments one by one. I found that the problem is still in the array declaration as described here.
I had code like this and project was not indexing:
class {
var first: String!
var second: String!
var third: String!
var fourth: String!
var fifth: String!
func abc() -> [String] {
var array = [first, second, third, fourth, fifth]
}
}
I've changed it to this and indexing started working:
class {
var first: String!
var second: String!
var third: String!
var fourth: String!
var fifth: String!
func abc() -> [String] {
var array = [first]
array.append(second)
array.append(third)
array.append(fourth)
array.append(fifth)
}
}
Related Topics
Geofire/Firebase Function Is Executing Handler Multiple Times in Swift
How to Save Re-Ordered Tableview Cells to Core Data
Add Catextlayer on Top of Nsimageview
Pie Chart Entries Outside Slices Have Different Position Offsets
Convert Ble Current Time to Date
How to Present Ads During Certain Scenes in Sprite-Kit
Swiftui Change on Multilevel Children Published Object Change
How to Calculate The Camera Position from Vuforia Gl Matrix
Swiftui Wrapping Hstack with Images
Public Default Init in Protocol
How to Implement Doubly Linked List in Swift
Communicate Data Between Watchos & Today Extension Widget
How to Get Address in English Language Only Using Gmsgeocoder
Removing Scheduled Local Notification
Why Are Implicitly Unwrapped Variables Now Printing Out as Some(...) in Swift 4.1
Do Not Copy Swift Libraries with Xcode 8
Realm Write Transaction Failing, Despite Being in Transaction