In swift if I have a global constant such as let host = XXX in a file, the change of this constant will cause all project files recompiled
Asked
Active
Viewed 61 times
-2
-
1This is a statement, not a question. – Sergey Kalinichenko Sep 02 '16 at 03:06
-
i I mean, if I change the value of this constant , it will lead to a re-editing of this project. This makes me in trouble. – 李剑伟 Sep 02 '16 at 03:22
-
The fact that stuff would be recompiled seems logical, given that it could have easily performed some optimizations based upon these constants. You might be able to avoid the recompiling if you made it a `var`, but you probably lose some efficiencies. – Rob Sep 02 '16 at 04:36
1 Answers
0
Correct, changing something at that scope will cause the recompile.
Just the same as if you were to change a constant in a specific class, that class would be recompiled.
If you need to change values outside of the code, e.g. host
then it's probably better to use a plist file with some properties in it.
example:
settings.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>host</key>
<string>1.2.3.4</string>
</dict>
</plist>
example code to get value:
guard let settingsFileURL = NSBundle.mainBundle().URLForResource("settings", withExtension: "plst") else {
fatalError("unable to find plist file")
}
let settings = NSDictionary(contentsOfURL: settingsFileURL)
guard let host = settings?.valueForKey("host") as? String else {
fatalError("unable to get host value from plist file")
}
print("host = \(host)")

Adrian Sluyters
- 2,186
- 1
- 16
- 21