4

I want to store a UIColor in NSUserDefaults. However, this lead to some problems, so I figured I could save the UIColor as String to be converted back into a UIColor.

I decided to do this with a switch statement:

switch bc {
    case "UIColor.redColor()":
        blockColour = UIColor.redColor()
    case "UIColor.orangeColor()":
        blockColour = UIColor.orangeColor()
    case "UIColor.blueColor()":
        blockColour = UIColor.blueColor()
    case "UIColor.greenColor()":
        blockColour = UIColor.greenColor()
    case "UIColor.blackColor()":
        blockColour = UIColor.blackColor()
    case "UIColor.grayColor()":
        blockColour = UIColor.grayColor()
    case "UIColor.purpleColor()":
        blockColour = UIColor.purpleColor()
    default:
        println("ERROR!")
    }

However, I get errors on every case line, saying:

Type 'String' does not conform to protocol 'IntervalType'

I'm sure this can't be the most effective method or the easiest way to do this, but it's the only way I could figure out how to save a UIColor in NSUserDefaults.

What's the problem?

EDIT: Preceding code that shows how bc is defined:

var bc : String!
var blockColour : UIColor!

var userDefaults = NSUserDefaults.standardUserDefaults()

    if var blockColourString : AnyObject = userDefaults.valueForKey("blockColour") {
        blockColourString = userDefaults.valueForKey("blockColour")
        bc = blockColourString as String
    }
    else {
        var blockColourString : AnyObject = "UIColor.orangeColor()"
        userDefaults.setValue(blockColourString, forKey: "blockColour")
        bc = blockColourString as String
    }

    userDefaults.synchronize()
user2397282
  • 3,798
  • 15
  • 48
  • 94
  • How are you defining bc? – franklinexpress Aug 15 '14 at 14:03
  • Why don't you want to store UIColor instead? – Timur Bernikovich Aug 15 '14 at 14:04
  • Franklin, see my edit. Timur, it gives an error about attempting to insert a non-property list object for key blockColour? – user2397282 Aug 15 '14 at 14:09
  • What version of XCode 6 are you using? Is it beta 5? – Gergely Orosz Aug 15 '14 at 14:18
  • 3
    This is not a duplicate. Q: My car is not running; I think it needs gas. A: Take the bus. Yes, he should properly encode for userDefaults(); add that as an answer; let the poster decide if he wishes to avoid `switch/case`. – GoZoner Aug 15 '14 at 14:27
  • 1
    @GoZoner This is the A/B problem as discussed endlessly on meta. The problem is how to store colours in user defaults. The answer is to store them as data, not to do some ridiculous technique to encode them as a string, ask a vague question about switch statements not working, and help the questioner carry on getting things increasingly wrong. The questioner even says *I'm sure this can't be the most effective method or the easiest way to do this, but it's the only way I could figure out how to save a UIColor in NSUserDefaults.* _That's_ the question being asked here. – jrturton Aug 15 '14 at 14:35

1 Answers1

2

This is a duplicate, but the duplicate does not have a swift equivalent. Here's that, untested:

One way of doing it might be to archive it (like with NSColor, though I haven't tested this):

let colorData = NSKeyedArchiver.archivedDataWithRootObject(color)
NSUserDefaults.standardUserDefaults.setObject(colorData forKey:"myColor")

And to get it back:

var color : UIColor?
if let colorData = NSUserDefaults.standardUserDefaults.objectForKey("myColor") as? NSData {
    color = NSKeyedUnarchiver.unarchiveObjectWithData(colorData) as UIColor
}
jrturton
  • 118,105
  • 32
  • 252
  • 268