I am trying to add a value to an array and store in userdefaults but I got error intitialy when trying to append the new value to the value pulled. below is my code.
private func putArray(_ value: GMSAutocompletePrediction?, forKey key: String) {
guard let value = value else {
return
}
let newArray = getArray(forKey: key)?.append(value) // error here
storage.setValue(NSKeyedArchiver.archivedData(withRootObject: value), forKey: key)
}
private func getArray(forKey key: String) -> [GMSAutocompletePrediction]? {
guard let data = storage.data(forKey: key) else { return nil}
return NSKeyedUnarchiver.unarchiveObject(with: data) as? [GMSAutocompletePrediction]
}
below is my error
Cannot use mutating member on immutable value: function call returns immutable value
The problem is getArray(forKey: key)? is immutable , you can't attach append directly to it , so you need
var newArray = getArray(forKey: key) ?? []
newArray.append(value)
Related
I'm trying to save array added from tableview using this function:
class func saveArray(_ value: [Dictionary<String, AnyObject>], key: String) {
let data = NSKeyedArchiver.archivedData(withRootObject: value)
UserDefaults.standard.set(data, forKey: key)
}
Below is the function where I want to save the array:
func addItemCat(items: [Data]) {
print("ITEM: ", items)
dataSource.myListTableViewController.myListArr = items
self.myListTV.isHidden = false
UserDefaultsHelper.saveArray(items, key: Constants.myList.myList)
}
However, I got this error: Cannot convert value of type '[Data]' to expected argument type '[Dictionary String, AnyObject ]'
Below is my Data model:
data model screencap
I'm new to Swift and I hope someone can explain what is the problem.
Problem is with data types, saveArray function expects value parameter of type array of dictionary [Dictionary<String, AnyObject>], but you are passing array of data model objects which is a type-mismatch error.
To solve this:
First, You should not use pre-defined keywords for creating your custom object. Use DataObject instead:
struct DataObject {
}
Now change your saveArray function as:
class func saveArray(_ value: [DataObject], key: String) {
let data = NSKeyedArchiver.archivedData(withRootObject: value)
UserDefaults.standard.set(data, forKey: key)
}
and addItemCat, function as:
func addItemCat(items: [DataObject]) {
print("ITEM: ", items)
dataSource.myListTableViewController.myListArr = items
self.myListTV.isHidden = false
UserDefaultsHelper.saveArray(items, key: Constants.myList.myList)
}
I am coding a Swift class that builds a dictionary when instantiating it, plus the possibility of adding pairs of key - value, but once done the problem is I can not access the individual pairs I add, so there must be a problem somewhere I can not detect, please help
class MyDictionary {
var dictionary = ["":""]
init() {
}
func addPair(key: String, value: String) {
//dictionary["key"] = "value"
dictionary.updateValue(value, forKey: key)
}
}
// just to compare... this one works
var dict = ["one": "abc"]
dict["two"] = "efg"
print(dict["two"]!)
let myDictionary = MyDictionary()
myDictionary.addPair(key: "key1", value: "value1")
print(myDictionary)
//print(myDictionary["key1"]) //Does not work
Change
print(myDictionary["key1"])
To
print(myDictionary.dictionary["key1"])
I a assuming you want to create a custom dictionary, because you'd like to check (maybe in future) if the values to be added as as a key-pair are acceptable or so.
Anyway, if you want to have a custom dictionary class to act as a wrapper over the system Dictionary class, I'd advice to make the dictionary property private (so no outsiders can access and modify it directly), and add an additional method for getting the value for a key (if such key exists):
class MyDictionary {
private var dictionary = ["":""]
init() {
}
func addPair(key: String, value: String) {
dictionary.updateValue(value, forKey: key)
}
func valueForKey(_ key: String) -> String? {
if let valueForKey = dictionary[key] {
return valueForKey
}
return nil
}
}
This is how would you use it:
let myDictionary = MyDictionary()
myDictionary.addPair(key: "key1", value: "value1")
if let valueForKey = myDictionary.valueForKey("key1") {
print(valueForKey)
}
If you really want to get the value the way you mentioned it's not working, you'll need to implement a subscript, because MyDictionary is a custom class and does not have one yet:
class MyDictionary {
private var dictionary = ["":""]
/* more code */
func valueForKey(_ key: String) -> String? {
if let valueForKey = dictionary[key] {
return valueForKey
}
return nil
}
subscript(_ key: String) -> String? {
get {
return valueForKey(key)
}
}
}
This is how would you use the subscript:
let myDictionary = MyDictionary()
myDictionary.addPair(key: "key1", value: "value1")
if let valueForKey = myDictionary["key1"] {
print(valueForKey)
print(myDictionary["key1"]!)
}
Note that myDictionary["key1"] returns an Optional String, or String?, therefore you'd have to force unwrap the value in order for the print() method not to complain. Since we do it inside the if let statement - we know myDictionary["key1"] is not nil, and myDictionary["key1"]! must be some valid String object.
See more here: https://docs.swift.org/swift-book/LanguageGuide/Subscripts.html
To be able to use your syntax add key subscription support (getter and setter).
class MyDictionary {
var dictionary = ["":""]
init() {
}
func addPair(key: String, value: String) {
dictionary.updateValue(value, forKey: key)
}
subscript(_ key: String) -> String? {
get { return dictionary[key] }
set { dictionary[key] = newValue }
}
}
let myDictionary = MyDictionary()
myDictionary.addPair(key: "key1", value: "value1")
print(myDictionary["key1"]!)
You could extend the class as generic to use it with different types. The subscription setter makes addPair even redundant.
class MyDictionary<K : Hashable, V> {
var dictionary = [K:V]()
subscript(_ key: K) -> V? {
get { return dictionary[key] }
set { dictionary[key] = newValue }
}
}
let myDictionary = MyDictionary<String,String>()
myDictionary["key1"] = "value1"
print(myDictionary["key1"]!)
I am new to Swift and have a requirement to store a database of key value pairs. The key value pairs are a name with a corresponding 4 digit number in database that remains in memory after the app is excited. I am thinking to use a dictionary with the name as the key and the 4 digit numbers as the value. These are then stored in the iPad flash memory using the user defaults class.
Below is the code that I’ve currently developed. The code that adds to the database compiles ok but the code that checks the name and number for a match in the database won't compile due to the following message (Value of optional type '[Any]?' not unwrapped; did you mean to use '!' or '?'?) which is because of this line of code (if let databaseCheck = database[name]). Ive obviously tried unwrapping but can't seem to shake the error message.
Anyone got any ideas whats causing the error or any issues with the approach?
public func checkDatabaseMatch( _ name: String, _ number: String) -> Bool
{
var foundInDatabaseFlag: Bool = false
let database = UserDefaults.standard.array(forKey: "Database")
if let databaseCheck = database[name]
{
if (databaseCheck == number)
{
foundInDatabaseFlag = true
}
}
return foundInDatabaseFlag
}
public func saveToDatabase( _ name: String, _ number: String)
{
var newEntry: [String: String] = [:]
newEntry[name] = number
UserDefaults.standard.set(newEntry, forKey: "Database")
}
There is a major mistake. You save a dictionary but retrieve an array.
Apart from that a dictionary retrieved from UserDefaults is [String:Any] by default, you have to conditional downcast the object.
The code checks if there is a dictionary in UserDefaults and if there is the requested key in one expression
public func checkDatabaseMatch( _ name: String, _ number: String) -> Bool
{
guard let database = UserDefaults.standard.dictionary(forKey: "Database") as? [String:String],
let databaseCheck = database[name] else { return false }
return databaseCheck == number
}
Another mistake is that you are always overwriting the entire dictionary in UserDefaults. If you want to save multiple key-value pairs you have to read the dictionary first.
public func saveToDatabase( _ name: String, _ number: String)
{
var newEntry : [String: String]
if let database = UserDefaults.standard.dictionary(forKey: "Database") as? [String:String] {
newEntry = database
} else {
newEntry = [:]
}
newEntry[name] = number
UserDefaults.standard.set(newEntry, forKey: "Database")
}
Side note: The parameter labels are highly recommended in Swift for better readability.
func resetUserDefaults() {
let userDefaults = UserDefaults.standard
let dict = userDefaults.dictionaryRepresentation()
for (key,_) in dict {
if let key = key as? String {
userDefaults.removeObject(forKey: key)
} else {
#if DEBUG
NSLog("\(key)")
#endif
}
}
}
I'm getting this warning. can anyone suggest me how to avoid this warnoing
All keys in UserDefaults must be of type String. So key is declared as a String. So attempting to cast it to a String is pointless. Hence the warning.
All you need is:
func resetUserDefaults() {
let userDefaults = UserDefaults.standard
let dict = userDefaults.dictionaryRepresentation()
for (key,_) in dict {
userDefaults.removeObject(forKey: key)
}
}
There is no need to cast something to the type that it is already known (to the compiler) to have.
Just remove the whole condition and use your key directly.
Since the keys in the UserDefault should of type String, casting the key to string is of no use, and hence you are getting this warning.
func resetUserDefaults() {
let userDefaults = UserDefaults.standard
let dict = userDefaults.dictionaryRepresentation()
for (key, _) in dict {
userDefaults.removeObject(forKey: key)
}
}
It will always show waring because dictionaryRepresentation() return [String : Any].
So when you cast from string to string it will definitely show warning.
for more see this -> https://developer.apple.com/documentation/foundation/userdefaults/1415919-dictionaryrepresentation
I had the same issue with a private function in Swift 5 and I found a solution working for me.
The solution was to change the value to optional.
I added a question mark after the type I was looking for. (as String"?")
You can see an example here :
private func doSomeThing(completion: #escaping (String) -> ()) {
let Something = somethingElse;
if let anoterThing = something as String?{
completion(anoterThing)
}else{
completion("Error at private func doSomeThing")
}
}
You can find more pieces of information here:
https://docs.swift.org/swift-book/LanguageGuide/OptionalChaining.html
Swift: difference as String? vs. as? String
Downcasting in Swift with as and as?
Best Regards
What I'm trying to do is retrieve json data(which is in array format) and check to see if my local array already contains the data, if it does move on to the next value in the JSON data until their is a value that the array doesn't contain then append it to the array. This data in the array must be in order. I'm attempting to do this now but get the error:
Type 'ResultsGenrePosters' does not conform to protocol 'Sequence'
This is what it looks like:
public struct ResultsGenrePosters: Decodable {
public let results : [GenrePosters]?
public init?(json: JSON) {
results = "results" <~~ json
}
}
public struct GenrePosters: Decodable {
public let poster : String
public init? (json: JSON) {
guard let poster: String = "poster_path" <~~ json
else {return nil}
self.poster = poster
}
static func updateGenrePoster(genreID: NSNumber, urlExtension: String, completionHandler:#escaping (_ details: [String]) -> Void){
var posterArray: [String] = []
let nm = NetworkManager.sharedManager
nm.getJSONData(type:"genre/\(genreID)", urlExtension: urlExtension, completion: {
data in
if let jsonDictionary = nm.parseJSONData(data)
{
guard let genrePosters = ResultsGenrePosters(json: jsonDictionary)
else {
print("Error initializing object")
return
}
guard let posterString = genrePosters.results?[0].poster
else {
print("No such item")
return
}
for posterString in genrePosters {
if posterArray.contains(posterString){continue
} else { posterArray.append(posterString) } //This is where the error happens
}
}
completionHandler(posterArray)
})
}
}
Alt + click on genrePosters and what does it tell you? It should say its ResultsGenrePosters because thats what the error is saying. Now look at the type of posterArray; its an array of String, not Array ResultsGenrePosters. I think you mean to write for poster in genrePosters and have confused yourself about the types because you wrote for posterString in genrePosters.
Maybe you want to use map to transform genrePosters into a [String] ?
This transforms your posterArray, if it exists into an array containing just the poster names. If it doesn't exist you get an empty array. This only works if poster is String. If its String? you should use flatMap instead.
let posterNames = genrePosters.results?.map { $0.poster } ?? [String]()