1

I am trying to save an array of sounds that I concatenated together in swift, and later on load them. However, all of the data properly saves, but when I try to load it, all of the properties that the data had held no longer exist, but it still counts the data as existing just without any of the properties it had when saved. I am confused why this happens

  struct saveSound{
static func save(newSound: mergedSound = mergedSound()){
    if newSound.soundName == "" || newSound.soundUrl == "" { print("no new data") ; return}
    var sounds = loadSounds.getSounds()
    sounds.append(newSound)
    let archive = NSKeyedArchiver.archivedData(withRootObject: sounds)
    for soun in sounds{

        print(soun.soundName," - saved")
    }
    UserDefaults.standard.set(archive, forKey: "SavedSounds")
}
 }
 struct loadSounds {

static func getSounds() -> [mergedSound]{

    var sounds = [mergedSound]()
    var data = Data()
    if UserDefaults.standard.object(forKey: "SavedSounds") as? Data != nil {
        print("data exists")
        data = UserDefaults.standard.object(forKey: "SavedSounds") as! Data
        sounds =  NSKeyedUnarchiver.unarchiveObject(with: data) as! [mergedSound]
        for souns in sounds{
            print(souns.soundName,sounds.count, " - loaded")
        }

    }
    return sounds
}
}
Chris Hutchison
  • 601
  • 6
  • 20
  • Side-point: user defaults is *not a good place* to save application resources like this. You should be including them in the app bundle if they never change or in the Application Support folder if they do. – Joshua Nozzi May 26 '17 at 22:34
  • Im kind of new to file manipulation through swift, could you explain why its not good practice to save to defaults, just so i can understand – Chris Hutchison May 26 '17 at 22:35
  • Way too much data for a system that's meant to hold small bits of info that represent settings. In an iCloud-sync'd app, such huge amounts of data would slow down syncing dramatically. App launch times can also be affected. – Joshua Nozzi May 26 '17 at 22:42
  • Possible duplicate of [Why NSUserDefaults failed to save NSMutableDictionary in iPhone SDK?](https://stackoverflow.com/questions/471830/why-nsuserdefaults-failed-to-save-nsmutabledictionary-in-iphone-sdk) – Tom Harrington May 30 '17 at 14:57

1 Answers1

1

Are you sure your data is actually saved? Have you looked in the preferences PLIST file to verify? Your log statement does nothing to actually verify this assumption.

Also, is your Sound class NSCoding compliant? Do you implement the protocol to archive / unarchive each property? If not (if you declare compliance but don't handle your class' custom properties), they'll never be written (or read) and your unarchived instances will have default values.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • i don't even have a preference plist lol only info.plist, but how would i make my class nscoding compliant, because i definitely didn't do such – Chris Hutchison May 26 '17 at 22:56
  • You have some reading to do. Preferences are stored in ~/Library/Preferences (where ~ is your home folder). Note ~/Library is a hidden folder. – Joshua Nozzi May 26 '17 at 22:58