userdefaults

How to add to a number currently stored in user defaults Swift 4

删除回忆录丶 提交于 2019-12-14 04:04:13
问题 I want the user to be able to enter a new number and it will be added to what is currently saved in UserDefaults and then save that combined number in user defaults. Anyone have any idea how to do this? Thanks! Code: let typeHoursInt = Double(typeHours.text!)! let typePayInt = Double(typePay.text!)! totalMade.text = String(typeHoursInt * typePayInt) UserDefaults.standard.set(totalMade.text, forKey: "savedMoney") 回答1: This does what you ask. You should store your totalMade variable in User

save and retrieve array to user default using swift

早过忘川 提交于 2019-12-13 10:08:14
问题 I want to save array from json to user default, I'll get the array when I print it. When I retrieve it in different view controller I only get the last array. So how to retrieve all data user default in different view controller. This is how I save my code in user default first view controller let kode_pelaksanaan = item["kode_pelaksanaan"].string UserDefaults.standard.set(kode_pelaksanaan, forKey: "kode_pelaksanaan") output: kode_pelaksanaan : ELR2018120005 kode_pelaksanaan : ELR2018120004

Saving array of [String: Any] to user default crash occasionally?

拟墨画扇 提交于 2019-12-13 07:11:00
问题 I try to save an array of [String: Any] to user default, and for some situations it works, but others do not. I use the following to save to the default: static func savingQueueToDisk(){ let queueDict = App.delegate?.queue?.map({ (track) -> [String: Any] in return track.dict }) if let queueDict = queueDict{ UserDefaults.standard.set(queueDict, forKey: App.UserDefaultKey.queue) UserDefaults.standard.synchronize() } } Queue is an array of Track, which is defined as follows: class Track { var

Login credentials missing in swift UserDefaults

自闭症网瘾萝莉.ら 提交于 2019-12-13 02:53:35
问题 I have an application which uses a rest api for authentication. The problem I am facing now is that I save user's token in my UserDefaults and username too because those are the two main parameters needed to get user details. so if the application is closed by the user he should still be able to view the view his profile when he opens the application back but instead the profile returns empty details. this is the UserDefaults codes that I have let defaults = UserDefaults.standard var

Saving array with UserDefaults

强颜欢笑 提交于 2019-12-12 03:54:13
问题 Is this the right way to save array of dictionaries in tableView using UserDefaults in Swift 3? var arrNotes = [String:String] func saveNotesArray () { UserDefaults.standard.set(arrNotes, forKey: "notes") UserDefaults.standard.synchronize() } 回答1: Use this function to get your notes: func saveNotesArray () { var notes = UserDefaults.standard.object(forKey: "notes") as? [String:String] } Are you getting object in same way? 回答2: "[String: String]" Means a dictionary where the keys and the

UserDefaults between Objective-C and Swift

烈酒焚心 提交于 2019-12-11 03:05:45
问题 Just trying to be extra careful here... If I have an app that saved a value in UserDefaults like this in Objective-C: NSString *newString = textField.text; [[NSUserDefaults standardUserDefaults] setObject: newString forKey:@"textKey"]; Would this be the proper way of checking whether this value exists when I release an update to the app that is now coded in Swift: if (UserDefaults.standard.object(forKey: "textKey") as? String) != nil { print("There is a string") } else { print("No string

Save/Get UIColor from UserDefaults

a 夏天 提交于 2019-12-10 21:56:34
问题 I need some help to load and read UIColor from UserDefaults. I found a nice extension to do that: extension UserDefaults { func colorForKey(key: String) -> UIColor? { var color: UIColor? if let colorData = data(forKey: key) { color = NSKeyedUnarchiver.unarchiveObject(with: colorData) as? UIColor } return color } func setColor(color: UIColor?, forKey key: String) { var colorData: NSData? if let color = color { colorData = NSKeyedArchiver.archivedData(withRootObject: color) as NSData? } set

Swift 3 / iOS 10 / TodayExtension - UserDefaults always returns nil

别说谁变了你拦得住时间么 提交于 2019-12-10 10:11:32
问题 This is my first Question on this site. I have a problem that I cannot fix. I'm working on an easy note application with today extension. I have had no problem in Swift 2.2 and iOS 9. Problem just appears in Swift 2.3 and Swift 3, on iOS 10. The problem I have is the following : User can write a note (saved in UserDefaults) and open Notification Center and watch his notes in a TodayExtension. I have these methods to save notes in UserDefaults and retrieve it from UserDefaults (I'm using

NSArrayURL , userDefault filePath and URL

杀马特。学长 韩版系。学妹 提交于 2019-12-02 17:17:24
问题 I have one pdf source code and I want to add the Url in Array and use UserDefault let defaults = UserDefaults.standard struct Constants { static let myKeyURL = "myKeyUrl" } I download the Pdf Like This let documentsPath =NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] let fileName = urlString as NSString; let filePath="\(documentsPath)/\(fileName.lastPathComponent)"; After I save the Path like This var arrayUrl = [String]() arrayUrl.append(filePath) self

storing [Int:String] dictionaries to user.defaults crashes with SIGABRT error

不羁的心 提交于 2019-12-01 12:02:06
问题 saving a bunch of data using user.default. everything saves fine except one dictionary [Int:String]. The funny part is this, I can save a dictionary [String:Int] fine, but as soon as its [Int:String], it crashes when I run my save function that has my code in it. the console shows the follow: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object { 99 = Jennifer; } for key playernames' thank you 回答1: The Object that you