[英]Write into Settings bundle in Swift
I have created an app with settings bundle in it and the app configuration can be changed. 我创建了一个带有设置捆绑包的应用程序,可以更改该应用程序的配置。 Reading from Settings bundle is done. 从“设置”捆绑中读取完成。 I am using below code. 我正在使用下面的代码。
let defs: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defs.synchronize()
var settingsBundle: NSString = NSBundle.mainBundle().pathForResource("Settings", ofType: "bundle")!
if(settingsBundle.containsString("")){
NSLog("Could not find Settings.bundle");
return;
}
var settings: NSDictionary = NSDictionary(contentsOfFile: settingsBundle.stringByAppendingPathComponent("Root.plist"))!
var preferences: NSArray = settings.objectForKey("PreferenceSpecifiers") as! NSArray
var defaultsToRegister: NSMutableDictionary = NSMutableDictionary(capacity: preferences.count)
for prefSpecification in preferences {
if (prefSpecification.objectForKey("Key") != nil) {
let key: NSString = prefSpecification.objectForKey("Key")! as! NSString
if !key.containsString("") {
let currentObject: AnyObject? = defs.objectForKey(key as! String)
if currentObject == nil {
// not readable: set value from Settings.bundle
let objectToSet: AnyObject? = prefSpecification.objectForKey("DefaultValue")
var objectKey : String = key as! String
var finalObject : String = objectToSet as! String
defaultsToRegister.setObject(finalObject, forKey: objectKey)
}else{
//already readable: don't touch
var objectKey : String = key as! String
var finalObject : String = currentObject as! String
defaultsToRegister.setObject(finalObject, forKey: objectKey)
}
}
}
}
defs.registerDefaults(defaultsToRegister as [NSObject : AnyObject])
defs.synchronize()
The same settings can be updated the app itself. 相同的设置可以更新应用程序本身。 But how I can write the updated values into the Settings bundle? 但是,如何将更新的值写入“设置”捆绑包中? In this link , the accepted answer says it is possible using NSUserDefaults
. 在此链接中 ,可接受的答案表示可以使用NSUserDefaults
。 But I couldn't, someonce can please help? 但是我不能,有人可以帮忙吗?
EDIT Please check below for more info: 编辑请检查下面的更多信息:
{
PreferenceSpecifiers = (
{
Title = Group;
Type = PSGroupSpecifier;
},
{
DefaultValue = Test1;
Key = "multi_values";
Title = "Multiple Values";
Titles = (
FIrst,
Second,
Third
);
Type = PSMultiValueSpecifier;
Values = (
FirstValue,
SecondValue,
ThirdValue
);
}
);
StringsTable = Root;
} }
I am having a Settings bundle like above format and I am able to read it in the app. 我有一个像以上格式一样的设置包,可以在应用程序中阅读它。 But how can I update this multi values selection as per I make the changes in the app? 但是,如何根据我在应用程序中所做的更改来更新此多值选择? I used the following code to update the Title value. 我使用以下代码更新了Title值。
var settings: NSDictionary = NSDictionary(contentsOfFile: settingsBundle.stringByAppendingPathComponent("Root.plist"))!
var preferences: NSArray = settings.objectForKey("PreferenceSpecifiers") as! NSArray
preferences.objectAtIndex(1).setValue("MutipleValue is updated", forKey: "Title")
settings.setValue(preferences, forKey: "PreferenceSpecifiers")
settings.writeToFile(settingsBundle.stringByAppendingPathComponent("Root.plist"), atomically: false)
In link that you provided has answer to your question 您提供的链接中有您问题的答案
Note that you shouldn't read from the settings bundle directly, as it makes no sense . 请注意,您不应直接从设置包中读取内容,因为这没有任何意义 。 You should always fetch and set user defaults using NSUserDefaults . 您应该始终使用NSUserDefaults获取并设置用户默认值 。 When the user makes a change in the settings application, NSUserDefaults will reflect this automatically. 当用户在设置应用程序中进行更改时,NSUserDefaults将自动反映出来。 They will always be kept in sync. 它们将始终保持同步。
When you create a new item in settings.bundle
, you should set it identifier
. 在settings.bundle
创建新项目时,应设置其identifier
。
After that, you can get required value with NSUserDefaults
by following way: 之后,您可以通过以下方式使用NSUserDefaults
获得所需的值:
if let export_enabled = NSUserDefaults.standardUserDefaults().valueForKey("export_enabled") as? Bool
{
if let export_interval = NSUserDefaults.standardUserDefaults().valueForKey("export_interval") as? Number
{
// ...
}
}
And change: 并更改:
NSUserDefaults.standardUserDefaults().setValue(true, forKey: "export_enabled")
Update 更新
iOS application on device has the following structure: 设备上的iOS应用程序具有以下结构:
AppTitle.app
AppTitle
AppTitleIcon.png
Info.plist
com.company.apptitle.plist (read/write)
settings.bundle (readonly)
Settings.bundle
intended for forming configuration hierarchy in the default Settings.app
. Settings.bundle
打算在默认成形结构层次Settings.app
。 It contains titles of sections and settings types, but settings values are stored in com.company.apptitle.plist
. 它包含部分标题和设置类型,但是设置值存储在com.company.apptitle.plist
。
Because settings.bundle
is read-only, you cannot change its structure or section titles. 因为settings.bundle
是只读的,所以您不能更改其结构或部分标题。 You can only change settings values and this change occurs in com.company.apptitle.plist
via NSUserDefaults
. 您只能更改设置值,并且此更改通过NSUserDefaults
在com.company.apptitle.plist
进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.