import Foundation
import ReactiveCocoa

struct Settings {
  private static let serverAddressKey = "SceneLauncher.serverAddress"
  
  // TODO only let non-empty-values out?
  static let serverAddress = { () -> MutableProperty<String?> in
    let property = MutableProperty(NSUserDefaults.standardUserDefaults().stringForKey(serverAddressKey))
    let ignoreInitialValue: SignalProducer<String?, NoError> = property.producer |> skip(1)
    // TODO compiler crashes if we do "producer |> skip |> start"
    ignoreInitialValue.start(next: {
      NSUserDefaults.standardUserDefaults().setObject($0, forKey: serverAddressKey)
    })
    return property
  }()

  // TODO remove
  static func getServerAddress() -> String? {
    return serverAddress.value
  }
  
  static func setServerAddress(newAddress: String) {
    serverAddress.put(newAddress)
  }
}
import Foundation
import ReactiveCocoa

struct Settings {
  private static let serverAddressKey = "SceneLauncher.serverAddress"
  
  // TODO only let non-empty-values out?
  static let serverAddress = { () -> MutableProperty<String?> in
    let property = MutableProperty(NSUserDefaults.standardUserDefaults().stringForKey(serverAddressKey))
    let ignoreInitialValue: SignalProducer<String?, NoError> = property.producer |> skip(1)
    ignoreInitialValue.start(next: { println("Yes sir got \($0)") })
    return property
  }()

  // TODO remove
  static func getServerAddress() -> String? {
    return serverAddress.value
  }
  
  static func setServerAddress(newAddress: String) {
    serverAddress.put(newAddress)
    // TODO do this when serverAddress changes
    NSUserDefaults.standardUserDefaults().setObject(newAddress, forKey: serverAddressKey)
  }
}
--- 
+++ 
@@ -8,7 +8,10 @@
   static let serverAddress = { () -> MutableProperty<String?> in
     let property = MutableProperty(NSUserDefaults.standardUserDefaults().stringForKey(serverAddressKey))
     let ignoreInitialValue: SignalProducer<String?, NoError> = property.producer |> skip(1)
-    ignoreInitialValue.start(next: { println("Yes sir got \($0)") })
+    // TODO compiler crashes if we do "producer |> skip |> start"
+    ignoreInitialValue.start(next: {
+      NSUserDefaults.standardUserDefaults().setObject($0, forKey: serverAddressKey)
+    })
     return property
   }()
 
@@ -19,7 +22,5 @@
   
   static func setServerAddress(newAddress: String) {
     serverAddress.put(newAddress)
-    // TODO do this when serverAddress changes
-    NSUserDefaults.standardUserDefaults().setObject(newAddress, forKey: serverAddressKey)
   }
 }
Save server address when it changes
