Removing from array of dictionary - ios

I use UserDefualt to save array of Dictionary:
let pref = NSUserDefaults.standardUserDefaults();
var fav=pref.arrayForKey("favRecipes")!;
fav.append(self.dict);
pref.setObject(fav, forKey: "favRecipes");
before i save another data i check if it already exist:
#IBAction func addToFavorites(sender: UIButton) {
//not exist
if !isFavorite(){
var fav=pref.arrayForKey("favRecipes")!;
fav.append(self.dict);
pref.setObject(fav, forKey: "favRecipes");
print("added");
} //exist
else{
;
}
}
private func isFavorite()->Bool{
var fav=pref.arrayForKey("favRecipes")!;
for r in fav{
if r["id"]! as! Int==id{
return true;
}
}
return false;
}
The purpose of this code is to add something to your favorites list, and if its already there u have the option to remove it.
for some reason i'm not succeeding to do the last part.
this my try so far:
func remove(event:UIAlertAction!){
var fav=pref.arrayForKey("favRecipes")!;
for r in fav{
if r["id"]! as! Int==id{
pref.removeObjectForKey("\(id)");
//pref.synchronize();
//fav=pref.arrayForKey("favRecipes")!;
//print(r["id"] as! Int);
}
}
}
ofcourse i tried to look and search about removing from dictionarys at userDefualt but nothing helps.

You have to change the array fav and write that array back into the userDefaults. There is no object with the key \(id) in the defaults, only an array for key favRecipes which in turn contains an entry for the key id.
func remove(event:UIAlertAction!){
var fav = pref.arrayForKey("favRecipes")!
fav = fav.filter { $0["id"] != id }
pref.setObject(fav, forKey: "favRecipes")
}

Related

Deteting item in an array stored in UserDefaults

I am saving items in UserDefaults and it works fine. I simply append new elements to the array. now deleting the entire saved items is done but now I want to enable the user the ability to delete just one item instead of deleting the entire saved items.
below is how I delete all the entire array
public func deleteSavePropery() {
delete(key: propertyKey)
}
private func delete(key: String) {
storage.removeObject(forKey: key)
}
NOTE, saveProperty is a Codable object
You need to retrieve the array if exists then delete the item finally save back
let storage = UserDefaults.standard
private func deleteItem(key: String,item:Item) {
if let data = storage.data(forKey: key) , var arr = try? JSONDecoder().decode([Item].self, from: data) {
arr.removeAll(where: { $0 == item})
guard let res = try? JSONEncoder().encode(arr) else { return }
storage.set(res, forKey: key)
}
}
struct Item:Codable,Equatable {
let name:String
}
You need to,
Fetch the array from UserDefaults.
Remove the element from the array based on your condition.
Save the array back to UserDefaults.
Example-1: removing the element from array based on the index.
func remove(at index: Int) {
if let data = storage.data(forKey: "YOUR_KEY") {
do {
var arr = try JSONDecoder().decode([Item].self, from: data)
arr.remove(at: index)
let data = try JSONEncoder().encode(arr)
storage.set(data, forKey: "YOUR_KEY")
} catch {
print(error)
}
}
}
Example-2: removing a particular element from array.
func remove(element: Item) {
if let data = storage.data(forKey: "YOUR_KEY") {
do {
var arr = try JSONDecoder().decode([Item].self, from: data)
arr.removeAll { $0 === element }
let data = try JSONEncoder().encode(arr)
storage.set(data, forKey: "YOUR_KEY")
} catch {
print(error)
}
}
}
UserDefaults doesn't provide any API to manage the contents of a particular key. In fact, it doesn't care what kind of content is saved there.
So, what you need to do is: extract the array from UserDefaults, edit it and then save an updated array to UserDefaults.

How to store user data as string, and load that string in viewDidLoad

I am trying to load a value that has been inputted by the user in the viewDidLoad via a String. I am using UserDefaults to save the users value that they input into a UITextField (userValue), I then save this to the String 'search'. I am able to print out the value of search in the GoButton function, and it works fine, but when I load my ViewController as new, the value of 'search' is equal to nil. The aim here is to have the users previous search saved, and loaded into the UITextField (that is used as a search box) upon loading the ViewController.
Code Below:
class ViewController: UIViewController {
#IBOutlet weak var userValue: UITextField!
var search: String!
}
viewDidLoad:
override func viewDidLoad() {
if (search != nil)
{
userValue.text! = String (search)
}
}
Button Function:
#IBAction func GoButton(_ sender: Any) {
let userSearch: String = userValue.text!
let perference = UserDefaults.standard
perference.set(userSearch, forKey: "hello")
perference.value(forKey: "hello")
let value = perference.value(forKey: "hello") as! String
search = value
print (search) // <<this works, it prints out the users search value
}
#VishalSharma has the right idea, but the code should probably look more like…
override func viewDidLoad() {
super.viewDidLoad()
if let search = UserDefaults.standard.string(forKey: "hello") {
userValue.text = search
}
}
or even more simply…
userValue.text = UserDefaults.standard.string(forKey: "hello")
When you load, search is effectively nil.
So either you read userDefaults in viewDidload or you come through a segue: then you can load search in the prepare.
I've always found it convenient and useful to store all UserDefault properties as an extension within the same file along with their getters and setters. It is far easier to maintain, use and read. by using the #function keyword for the key you are referencing the variable's name and not a string that can be accidentally changed somewhere else in code.
UserDefaults.swift
import Foundation
// An Extension to consolidate and manage user defaults.
extension UserDefaults {
/// A value Indicating if the user has finished account setup.
/// - Returns: Bool
var finishedAcountSetup: Bool {
get { return bool(forKey: #function) }
set { set(newValue, forKey: #function) }
}
/// The hello text at the start of the application.
/// - Returns: String?
var helloText: String? {
get { return string(forKey: #function) }
set {set(newValue, forKey: #function) }
}
//etc...
}
When you use these values reference the standard settings:
//Setting
UserDefaults.standard.helloText = "Updated Hello Text"
// Getting
// for non-optional value you can just get:
let didCompleteSetup = UserDefaults.standard.finishedAcountSetup
// Otherwise, safely unwrap the value with `if-let-else` so you can set a default value.
if let text = UserDefaults.standard.helloText {
// Ensure there is text to set, otherwise use the default
label.text = text
} else {
// helloText is nil, set the default
label.text = "Some Default Value"
}
obviously, it provides nil because when view controller load the search is nil try this.
let perference = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
if (perference.value(forKey: "hello") != nil) {
search = perference.value(forKey: "hello") as! String
userValue.text! = String (search)
}
}

Removing Custom Objects from List in UserDefaults

I am trying to remove an element from a list which is stored in NSUserDefaults. The getAll function is implemented below:
func getAllOrders() -> [Order] {
var orders = [Order]()
if let userDefaults = UserDefaults(suiteName: "group.com.johndoe.SoupChef.Shared") {
if let ordersData = userDefaults.data(forKey: "Orders") {
orders = try! JSONDecoder().decode([Order].self, from: ordersData)
}
}
return orders
}
And here is the code for deleting the order.
func delete(order :Order) {
var persistedOrders = getAllOrders()
persistedOrders.removeAll { persistedOrder in
persistedOrder.identifier.uuidString == order.identifier.uuidString
}
}
After deleting the order in the code above when I call getAllOrders I still see all the elements, meaning I don't see the order being deleted.
That's because you don't save your changes. Once you've performed the removal you need to turn persistedOrders back into JSON and then:
userDefaults.set(json, forKey:"Orders")
You need to use jsonEncoder and encode the edited array then store it again the user defaults
func delete(order :Order) {
var persistedOrders = getAllOrders()
persistedOrders.removeAll { persistedOrder in
persistedOrder.identifier.uuidString == order.identifier.uuidString
}
do {
let data = try JSONEncoder().encode(persistedOrders)
userDefaults.set(data, forKey:"Orders")
}
catch {
print(error)
}
}
You have to store correctly your UserDefaults
UserDefaults.standard.set(json, forKey:"Orders")
Now, you can remove them using:
UserDefaults.standard.removeObject(forKey: "Orders")

Check if UserDefault exists - Swift

I'm trying to check if the a user default exists, seen below:
func userAlreadyExist() -> Bool {
var userDefaults : NSUserDefaults = NSUserDefaults.standardUserDefaults()
if userDefaults.objectForKey(kUSERID) {
return true
}
return false
}
However, no mater what it will always return true even when the object doesn't exist yet? Is this the right way for checking existence ?
Astun has a great answer. See below for the Swift 3 version.
func isKeyPresentInUserDefaults(key: String) -> Bool {
return UserDefaults.standard.object(forKey: key) != nil
}
I copy/pasted your code but Xcode 6.1.1 was throwing some errors my way, it ended up looking like this and it works like a charm. Thanks!
func userAlreadyExist(kUsernameKey: String) -> Bool {
return NSUserDefaults.standardUserDefaults().objectForKey(kUsernameKey) != nil
}
Swift 5:
if UserDefaults.standard.object(forKey: "keyName") != nil {
//Key exists
}
Yes this is right way to check the optional have nil or any value objectForKey method returns AnyObject? which is Implicit optional.
So if userDefaults.objectForKey(kUSERID) have any value than it evaluates to true. if userDefaults.objectForKey(kUSERID) has nil value than it evaluates to false.
From swift programming guide
If Statements and Forced Unwrapping
You can use an if statement to find out whether an optional contains a value. If an optional does have a value, it evaluates to
true; if it has no value at all, it evaluates to false.
Now there is a bug in simulators than after setting key in userDefaults they always remain set no matter you delete your app.You need to reset simulator.
Reset your Simulator check this method before setting key in userDefaults or remove key userDefaults.removeObjectForKey(kUSERID) from userDefaults
and it will return NO.On devices it is resolved in iOS8 beta4.
This is essentially the same as suggested in other answers but in a more convenient way (Swift 3+):
extension UserDefaults {
static func contains(_ key: String) -> Bool {
return UserDefaults.standard.object(forKey: key) != nil
}
}
usage: if UserDefaults.contains(kUSERID) { ... }
Simple Code to check whether value stored in UserDefault.
let userdefaults = UserDefaults.standard
if let savedValue = userdefaults.string(forKey: "key"){
print("Here you will get saved value")
} else {
print("No value in Userdefault,Either you can save value here or perform other operation")
userdefaults.set("Here you can save value", forKey: "key")
}
Many of the solutions here are valid. Still, I think they solve the wrong problem.
Usually, code like this is used to check if a value is set so another default value can be used:
if isKeyPresentInUserDefaults(key: "username") {
return UserDefaults.standard.string(forKey: "username")
} else {
return "No username was set"
}
You shouldn't care if a key is set or not. There is a far more elegant approach for having default values in UserDefaults:
UserDefault.standard.register(defaults: ["username": "No username was set"])
If you run this code at app launch, subsequent calls to UserDefaults.standard.string(forKey: "username") will return the default value of "No username was set" if no value was set for the key yet.
for swift 3.2
func userAlreadyExist(kUsernameKey: String) -> Bool {
return UserDefaults.standard.object(forKey: kUsernameKey) != nil
}
public class PreferencesUtils {
private init() {
}
public static func setBoolData(boolValue: Bool, dataName: String) {
UserDefaults.standard.set(boolValue, forKey: dataName)
}
public static func getBoolData(dataName: String)-> Bool{
let defaults = UserDefaults.standard
if(defaults.value(forKey: dataName) != nil) {
return defaults.value(forKey: dataName)! as! Bool
} else {
return false
}
}
public static func saveStringData(data: String, dataName: String){
let preferences = UserDefaults.standard
preferences.set(data, forKey: dataName)
let didSave = preferences.synchronize()
if !didSave {
debugPrint("Not saved yet")
}
}
public static func getSavedStringData(dataName: String)-> String{
let defaults = UserDefaults.standard
if(defaults.value(forKey: dataName) != nil){
return defaults.value(forKey: dataName) as! String
} else {
return ""
}
}
public static func saveIntData(data : Int, dataName: String){
let preferences = UserDefaults.standard
preferences.set(data, forKey: dataName)
let didSave = preferences.synchronize()
if !didSave {
debugPrint("Not saved yet")
}
}
public static func getSavedIntData(dataName: String) -> Int {
let defaults = UserDefaults.standard
if(defaults.value(forKey: dataName) != nil){
return defaults.value(forKey: dataName) as! Int
}else{
return 0
}
}
}
Or you can try this library: Link
func keyExists(key: String) -> Bool {
guard let _ = UserDefaults.standard.object(forKey: key) {
return false;
}
return true;
}
override func viewDidLoad()
{
super.viewDidLoad()
if UserDefaults.standard.string(forKey: "CHARRY") == "CHARRY"{
lb.text = "CHARRY"
im.image = UIImage(named: "CHARRY")
}
}
#IBAction func PressedCar(_ sender: UIButton){
lb.text = "CHARRY"
im.image = UIImage(named: "CHARRY")
UserDefaults.standard.set("CAR", forKey: "CHARRY")
}

iOS Equivalent For Android Shared Preferences

I am porting an Android app to iOS, one thing I used was the Shared Preferences in Android to save each time a level was complete.
That way when the user gets back into the app, they can see they are up to level 3 or whatever.
Is there a similar mechanism in iOS? or do I have to manually write out to an application specific file?
If so, how do I write out to files only visible to my application?
Thanks.
Use NSUserDefaults: - note that this is for small bits of data, such as the current level like you mentioned. Don't abuse this and use it as a large database, because it is loaded into memory every time you open your app, whether you need something from it or not (other parts of your app will also use this).
Objective-C:
Reading:
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSString *currentLevelKey = #"currentlevel";
if ([preferences objectForKey:currentLevelKey] == nil)
{
// Doesn't exist.
}
else
{
// Get current level
const NSInteger currentLevel = [preferences integerForKey:currentLevelKey];
}
Writing:
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSString *currentLevelKey = #"currentlevel";
const NSInteger currentLevel = ...;
[preferences setInteger:currentLevel forKey:currentLevelKey];
// Save to disk
const BOOL didSave = [preferences synchronize];
if (!didSave)
{
// Couldn't save (I've never seen this happen in real world testing)
}
.
Swift:
Reading:
let preferences = NSUserDefaults.standardUserDefaults()
let currentLevelKey = "currentLevel"
if preferences.objectForKey(currentLevelKey) == nil {
// Doesn't exist
} else {
let currentLevel = preferences.integerForKey(currentLevelKey)
}
Writing:
let preferences = NSUserDefaults.standardUserDefaults()
let currentLevelKey = "currentLevel"
let currentLevel = ...
preferences.setInteger(currentLevel, forKey: currentLevelKey)
// Save to disk
let didSave = preferences.synchronize()
if !didSave {
// Couldn't save (I've never seen this happen in real world testing)
}
Here is an update for Swift 3
Reading
let preferences = UserDefaults.standard
let currentLevelKey = "currentLevel"
if preferences.object(forKey: currentLevelKey) == nil {
// Doesn't exist
} else {
let currentLevel = preferences.integer(forKey: currentLevelKey)
}
Writing
let preferences = UserDefaults.standard
let currentLevel = ...
let currentLevelKey = "currentLevel"
preferences.set(currentLevel, forKey: currentLevelKey)
Update
From UserDefaults documentation
synchronize() waits for any pending asynchronous updates to the defaults database and returns; this method is now unnecessary and shouldn't be used.
class Configuration {
static func value<T>(defaultValue: T, forKey key: String) -> T{
let preferences = UserDefaults.standard
return preferences.object(forKey: key) == nil ? defaultValue : preferences.object(forKey: key) as! T
}
static func value(value: Any, forKey key: String){
UserDefaults.standard.set(value, forKey: key)
}
}
Example
//set
Configuration.value(value: "my_value", forKey: "key_1")
//get
let myValue = Configuration.value(defaultValue: "default_value", forKey: "key_1")
As per the previous answer, you already know that UserDefaults is the equivalent to shared preferences in ios. You can create a common write function and for read create function based on data type. And call your required method from anywhere.
ViewController.swift
// write data
writeAnyData(key: "MY_KEY", value: "MyData")
// read string data
readStringData(key: "MY_KEY"), animated: true)
Utils.swift
// read and write user default
let userDefault = UserDefaults.standard
// write
func writeAnyData(key: String, value: Any){
userDefault.set(value, forKey: key)
userDefault.synchronize()
}
// read int values
func readIntData(key: String) -> Int{
if userDefault.object(forKey: key) == nil {
return 0
} else {
return userDefault.integer(forKey: key)
}
}
// read string values
func readStringData(key: String) -> String{
if userDefault.object(forKey: key) == nil {
return ""
} else {
return userDefault.string(forKey: key)!
}
}
// read bool value
func readBoolData(key: String) -> Bool{
if userDefault.object(forKey: key) == nil {
return false
} else {
return userDefault.bool(forKey: key)
}
}

Resources