2

I try to set an array of strings to UserDefaults but my app gets frozen at that point. I have no problem to set others kinds of data (like String and Bool). I have read this topic and others post elsewhere stating it could be problematic depending on the kind of data we want to store. In my case, I only want to store an array of strings [String]. I have both Objective-C (where I can succesfully save arrays of strings) and Swift classes in my project.

Objective-C class (AppDelegate.m)

{
    NSUserDefaults *appSettings; /// declared in header file
}

appSettings = [NSUserDefaults standardUserDefaults];
NSString *xibHome = @"Home";
[appSettings setObject:xibHome forKey:kNibHome];
NSArray *array = [[NSArray alloc] initWithObjects:@"Test", nil];
[appSettings setObject:array forKey:@"test"]; ---> works fine

[SetupAppSettings appSettingsWithSettings:appSettings];

SetupAppSettings.swift file

class SetupAppSettings: NSObject {

    @objc class func appSettings(settings:UserDefaults) {
    let swiftArray = ["Test"]
    settings.set(swiftArray, forKey: "test") ---> app crashes (no message error, just frozen at launchtime)
    ....
    }
}

It looks like the problem resides in my Swift code. But I cannot figure out why [String] type objects cannot be stored.

Could anyone help me to sort it out?

EDITED

Here is two screenshots as recommended of the process just after pausing the app if it can help:

enter image description here

enter image description here

Trichophyton
  • 625
  • 5
  • 21
  • Freeze and crash aren't the same thing. On a freeze, try pausing the app in the debugger and examining what's happening on the main thread. – Phillip Mills Jan 30 '18 at 14:47
  • I am not sure how I can get an idea of what is going wrong. I edited my question with a screenshot but I don't know how it can help me to understand the problem. – Trichophyton Jan 30 '18 at 15:03
  • Are there any other threads doing processing that might be causing your main one to wait? Is there anything in the hidden stack frames that suggest the main thread is waiting on itself? (If I copy your code to a playground, it seems to do the right thing so I'm guessing it's something else happening around it.) – Phillip Mills Jan 30 '18 at 15:20
  • `UserDefaults.standard.set(swiftArray, forKey: "test")` is how you would set it in Swift straight forwardly. Use this syntax to build on your method. – trndjc Jan 30 '18 at 17:32
  • You can also `print(Array(UserDefaults.standard.dictionaryRepresentation().keys))` print to console to verify that it's been set. – trndjc Jan 30 '18 at 17:32
  • @PhillipMills: I cannot see an other process in background (or I cannot recognize it). But what is strange is that after a couple of builds on a real device (iOS 9.3), it seems to be able to work but it's still impossible to have it working on the simulator (iOS 11). I was wondering if it had to do with something else (like notification registration since a different code is executed between those two different iOS versions). But everything keeps working well on the simulator as soon as I remove the `settings.set(swiftArray, forKey: "test")` command. I cannot understand why. – Trichophyton Jan 30 '18 at 23:34
  • @iabuseservers: what is the difference between `UserDefaults.standard.set(swiftArray, forKey: "test")` and my `settings.set(swiftArray, forKey: "test")` where settings is UserDefaults.standard defined in objective-C code? I will not be able to print the keys since the app freezes during the setting process. Or do you mean something else? Thanks for helping anyway – Trichophyton Jan 30 '18 at 23:37

0 Answers0