I want to continually add values to a global/constant array. The problem is it just keeps overwriting the last value rather than adding it to the the array so I always end up with 2 values
Global array
struct Globals {
struct savedCalculationData {
static var dataArray = ["something here"]
}
}
Saved to array in another class
//code to copy string value...
//Add to global array
var copiedDataArray = Globals.savedCalculationData.dataArray
copiedDataArray.append ("\(copyText)") <--- string value
//Save array to defaults
NSUserDefaults.standardUserDefaults().setObject(copiedDataArray, forKey: "savedDataArray")
NSUserDefaults.standardUserDefaults().synchronize()
You're appending to a copy of the global array, but don't appear to ever set that updated array back, so the global array is not changing.
Assuming you want keep the global data saved whenever you append to it, I would recommend designing your Globals struct to take care of this automatically:
struct Globals {
// Convenience variable for the standard defaults
private static var Defaults: NSUserDefaults { return NSUserDefaults.standardUserDefaults() }
struct SavedCalculationData {
private static let DataArrayKey = "savedDataArray"
static var dataArray: [String] {
get {
return Defaults.objectForKey(DataArrayKey) as? [String] ?? []
}
set {
// This setter is called when the array contents change,
// not just when a new array is set
Defaults.setObject(newValue, forKey: DataArrayKey)
Defaults.synchronize()
}
}
}
}
Then you can update the global data more succinctly:
Globals.SavedCalculationData.dataArray.append(copyText)
Since Array is copied when it's assigned to new variables, you are not modifying Globals.savedCalculationData.dataArray at all.
You should append without copying:
Globals.savedCalculationData.dataArray.append("\(copyText)")
NSUserDefaults.standardUserDefaults().setObject(Globals.savedCalculationData.dataArray, forKey: "savedDataArray")
or write back copiedDataArray to Globals.savedCalculationData.dataArray
var copiedDataArray = Globals.savedCalculationData.dataArray
copiedDataArray.append ("\(copyText)")
Globals.savedCalculationData.dataArray = copiedDataArray
NSUserDefaults.standardUserDefaults().setObject(copiedDataArray, forKey: "savedDataArray")
Related
Consider a function which which assigns value to an array like so...
fileprivate func getMessages() {
if let myId = recipientGrp?.id,
let array = realm()?.objects(TheMessage.self)
.filter("ownerId = '\(myId)' || receiverId = '\(myId)'")
.sorted(byKeyPath: "id", ascending: true) {
allMessages = array
}
}
Here, allMessages is the array into which values are added. Also allMessages is declared like so....
var allMessages: Results<TheMessage>?
What I want to achieve is I want to remove all elements of the array allMessages. allMessages.removeAll doesn’t work. So how can I achieve that?
Because allMessages is a Results<Message>, you need to remove those objects from Realm to actually remove them.
let realm = // get a realm
try realm.write {
realm.delete(allMessages)
}
// allMessages will automatically become empty at this point.
I have a Dictionary and i want to save it to NSUserDefaults(or something else so I can have access to my variables after i have terminated the app) , I found an example:
var saved = NSUserDefaults.standardUserDefaults()
let dict = ["Name": "Paul", "Country": "UK"]
saved.setObject(dict, forKey: "SavedDict")
But when i used it to mine Dictionary it didn't work. (maybe because my dictionary it's a little bit different)
My Dictionary is made like this:
var userDictionary = [Int : Event]()
struct Event {
var sensorName: String
var sensorType: String
var sensorSub: String
}
And i add elements like this:
userDictionary[value] = Event(sensorName: "first", sensorType: "Temp", sensorSub: "Third")
And here is what i tried to do so I can store it.
saved.setObject(userDictionary, forKey: "valueDictionary")
And I get this error:
Cannot convert value of type '[Int : SensorsView.Event]' to expected
argument type 'AnyObject?'
To avoid this error I did this:
self.saved.setObject(self.userDictionary as? AnyObject, forKey: "valueDictionary")
But I can't retrieve what i saved
Unfortunately this question didn't help me after some comments i believe that the goal here is to convert my dictionary to Data (or something else) and after i retrieve it i convert it back to Dictionary
Try to convert the data to NSData and then retrieve like so:
/// Save
NSUserDefaults.standardUserDefaults().setObject(NSKeyedArchiver.archivedDataWithRootObject(object), forKey: key)
/// Read
var data = NSUserDefaults.standardUserDefaults().objectForKey(key) as NSData
var object = NSKeyedUnarchiver.unarchiveObjectWithData(data) as [String: String]
What i think is from the link below you will be able to store the Dictionary into NSUserDefaults
Swift NSUserDefaults not saving Dictionary?
or
Saving dictionary into NSUserDefaults may help
I managed to save my custom Dictionary using Realm!
Instead of a struct I use a different class like this:
import RealmSwift
class Sensors : Object {
dynamic var sensorName = ""
dynamic var sensorType = ""
dynamic var sensorSub = ""
}
and then I fill it like this:
var useOfRealm = try! Realm()
var allSensors = useOfRealm.objects(Sensors.self)
var saving = Sensors()
func fillThis() {
try! useOfRealm.write {
saving.sensorName = "something"
saving.sensorType = "something"
saving.sensorSub = "something"
useOfRealm.add(saving)
}
}
Use the function with parameters so you can fill the 'Dictionary' Dynamically.
Use allSensors so you can retrieve everything that you want.
I am currently trying to save an array to the user defaults.
Here is my code:
//where things will be stored
let defaults = NSUserDefaults.standardUserDefaults()
var taskNames = [String]()
var taskPriorities = [Float]()
//this is the function that saves tasks
func saveTask() {
println(taskNames)
println(taskPriorities)
defaults.setObject(taskNames, forKey: "taskName")
defaults.setObject(taskPriorities, forKey: "taskPriorities")
defaults.synchronize()
}
//this is the function that loads tasks
func loadTasks() {
var taskNamesLoad = defaults.dataForKey("taskName")
println(defaults.dataForKey("taskName"))
println(taskNamesLoad)
}
When I call the function to load the data (after saving some data with the other function of course) the output to the Console is nil and there is no data saved in the user defaults. How can I fix this?
You should use arrayForKey or objectForKey instead dataForKey. Because you store the Array object, not NSData.
Like below:
func loadTasks() {
var taskNamesLoad = defaults.arrayForKey("taskName")
println(defaults.arrayForKey("taskName"))
println(taskNamesLoad)
}
If you're going to save to user defaults you need to use plist safe objects. So instead of using Float you need to use NSNumber.
I have a Dictionary that holds another Dictionary that holds an Array which holds another Array of a custom class. I'm having a lot of trouble working with these can someone who this comes easy to tell me the ways I can define, initialize, and access and assign to either part specifically.
Dic = [String: [String: [[MyClass]]]]
Sorry if it's confusing.
This code shows you how to do what you asked, but the data structure you requested is quiet cumbersome to use. I'll recommend to think again about what you want to accomplish and review this data structure.
class MyClass {
var name : String
init(name: String) {
self.name = name
}
}
// Create your dictionary
var dic : [String: [String: [[MyClass]]]] = [:]
// Create a list of MyClass object
var list = [MyClass(name: "first"), MyClass(name: "second"), MyClass(name: "third")]
// Create a dictionary with string key and array of array of type MyList
var myClassDic = ["test": [list]]
// update or add new value via the updateValue method
dic.updateValue(myClassDic, forKey: "index1")
// update or add new value via the subscript
dic["index2"] = ["test2": [[MyClass(name: "forth"), MyClass(name: "fith")]]]
// Iterate over your outer dictionairy
for key in dic.keys {
// retrieve an entry from your outer dictionary
var tempDic = dic[key]
// Iterate over your inner dictionary
for sKey in tempDic!.keys {
// retrieve an array of array of MyList Object
var containerList = tempDic![sKey]
// iterate over the outer array
for listVal in containerList! {
//Iterate over the inner array
for sListVal in listVal {
print("\(sListVal.name) ")
}
println()
}
}
}
for example.. I want to pass in some dictionaries into a class in its initializer, and i want to reference those dictionaries across my class.. the problem is when I set them as properties, they are actually being copied, not referenced.
example:
var activeDict: [Int: Projectile]
var inactiveDict: [Int: Projectile]
init(inout activeDict: [Int: Projectile], inout inactiveDict: [Int: Projectile]) {
self.activeDict = activeDict
self.inactiveDict = inactiveDict
I want to use activeDict, and inactiveDict across my class. I want them to be references of the originals that are being passed in.
You can store an UnsafeMutablePointer to the dictionary, here is an example, tested in playground:
var dict = ["hejj": 1]
class A<T> {
var myDictRef: UnsafeMutablePointer<T>
init(ref: UnsafeMutablePointer<T>) {
self.myDictRef = ref
}
}
let a = A(ref: &dict)
a.myDictRef.memory["asd"] = 3
a.myDictRef.memory
dict
Updated
In your case use it like that:
var activeDict: UnsafeMutablePointer<[Int: Projectile]>
var inactiveDict: UnsafeMutablePointer<[Int: Projectile]>
init(activeDict: UnsafeMutablePointer<[Int: Projectile]>, inactiveDict: UnsafeMutablePointer<[Int: Projectile]>) {
self.activeDict = activeDict
self.inactiveDict = inactiveDict
}
whenever you want to use activeDict or inactiveDict, you can call the dictionary behind the pointer with their memory property like:
activeDict.memory[YourKey] = YourValue