Identify slow code to optimize build time

Jay*_*ing 6 xcode ios swift

I'm using these Compilation Swift Flag to identify codes that slow down the compilation time:

-Xfrontend -warn-long-function-bodies=100
-Xfrontend -warn-long-expression-type-checking=100
Run Code Online (Sandbox Code Playgroud)

Then after building, I get warnings like these:

Instance method 'startFadePositionTitle()' took 2702ms to type-check (limit: 500ms)

for this part of the code:

    func startFadePositionTitle() -> CGFloat {
        let value: CGFloat = ((backgroundImage.frame.height/2 - contentTitle.frame.height/2) - navbarView.frame.height)/2
        return value
    }
Run Code Online (Sandbox Code Playgroud)

Can someone explains me what is wrong in this method and what could I possibly improve?

Moj*_*ini 9

你应该把它分成更小的块,这样 Swift 就可以更轻松地进行类型检查。而且你说的越多,Swift 就越不需要思考。所以你可以帮助编译器并告诉它你已经知道的任何事情:

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
}
Run Code Online (Sandbox Code Playgroud)

实例方法 'beginFadePositionTitle()' 花了 1毫秒进行类型检查(限制:1 毫秒)

这是当您将所有内容告诉编译器时的结果。看到不同?