logo
down
shadow

NSuserDefaults not saving until i close the app swift


NSuserDefaults not saving until i close the app swift

By : Fred
Date : November 14 2020, 04:51 PM
To fix this issue It's hard to post code as a comment so lets try to work here.
Try changing your if statement to this:
code :
if levelfinished() {
    defaults.setInteger(nextlevel, forKey: "levelsCompleted")
    defaults.synchronize()
    levelsCompleted = defaults.intergerForKey("levelsCompleted")
    println("level \(levelsCompleted) unlocked)
}


Share : facebook icon twitter icon
Swift NSUserDefaults not saving Dictionary?

Swift NSUserDefaults not saving Dictionary?


By : Alex Sandro Juchem
Date : March 29 2020, 07:55 AM
Any of those help Update for Swift 2, Xcode 7: As @atxe noticed, NSUserDefaults dictionaries are now mapped as [String, AnyObject]. This is a consequence of the Objective-C "lightweight generics" which allow to declare the Objective-C method as
code :
- (NSDictionary<NSString *,id> *)dictionaryForKey:(NSString *)defaultName
let jo = [
    "a" : "1.0",
    "b" : "2.0"
]

let akey = "aKey"
// Swift 2:
userDefaults.setObject(jo, forKey: akey)
// Swift 3:
userDefaults.set(jo, forKey: akey)
var jo : [NSObject : AnyObject] = [
    "a" : "1.0",
    "b" : "2.0"
] 
userDefaults.setObject(jo, forKey: akey)
var isOk = userDefaults.synchronize()
if let data0 = userDefaults.dictionaryForKey(akey) {
    print(data0)
} else {
    print("not set")
}

// Output: [b: 2.0, a: 1.0]
Saving NSUserDefaults with UISwitch in Swift 2

Saving NSUserDefaults with UISwitch in Swift 2


By : Tucura
Date : March 29 2020, 07:55 AM
Any of those help You forgot to actually load the values from NSUserDefaults.
You set them with setValue and setObject, but you never load them back.
code :
let defaults = NSUserDefaults.standardUserDefaults()
let MotionMode = defaults.objectForKey("MotionMode") as! Bool
if MotionMode {
    // true
} else {
    // false
}
let defaults = NSUserDefaults.standardUserDefaults()
if let MotionMode = defaults.objectForKey("MotionMode") as? Bool {
    if MotionMode {
        // true
    } else {
        // false
    }
}
NSUserDefaults not saving data (Swift)

NSUserDefaults not saving data (Swift)


By : Tanuj
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I am trying to save inputs from various text fields using NSUserDefaults: , Try this,
code :
 @IBAction func continue2save(sender: AnyObject) {

    let stringy1 = mcweightTF.text!
    let stringy2 = mcnumTF.text!

    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setObject("stringy1", forKey: "savemcw")
    defaults.setObject("stringy2", forKey: "savemcn")
}

@IBAction func calculate(sender: AnyObject) {

    let defaults = NSUserDefaults.standardUserDefaults()
    let stringy1 = defaults.stringForKey("savemcw")
    // Optional Chaining for stringy1
    if let stringy = stringy1 {
        calcLabel.text = stringy
    }
}
Swift: Is not saving NSUserDefaults.standardUserDefaults values

Swift: Is not saving NSUserDefaults.standardUserDefaults values


By : Logan
Date : March 29 2020, 07:55 AM
help you fix your problem I'm trying to save key value in the NSUserDefaults but is not been save. Here is my code: , Swift 2.x:
According with Apple sources:
code :
public func objectForKey(defaultName: String) -> AnyObject?
if let value = userDefaults.objectForKey("data")  {
    // do whatever you want with your value
    // P.S. value could be numeric,string,..
}
Swift - Saving highscore using NSUserDefaults

Swift - Saving highscore using NSUserDefaults


By : Nirmala Mary
Date : March 29 2020, 07:55 AM
I hope this helps you . At first, NSUserDefaults is a dictionary (NSDictionary I think). Every app has its own user defaults, so you cannot access the user defaults from any other app.
If the user (the one who plays your game) makes a new highscore, you have to save that highscore like this:
Related Posts Related Posts :
  • Axis change of stackView giving layout error
  • How to use AVAssetResourceLoader?
  • How to write block definition using properties?
  • iOS connect/reconnect to Bluetooth device when using external-accessory and MFi
  • Saving XMPP Multi User chat messages through XMPP MessageArchiving in iOS
  • Deciding iOS version: pros and cons
  • iOS: Resigned app cannot access keychain through security API
  • How can I set To and Subject fields in an E-Mail being sent by UIDocumentInteractionController?
  • Updating uilabel in view controller underneath another
  • Maximum time Intervel for NSTimer
  • Delete Core Data Entry from Table View Controller
  • How to unload UIViewcontroller from memory?
  • Compressing UIImage as Far as Possible?
  • Optimizing workflow to update internally owned cocoapods dependencies?
  • Formatting an NSDate with Abbreviated Month/Day Names
  • I'm creating a quiz game with 4 answers and I want to add score
  • Tap Gesture to Hide Navigation Bar, Tab Bar, and Status Bar
  • FBSessionStateClosedLoginFailed when I am logged in through Settings
  • CGRect positioning according to center point
  • iOS: Update table view datasource with animations without core data
  • iOS : Where to start for AliPay integration?
  • Routing App Coverage File missing but not needed?
  • Drawing on a zoomable view
  • Append NSStrings and NSNumber to NSMutableArrays and make NSMutable dictionary
  • Terminating app due to uncaught exception 'NSRangeException' when calling dequeueReusableCellWithReuseIdentifier:forInde
  • Collision without physics (Cocos2D + Box2D iOS)
  • Creating a percentage based iOS layout
  • Update tableView row from AppDelegate Swift 4
  • Assigning return value of a class method to a variable error
  • how to display circular progress in delphi ios apps?
  • I can't figure how to fix this Expected identifier or '('
  • How to Test APIs in Swift 4?
  • Link error /Build/Products/Debug-iphonesimulator file not found
  • working with a very large JSON object in iOS
  • Turn off part of the iPhone Screen
  • Phonegap 3.0 app with facebook login using xCode
  • How to add interstitial admob ads to my ios project
  • adding tapku calendar to ios app
  • Add an alertcontroller in a global swift file?
  • Take square image with Cordova (Phonegap) Camera API?
  • Amazon S3 policy allowing only upload not overwrite
  • How to change the UILabel content by tapping?
  • Adding checkmark to selected table cell also checks another table cell
  • UICollectionView cellForItemAtIndexPath indexPath row is nil
  • Resize and set quality on JPEG image while retaining EXIF in iOS
  • How to push rootviewcontroller in Ios
  • How to add UIBarbuttonItem at centre on UInavigationbar in iOS
  • Would this IOS device test crash be expected
  • PickerView not showing any values
  • How to set root view controller in storyboard animated with navigation controller programmatically
  • Can anyone suggest me the effective way to deal with s3 upload fail because of timezone difference issue?
  • Getting list of files in documents folder
  • Error while using Realm Object as a singleton. Is it a good idea?
  • inserting into SQLite database
  • More nodes than I should
  • Facebook IOS SDK navigate to another controller after login not working
  • Swift Array - use "Contains" of type AnyObject
  • Sidebar menu with indicate navigationbar on iOS
  • Programmatically set size of UIView from Storyboard
  • Navigation bar blink
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org