Last active
August 18, 2016 20:36
-
-
Save RodolfoAntonici/57e1a49bb172c38df4f8e3f58771a321 to your computer and use it in GitHub Desktop.
A simple NSUserDefaults extension to input default values from Settings Bundle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSUserDefaultsExtension.swift | |
// Settings | |
// | |
// Created by Rodolfo Antonici on 18/08/16. | |
// Copyright © 2016 Rodolfo Antonici. All rights reserved. | |
// | |
import Foundation | |
extension NSUserDefaults { | |
func registerDefaultsFromSettingsBundle() { | |
guard let settingsBundle = NSBundle.mainBundle().pathForResource("Settings", ofType: "bundle") else { | |
print("Couldn't find Settings.bundle") | |
return | |
} | |
if let settings = NSDictionary(contentsOfFile: settingsBundle + "/Root.plist"), | |
let preferences = settings["PreferenceSpecifiers"] as? [[String : AnyObject]] { | |
var defaultsToRegister = [String : AnyObject]() | |
for preferenceSpecification in preferences { | |
if let key = preferenceSpecification["Key"] as? String where Array(preferenceSpecification.keys).contains("DefaultValue"){ | |
print("\(key) = \(preferenceSpecification["DefaultValue"])") | |
defaultsToRegister[key] = preferenceSpecification["DefaultValue"] | |
} | |
} | |
self.registerDefaults(defaultsToRegister) | |
self.synchronize() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment