Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Swift compiler ignore unused functions?

Does the Swift compiler compile unused functions or does it ignore them?

like image 718
Esqarrouth Avatar asked Apr 27 '15 15:04

Esqarrouth


1 Answers

Unused functions do make your program larger.

This is pretty easy to test. Created a new Swift application as a Single View Application (iOS). In the generated ViewController.swift, add a function. Do a build (Command-B) and check the product's build folder. This is usually:

~/Library/Developer/Xcode/DerivedData/
    <AppName>-<IDString>/Build/Products/Debug-iphoneos/
    <AppName>.app/

Check the file size of <AppName>.

Comment out the function and do a build again. The file size will be smaller.

I created an app called SwiftSandbox and added this:

func doSomething() -> NSString {
    var v: NSMutableString = NSMutableString(string: "MyString is Funny")
    v.replaceCharactersInRange(NSMakeRange(3, 3), withString: "World")
    return v
}

When building with this function, I saw this in bash:

-rwxr-xr-x   1 test  staff  213648 Apr 27 11:33 SwiftSandbox*

With the function commented out:

-rwxr-xr-x   1 test  staff  142992 Apr 27 11:33 SwiftSandbox*

Note that I didn't change any optimization settings in Xcode so I can't be sure what effect that would have.

like image 198
Joel Murphy Avatar answered Sep 28 '22 08:09

Joel Murphy