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)
}
}
Related
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")
I am working on a coin collection app, and I have recently started integrating core data into my app. I have a core data database which stores custom CoinCategory objects and each CoinCategory object has an array of Coin objects. This way, my app stores everything in terms of categories.
When I integrated the core data, I can add my first category without any errors and delete it without any problems, but when I add in a second coin category, I experience the following error:
2017-06-26 09:55:37.218 CoinCollection[18889:12839563] -[CoinCollection.CoinCategory compare:]: unrecognized selector sent to instance 0x608000236cc0
2017-06-26 09:55:37.219215-0400 CoinCollection[18889:12839563] [error] error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[CoinCollection.CoinCategory compare:]: unrecognized selector sent to instance 0x608000236cc0 with userInfo (null)
CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[CoinCollection.CoinCategory compare:]: unrecognized selector sent to instance 0x608000236cc0 with userInfo (null)
Could anyone please advise me on how to fix this? Thank you very much!
I am attaching my code below for my viewcontroller and the app delegate that is running the core data.
AppDelegate.swift
// We customize the app, system-wide
import UIKit
import CoreData
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "CoinCollection")
container.loadPersistentStores(completionHandler: { (storeDescription,
error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext ()
{
let context = persistentContainer.viewContext
if context.hasChanges
{
do
{
try context.save()
}
catch
{
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate.
//You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
And then for my view controller. Notice that I get a Coin object from a sub-viewcontroller that invokes this method, and that we decide whether the Coin object fits into the existing categories. If not, then we add the new Coin.
// Controls the table view controller showing the general coins (one per each category)
import UIKit
import CoreData
class CoinTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
//this is an array of all the coins in the collection
//each row of this two-dimensional array represents a new category
var coinsByCategory: [CoinCategoryMO] = []
var fetchResultController: NSFetchedResultsController<CoinCategoryMO>!
//other attributes....
////////////////////////////////////////////////////////////////////////
override func viewDidLoad()
{
super.viewDidLoad()
//////////////////////////////////////////////////////////////////////////////////
//we now fetch the data
let fetchRequest : NSFetchRequest<CoinCategoryMO> = CoinCategoryMO.fetchRequest()
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate)
{
let context = appDelegate.persistentContainer.viewContext
let sortDescriptor = NSSortDescriptor(key: "coinCategory", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
fetchResultController.delegate = self
do
{
try fetchResultController.performFetch()
if let fetchedObjects = fetchResultController.fetchedObjects
{
self.coinsByCategory = fetchedObjects
}
}
catch
{
print(error)
}
}
//configure even more....
}
// MARK: - Table view data soure
func deleteCoinCategory(rowPath: IndexPath)
{
if 0 <= rowPath.row && rowPath.row < self.coinsByCategory.count
{
//we have just tested that the rowPath index is valid
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate)
{
let context = appDelegate.persistentContainer.viewContext
let coinCategoryToDelete = self.fetchResultController.object(at: rowPath)
context.delete(coinCategoryToDelete)
appDelegate.saveContext()
}
}
}
func deleteCoin(c: Coin, indexOfSelectedCategory: IndexPath) -> Bool
{
//we have a coin that we want to delete from this viewcontroller
//and the data contained in it.
//
//the parameter indexOfSelectedCategory refers to the IndexPath of the
//row in the TableView contained in THIS viewcontroller whose category
//of coins we are modifying in this method
//
//Return value: a boolean that indicates whether a single coin has
//been deleted - meaning that the user should return to the parentviewcontroller
if 0 < indexOfSelectedCategory.row && indexOfSelectedCategory.row < self.coinsByCategory.count && self.coinsByCategory[indexOfSelectedCategory.row].coinCategory?.hasCoin(c: c) == true
{
//the index is valid as it refers to a category in the coinsByCategory array
//and the examined category has the coin in question
if self.coinsByCategory[indexOfSelectedCategory.row].coinCategory?.count == 1
{
//the coin "c" that we are going to delete is the only coin in the entire category
self.deleteCoinCategory(rowPath: indexOfSelectedCategory)
return true
}
else
{
//more than one coin in the category
self.coinsByCategory[indexOfSelectedCategory.row].coinCategory?.removeCoin(c: c)
//we save the changes in the database...
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate)
{
appDelegate.saveContext()
}
return false
}
}
return false
}
func addCoin(coinToAdd: Coin)
{
//we check over each category to see if the coin can be added
for category in self.coinsByCategory
{
if category.coinCategory?.coinFitsCategory(aCoin: coinToAdd) == true
{
//we can add the coin to the category
category.coinCategory?.addCoin(newCoin: coinToAdd)
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate)
{
//we save changes to the database
appDelegate.saveContext()
//we are DONE with this function
return
}
}
}
//since the coinToAdd does not fall in the existing categories, we create a new one
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate)
{
let newCategory = CoinCategoryMO(context: appDelegate.persistentContainer.viewContext)
newCategory.coinCategory = CoinCategory(coins: [coinToAdd], categoryType: CoinCategory.CategoryTypes.COUNTRY_VALUE_AND_CURRENCY)
print("Saving data to context ...")
appDelegate.saveContext()
}
}
//delegate methods control the core data database
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>)
{
tableView.beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?)
{
switch type
{
case .insert :
if let newIndexPath = newIndexPath
{
tableView.insertRows(at: [newIndexPath], with: .fade)
}
case .delete:
if let indexPath = indexPath
{
tableView.deleteRows(at: [indexPath], with: .fade)
}
case .update:
if let indexPath = indexPath
{
tableView.reloadRows(at: [indexPath], with: .fade)
}
default:
tableView.reloadData()
}
if let fetchedObjects = controller.fetchedObjects
{
self.coinsByCategory = fetchedObjects as! [CoinCategoryMO]
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>)
{
tableView.endUpdates()
}
I am looking forward to hearing your thoughts and thanks in advance for the help!
EDIT:
My code for the CoinCategory class:
// This class is supposed to represent a category of coin objects in CoreData
import Foundation
import CoreData
public class CoinCategory: NSObject, NSCoding
{
//These are the various types of categories that a user can create out of their coin collection
enum CategoryTypes : NSString
{
case COUNTRY_VALUE_AND_CURRENCY = //...
case COUNTRY = //...
case YEAR = //...
case CURRENCY = //...
case NO_CATEGORY = //...
}
//this struct is used to encode data in Key-Value pairs per the NSCoding protocol
struct Keys
{
static let Current_Category_Type = "current_category_type"
static let Coins_In_Category = "coins_in_category"
}
//this is the collection of the coins in the category
var coinsInCategory: [Coin] = [] //initially we have no coins in the collection
var currentCategoryType : CategoryTypes.RawValue = ""
public var count : NSNumber
{
get
{
//..number of coins in category
}
}
public var array : [Coin]
{
get
{
//read-only copy of the Coins array..
}
}
public required init?(coder aDecoder: NSCoder)
{
//we decode this object's information
if let categoryTypeObject = aDecoder.decodeObject(forKey: Keys.Current_Category_Type) as? CategoryTypes.RawValue
{
self.currentCategoryType = categoryTypeObject
}
if let coinsInCategoryArrayObject = aDecoder.decodeObject(forKey: Keys.Coins_In_Category) as? [Coin]
{
self.coinsInCategory = coinsInCategoryArrayObject
}
}
public func encode(with aCoder: NSCoder)
{
//we encode this object's information
aCoder.encode(currentCategoryType, forKey: Keys.Current_Category_Type)
aCoder.encode(self.coinsInCategory, forKey: Keys.Coins_In_Category)
}
override init()
{
super.init()
self.currentCategoryType = CategoryTypes.COUNTRY_VALUE_AND_CURRENCY.rawValue
self.coinsInCategory = []
}
convenience init(coins: [Coin], categoryType: CategoryTypes.RawValue)
{
self.init()
self.coinsInCategory = coins
if isACategoryType(categoryType: categoryType) == true
{
self.currentCategoryType = categoryType
}
else
{
self.currentCategoryType = CategoryTypes.NO_CATEGORY.rawValue
}
}
func isACategoryType(categoryType: NSString) -> Bool
{
switch categoryType
{
case CategoryTypes.COUNTRY_VALUE_AND_CURRENCY.rawValue:
return true
case CategoryTypes.COUNTRY.rawValue:
return true
case CategoryTypes.YEAR.rawValue:
return true
case CategoryTypes.CURRENCY.rawValue:
return true
default:
return false
}
}
func addCoin(newCoin: Coin)
{
//we are adding a new Coin object to this category
//if it falls into the category's type
if self.coinFitsCategory(aCoin: newCoin) == true
{
self.coinsInCategory.append(newCoin)
}
}
func coinFitsCategory(aCoin: Coin) -> Bool
{
//this function tests if aCoin fits into the category type
//but that all varies depending on which category the coin is
if self.coinsInCategory.count == 0
{
//this category is currently empty, so any addition goes!
return true
}
//otherwise, this category is not empty... so we are now going to
//examine the situation more critically
let testCoin = self.coinsInCategory[0]
switch self.currentCategoryType
{
case CategoryTypes.COUNTRY_VALUE_AND_CURRENCY.rawValue:
return (testCoin.getCountry().lowercased == aCoin.getCountry().lowercased) && (testCoin.getValue() == aCoin.getValue()) && (testCoin.getDenomination().lowercased == aCoin.getDenomination().lowercased)
case CategoryTypes.COUNTRY.rawValue:
return testCoin.getCountry().lowercased == aCoin.getCountry().lowercased
case CategoryTypes.CURRENCY.rawValue:
return testCoin.getDenomination().lowercased == aCoin.getDenomination().lowercased
case CategoryTypes.YEAR.rawValue:
return testCoin.getYear() == aCoin.getYear()
default:
return false
}
}
func getIndexOfCoinInCollection(coin: Coin) -> Int
{
//we are going to return -1 if the coin does not exist in the collection
//and are going to return the index otherwise if yes
for i in 0..<self.coinsInCategory.count
{
if coinsInCategory[i] == coin
{
return i
}
}
//have not found anything
return -1
}
func removeCoin(at: Int)
{
//we remove the coin at the index if it is in a valid range of the coinInCategory array
if isValidArrayIndex(index: at)
{
self.coinsInCategory.remove(at: at)
}
}
func getCoin(at: Int) -> Coin?
{
//we return nil if there is an issue in accessing the coin
if isValidArrayIndex(index: at)
{
return self.coinsInCategory[at]
}
else
{
return nil
}
}
func assignCoin(at: Int,c: Coin)
{
if isValidArrayIndex(index: at)
{
self.coinsInCategory[at].assign(right: c)
}
}
func deleteAllCoins()
{
//we delete all the coin in this category
self.coinsInCategory.removeAll()
}
func removeCoin(c: Coin)
{
//we delete a coin from the category
for i in 0..<self.coinsInCategory.count
{
if self.coinsInCategory[i] == c
{
//the coin at index "i" is equal to the coin "c" that we want to delete from the category
self.coinsInCategory.remove(at: i)
return
}
}
}
func hasCoin(c: Coin) -> Bool
{
return getIndexOfCoinInCollection(coin: c) != -1
}
func swapValuesWithAnotherCategory(other: CoinCategory)
{
swap(&self.currentCategoryType, &other.currentCategoryType)
swap(&self.coinsInCategory,&other.coinsInCategory)
}
func swapCoins(indexOne: Int, indexTwo: Int)
{
if isValidArrayIndex(index: indexOne) && isValidArrayIndex(index: indexTwo)
{
swap(&self.coinsInCategory[indexOne],&self.coinsInCategory[indexTwo])
}
}
private func isValidArrayIndex(index: Int) -> Bool
{
return (0 <= index && index < coinsInCategory.count)
}
}
And then the coin category class:
// This provides the class definition of Class Coin for the project
import UIKit
import CoreData
enum TimePeriods: String
{
//this enumeration represents the different time periods that a
//coin was minted in, for the sake of this programn
case BCE = "BCE"
case CE = "CE"
}
public class Coin : NSObject, NSCoding
{
//this struct represents all the keys used in encoding and decoding this object
struct Keys
{
static let Country = "country"
static let Mint = "mint"
static let Year = "year"
static let Currency = "currency"
static let Value = "value"
static let Grade = "grade"
static let Comments = "comments"
static let NumInstances = "numberInstances"
static let Description = "description"
static let Obverse = "obverse"
static let Reverse = "reverse"
}
//this represents a coin in the table view
static let GRADING_LOWER_LIMIT: NSNumber = 1
static let GRADING_UPPER_LIMIT: NSNumber = 70
//this represents the default strings returned if a field does not have the needed information
static let DEFAULT_DESCRIPTIVE_NAME: NSString = "(Description?)"
static let DEFAULT_COMMENTS: NSString = "(Comments?)"
static let DEFAULT_DENOMINATION: NSString = "(Denomination?)"
static let DEFAULT_MINT: NSString = "(Mint?)"
static let DEFAULT_COUNTRY: NSString = "(Country?)"
static let DEFAULT_YEAR: NSString = "(Year?)"
static let DEFAULT_GRADE: NSString = "(Grade?)"
static let DEFAULT_VALUE_AND_DENOMINATION: NSString = "(Value?) (Currency?)"
static let OBVERSE_IMAGE_STRING : NSString = "Obverse"
static let REVERSE_IMAGE_STRING : NSString = "Reverse"
static private let BULLET = "➣ " //represents the kind of bullet to be used to build a complete summary of the coin
//declare members with setters and getters
private var country: NSString = "" //what country/empire/etc. used in?
private var mint: NSString = "" //where minted? EX: US Mint, St. Petersburg
private var year: NSNumber? = nil //what year minted? per gregorian calendar
//the year can be negative to represent the BCE period
//positive to represent the CE periods
private var typeCurrency: NSString = "" //what is the unit of value? EX: Cents, dollars, centavos, etc
private var theValue: NSNumber = 0 //how many? EX: how many dollars, cents, centavos, etc.?
//additional information about the coin
private var grade: NSNumber? //on the american grading scale for coins. 1-70
private var comments: NSString = "" //extra comments stored by the user for himself
private var numberOfInstances: NSNumber = 0 //number of coins exactly like this. EX: 1,2,3,4...etc? For each instance, it must be >= 1.
//This describes the type of the coin
//EX: Walking Liberty Quarter, Barber Quarter, Standing Liberty Quarter... etc
private var descriptiveName: NSString = ""
private var obverseImage: UIImage? = nil
private var reverseImage: UIImage? = nil
public var valueAndDenomination: NSString
{
get
{
//need to check four cases
//case 1: we have the right values for value and denomination
//case 2: we do not have a value but do have denomination
//case 3: we have a value but do not have denomination
//case 4: we do not have both
//
//the reason why we consider 0 to be an empty value is because a coin that was worth
//nothing would not have been minted in the first place!!!
if (self.theValue != 0 && self.typeCurrency != "")
{
//have value and denomination
return "\(self.theValue) \(self.typeCurrency)" as NSString //like "20 Cents"
}
else if (self.theValue == 0 && self.typeCurrency != "" )
{
//do not have value, but have denomination
return "(Value?) \(self.typeCurrency)" as NSString
}
else if (self.theValue != 0 && self.typeCurrency == "")
{
//we have value, but do not have denomination
return "\(self.theValue) (Currency?)" as NSString
}
else
{
//we do not have both
return Coin.DEFAULT_VALUE_AND_DENOMINATION as NSString
}
}
}
public required init?(coder aDecoder: NSCoder)
{
//we decode this object's information
if let countryObject = aDecoder.decodeObject(forKey: Keys.Country) as? NSString
{
self.country = countryObject
}
if let mintObject = aDecoder.decodeObject(forKey: Keys.Country) as? NSString
{
self.mint = mintObject
}
if let yearObject = aDecoder.decodeObject(forKey: Keys.Year) as? NSNumber
{
self.year = yearObject
}
if let currencyObject = aDecoder.decodeObject(forKey: Keys.Currency) as? NSString
{
self.typeCurrency = currencyObject
}
if let valueObject = aDecoder.decodeObject(forKey: Keys.Value) as? NSNumber
{
self.theValue = valueObject
}
if let gradeObject = aDecoder.decodeObject(forKey: Keys.Grade) as? NSNumber
{
self.grade = gradeObject
}
if let commentObject = aDecoder.decodeObject(forKey: Keys.Comments) as? NSString
{
self.comments = commentObject
}
if let numInstancesObject = aDecoder.decodeObject(forKey: Keys.NumInstances) as? NSNumber
{
self.numberOfInstances = numInstancesObject
}
if let descriptiveNameObject = aDecoder.decodeObject(forKey: Keys.Description) as? NSString
{
self.descriptiveName = descriptiveNameObject
}
if let obverseImageObject = aDecoder.decodeObject(forKey: Keys.Obverse) as? UIImage
{
self.obverseImage = obverseImageObject
}
if let reverseImageObject = aDecoder.decodeObject(forKey: Keys.Reverse) as? UIImage
{
self.reverseImage = reverseImageObject
}
}
override init()
{
//default initializer
super.init()
self.country = ""
self.mint = ""
self.year = nil
self.typeCurrency = ""
self.theValue = 0
self.comments = ""
self.numberOfInstances = 1
self.descriptiveName = ""
self.obverseImage = nil
self.reverseImage = nil
}
init(country: NSString,year: Int?,typeCurrency: NSString, theValue: NSNumber,mint: NSString,grade: Int?,numInstances: NSNumber = 1,description: NSString, comments: NSString)
{
super.init()
self.country = country
self.mint = mint
self.year = year! as NSNumber
self.typeCurrency = typeCurrency
self.theValue = theValue
self.comments = comments
self.numberOfInstances = numInstances
self.descriptiveName = description
self.obverseImage = nil
self.reverseImage = nil
}
init(country: NSString,year: NSNumber?,typeCurrency: NSString, theValue: NSNumber,mint: NSString,grade: NSNumber?,numInstances: NSNumber = 1,description: NSString, comments: NSString,obverseImage: UIImage, reverseImage: UIImage)
{
super.init()
self.country = country
self.mint = mint
self.year = year
self.typeCurrency = typeCurrency
self.theValue = theValue
self.comments = comments
self.numberOfInstances = numInstances
self.descriptiveName = description
}
public func encode(with aCoder: NSCoder)
{
//we encode the coin's information
aCoder.encode(self.country, forKey: Keys.Country)
aCoder.encode(self.mint, forKey: Keys.Mint)
aCoder.encode(self.year, forKey: Keys.Year)
aCoder.encode(self.typeCurrency, forKey: Keys.Currency)
aCoder.encode(self.theValue, forKey: Keys.Value)
aCoder.encode(self.grade, forKey: Keys.Grade)
aCoder.encode(self.comments, forKey: Keys.Comments)
aCoder.encode(self.numberOfInstances, forKey: Keys.NumInstances)
aCoder.encode(self.descriptiveName, forKey: Keys.Description)
aCoder.encode(self.obverseImage, forKey: Keys.Obverse)
aCoder.encode(self.reverseImage, forKey: Keys.Reverse)
}
//setter and getter functions for class members...
func getCompleteSummary() -> NSString
{
//returns a bulleted list that represents the coin
//and it describes every single detail...
}
func getIncompleteSummary() -> String
{
//returns a bulleted list string that represents the coin
//and it describes every single detail...
}
/////////////////////////////////////////////////////////////////////////////////
func ofSameType(rhs: Coin) -> Bool
{
return (self.getCountry().lowercased == rhs.getCountry().lowercased) && (self.getValue() == rhs.getValue()) && (self.getDenomination().lowercased == rhs.getDenomination().lowercased)
}
public static func==(lhs: Coin, rhs: Coin) -> Bool
{
//we compare two coin objects for equality in ALL Categories
return lhs.country.lowercased == rhs.country.lowercased &&
lhs.theValue == rhs.theValue &&
lhs.typeCurrency.lowercased == rhs.typeCurrency.lowercased &&
lhs.mint.lowercased == rhs.mint.lowercased &&
lhs.year == rhs.year &&
lhs.grade == rhs.grade &&
lhs.comments == rhs.comments &&
lhs.numberOfInstances == rhs.numberOfInstances &&
lhs.descriptiveName == rhs.descriptiveName &&
lhs.obverseImage == rhs.obverseImage &&
lhs.reverseImage == rhs.reverseImage
}
func assign(right: Coin)
{
//we implement this instead of overloading the assignment "=" operator
//as it is not possible to overload the "=" operator
//we assign the right-hand-coin's field values
//to the left-hand coin's side
self.country = right.country
self.theValue = right.theValue
self.typeCurrency = right.typeCurrency
self.mint = right.mint
self.year = right.year
self.grade = right.grade
self.comments = right.comments
self.numberOfInstances = right.numberOfInstances
self.descriptiveName = right.descriptiveName
self.obverseImage = right.obverseImage
self.reverseImage = right.reverseImage
}
}
It looks like the problem is that your CoinCategory class does not implement the compare: method:
[CoinCollection.CoinCategory compare:]: unrecognized selector sent to instance 0x608000236cc0
My guess is that the FRC is trying to compare the coinCategory attribute of the second CoinCategoryMO to the coinCategory attribute of the first CoinCategoryMO in order to get them in the correct order (since your FRC is sorted using the coinCategory attribute).
So the direct answer is that you need to implement a compare: method for your CoinCategory class. But I think this is just an indicator that you need to change your approach. Rather than having an Entity with attributes that are custom objects (or collections thereof), you should probably aim to have multiple Entities (eg. a CoinMO entity, a CoinCategoryMO entity, a CoinCategoryTypeMO entity, etc) with to-many relationships between them. If you show the code for your custom objects (Coin and CoinCategory) it will be easier to advise how best to model them in CoreData.
I'm building a helper to enable typed access to NSUserDefaults properties. Something like this:
struct UserDefaults {
private static var standardUserDefaults: NSUserDefaults = {
return NSUserDefaults.standardUserDefaults()
}()
private static let propKey = "PROP"
static var prop: Bool {
get {
return standardUserDefaults.boolForKey(propKey)
}
set {
standardUserDefaults.setBool(newValue, forKey: propKey)
standardUserDefaults.synchronize()
}
}
}
This way I can have a nice syntax for reading and writing to NSUserDefaults:
UserDefaults.prop // read
UserDefaults.prop = false // write
The problem is that there's a lot of boilerplate code for this, I need 10 lines for each aditional property.
Is there any way of reducing the amount of lines needed for each new property? Reusing getter and setter? Any kind of run time generator?
You can try wrapping the actual value in a class that handles all the dirty work for you:
class WrappedUserDefault<T> {
let key : String
let defaultValue : T
var value : T {
get {
if let value = UserDefaults.standardUserDefaults.objectForKey(key) as? T {
return value
} else {
return defaultValue
}
}
set {
if let value = newValue as? AnyObject {
UserDefaults.standardUserDefaults.setValue(value, forKey: key)
} else {
UserDefaults.standardUserDefaults.removeObjectForKey(key)
}
UserDefaults.standardUserDefaults.synchronize()
}
}
init(key:String, defaultValue:T) {
self.key = key
self.defaultValue = defaultValue
}
}
struct UserDefaults {
static let standardUserDefaults = NSUserDefaults.standardUserDefaults()
static let ready = WrappedUserDefault<Bool>(key:"ready", defaultValue: true)
static let count = WrappedUserDefault<Int>(key: "count", defaultValue: 0)
}
Then with just a little bit more code you wind up with:
UserDefaults.count.value++
UserDefaults.ready.value = true
UserDefaults.ready.value
If the verbosity of ready.value bothers you, you can somewhat hide that, although then you're back to you're back to having a fair amount of copy/paste code:
struct UserDefaults {
static let standardUserDefaults = NSUserDefaults.standardUserDefaults()
private static let readyWrapper = WrappedUserDefault<Bool>(key:"ready", defaultValue: true)
static var ready : Bool {
get { return readyWrapper.value }
set { readyWrapper.value = newValue }
}
}
At least in this case though, the copy/paste code is fairly trivial, so unlikely to need to be altered in the future.
I like David's answer much better, but here's another option. Drops your 10 lines per variable down to 5 (mainly because of new line removal...)
struct UserDefaults {
private static var standardUserDefaults: NSUserDefaults = {
return NSUserDefaults.standardUserDefaults()
}()
//Repeate these 5 lines for all new variables,
//changing the as? to the proper variable type
//Adding in a default value for the return in
//case the as? cast fails for any reason
private static let propKey = "PROP"
static var prop: Bool {
get { return (getVar(propKey) as? Bool) ?? false }
set { setVar(newValue, key:propKey) }
}
//The generic set/get
private static func getVar(key : String) -> AnyObject?
{
return standardUserDefaults.objectForKey(key)
}
private static func setVar(newValue : AnyObject, key : String)
{
if(newValue is Bool)
{
standardUserDefaults.setBool((newValue as? Bool)!, forKey: key)
}
//... More cases here
else if(newValue == nil)
{
standardUserDefaults.removeObjectForKey(key)
}
else
{
standardUserDefaults.setObject(newValue, forKey: key)
}
standardUserDefaults.synchronize()
}
}
Hey guys in my app the user clicks a button called the showfunfact() that moves them through an array of strings. When the user removes the app from multitasking or turns off the phone I want the users place to be saved and then when the reload the can pick up where they left off
var TechfactIndex = 0
let TechnologyfactBook = TechFactBook()
override func viewDidLoad() {
super.viewDidLoad()
let defaults = NSUserDefaults.standardUserDefaults()
TechfactIndex = NSUserDefaults.standardUserDefaults().integerForKey("ByteLocation")
defaults.setObject(TechfactIndex, forKey: "ByteLocation")
}
#IBAction func showFunFact() {
if ( UIApplication.sharedApplication().applicationIconBadgeNumber != 0){
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}
if (TechfactIndex >= TechnologyfactBook.TechfactsArray.count) {
self.TechfactIndex = 0
}
TechByteLabel.text = TechnologyfactBook.TechfactsArray[TechfactIndex]
TechfactIndex++
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
NSUserDefaults.standardUserDefaults().setInteger(TechfactIndex, forKey: "ByteLocation")
NSUserDefaults.standardUserDefaults().synchronize()
}
The thing is that viewDidDisappear: is unrelated to your spec. It is not called "When the user removes the app from multitasking or turns off the phone". It is not called when the phone rings. It is not called when the user hits the home button. It's irrelevant. You've put your code in the wrong place.
What you want to do is register to hear when the app is deactivated. That is the moment to write information into the user defaults.
It's not that expensive of an operation to just write to the User Defaults every time the next fun fact is viewed. So for the sake of simplicity, you could just write the new index to the NSUserDefaults every time they view a new fun fact.
#IBAction func showFunFact() {
if (UIApplication.sharedApplication().applicationIconBadgeNumber != 0){
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}
TechfactIndex = NSUserDefaults.standardUserDefaults().integerForKey("ByteLocation")
if (TechfactIndex >= TechnologyfactBook.TechfactsArray.count) {
self.TechfactIndex = 0
}
TechByteLabel.text = TechnologyfactBook.TechfactsArray[TechfactIndex]
TechfactIndex++
NSUserDefaults.standardUserDefaults().setInteger(TechfactIndex, forKey: "ByteLocation")
NSUserDefaults.standardUserDefaults().synchronize()
}
However, the user defaults is usually reserved for preferences... so you may be better off implementing a plist (or similar simple saving option) here to store the current index.
EDIT: Here is a simple example on how you could use a plist to achieve this
First, create a Property List file in your directory, call it Data.plist. Make the root object a dictionary and add an NSNumber object with the key FunFactIndex. This will be a template for your plist on the first time a save occurs.
func showFunFact() {
if (UIApplication.sharedApplication().applicationIconBadgeNumber != 0){
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}
// Load the next index
var factIndex = getCurrentFunFactIndex() as Int
if factIndex < 0 {
println("error")
return
}
if (factIndex >= TechnologyfactBook.TechfactsArray.count) {
factIndex = 0
}
TechByteLabel.text = TechnologyfactBook.TechfactsArray[factIndex]
factIndex++
// Save the index
let saveSuccess = saveFunFactIndex(factIndex);
let successString = (saveSuccess) ? "success" : "failure"
println("Save was a \(successString)")
}
func getCurrentFunFactIndex() -> Int {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDirectory = paths.objectAtIndex(0) as? NSString
let path = documentsDirectory!.stringByAppendingPathComponent("Data.plist")
let fileManager = NSFileManager.defaultManager()
// Check if file exists, copy it over from the bundle if it doesn't
if !fileManager.fileExistsAtPath(path) {
let bundle = NSBundle.mainBundle().pathForResource("Data", ofType: "plist")
fileManager.copyItemAtPath(bundle!, toPath: path, error:nil)
}
if let dataDict = NSDictionary(contentsOfFile: path) {
if let indexNum: AnyObject = dataDict.objectForKey("FunFactIndex") {
return indexNum.integerValue
}
}
return -1 // Something went wrong
}
func saveFunFactIndex(index: Int) -> Bool {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDirectory = paths.objectAtIndex(0) as! NSString
let path = documentsDirectory.stringByAppendingPathComponent("Data.plist")
let fileManager = NSFileManager.defaultManager()
// Check if file exists, copy it over from the bundle if it doesn't
if !fileManager.fileExistsAtPath(path) {
let bundle = NSBundle.mainBundle().pathForResource("Data", ofType: "plist")
fileManager.copyItemAtPath(bundle!, toPath: path, error:nil)
}
if let dataDict = NSMutableDictionary(contentsOfFile: path) {
let indexNum: NSNumber = index
dataDict.setObject(indexNum, forKey: "FunFactIndex")
return dataDict.writeToFile(path, atomically: true)
}
return false
}
There are two methods I added for you; getCurrentFunFactIndex and saveFunFactIndex:. Both will first check in the Documents directory of the sandbox forData.plist. If that file does not exist, it will copy over the template plist that we created in the bundle. All future uses of these methods will use theData.plist` file that is in the Documents directory. This will allow the value to persist on subsequent app launches (removing the app from the background or turning off the device).
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")
}