9

I'm building an iPhone app using Swift. I've created a Settings class and declared some static variables in them, for storing colors. However, whenever I try to access the first variable I've declared (such as Settings.grayBorderColor below), the app crashes (with some message about Settings.grayBorderColor.unsafeMutableAddressor). I can access any properties below the first one just fine, and if I switch the order of the properties/variables, it is still accessing whichever property is declared first that causes the crash.

class Settings {

    // MARK: Properties

    static let grayBorderColor = UIColor(red: 0.76, green: 0.76, blue: 0.76, alpha: 1.0)
    static let lightGreenColor = UIColor(red: 0.66, green: 1.0, blue: 0.66, alpha: 1.0)
    static let darkGreenColor = UIColor(red: 0.66, green: 0.0, blue: 0.0, alpha: 1.0)
    static let darkRedColor = UIColor(red: 0.66, green: 0.0, blue: 0.0, alpha: 1.0)
    static let lightRedColor = UIColor(red: 1, green: 0.66, blue: 0.66, alpha: 1.0)
    static let lightGrayColor = UIColor.lightGrayColor()
    static let mediumGrayColor = UIColor.darkGrayColor()

}

What am I doing wrong?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • Can you try to rename the first one? – Twitter khuong291 Dec 17 '15 at 03:06
  • It still crashes if I rename it. What I meant is, if for example I change darkGreenColor to the first variable/property, then Settings.darkGreenColor causes the crash instead of settings.grayBorderColor. It seems whatever property is first always crashes. –  Dec 17 '15 at 03:12
  • Maybe it is a bug in Xcode, your code right. – Twitter khuong291 Dec 17 '15 at 03:24
  • Post the full error that Xcode throws – Shripada Dec 17 '15 at 04:35
  • There isn't really an error, it just crashes and says "(lldb)". But under the Thread list, the first item is: "0 Settings.grayBorderColor.unsafeMutableAddressor" –  Dec 17 '15 at 04:51
  • It's a weird problem. Did you try changing class settings to struct settings. If you only need this class for settings such as you mentioned than you might as well just make it a struct. Maybe that fixes it – crashoverride777 Dec 17 '15 at 16:24
  • Thanks for the tip... tried changing it to a struct, but getting the same crash behavior unfortunately. –  Dec 19 '15 at 00:43
  • Did you find the fix? I am getting the same `unsafeMutableAddressor` crash log from _Crashlytics_, but I am not able to repro. Can you provide your demo code which crashes? Here's my question if you can provide any hint [http://stackoverflow.com/questions/37732916/ios-swift-unsafemutableaddressor-crash-on-ios-8](http://stackoverflow.com/questions/37732916/ios-swift-unsafemutableaddressor-crash-on-ios-8) – O Fenômeno Jun 09 '16 at 17:52
  • I encounter this problem too. But it did not happen before with the same codes. It's a very strange crash. – AechoLiu Apr 17 '17 at 05:42

1 Answers1

-4

Change your code to this:

class Settings {

    // MARK: Properties

    static let grayBorderColor: UIColor! = UIColor(red: 0.76, green: 0.76, blue: 0.76, alpha: 1.0)
    static let lightGreenColor: UIColor! = UIColor(red: 0.66, green: 1.0, blue: 0.66, alpha: 1.0)
    static let darkGreenColor: UIColor! = UIColor(red: 0.66, green: 0.0, blue: 0.0, alpha: 1.0)
    static let darkRedColor: UIColor! = UIColor(red: 0.66, green: 0.0, blue: 0.0, alpha: 1.0)
    static let lightRedColor: UIColor! = UIColor(red: 1, green: 0.66, blue: 0.66, alpha: 1.0)
    static let lightGrayColor: UIColor! = UIColor.lightGrayColor()
    static let mediumGrayColor: UIColor! = UIColor.darkGrayColor()

}

You were missing property types. As stated in the documentation, calculated properties should have type specifier.

Dennis Pashkov
  • 934
  • 10
  • 24