UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

App crashes on opening using Userdefaults

Forums > Swift

Hello everyone,

I have this code that i run on app opening

if NewUser.shared.isNewUser() {
            UserDefaults.standard.set(1, forKey: "limitedCampaign")
            if let vc = storyboard?.instantiateViewController(identifier: "onboarding") as? OnboardingViewController {
                vc.modalPresentationStyle = .fullScreen
                present(vc, animated: true)
            }
        } else {
            limitedViewFirstRun()
        }

If the user is new i set in user defaults value 1 to limitedCampaing, if is not new i just run limitedViewFirstRun(). And here is the func:

func limitedViewFirstRun() {
        if UserDefaults.standard.integer(forKey: "limitedCampaign") != 0 && (limited[0]["a"]! as? String) != "" && (limited[0]["d"]! as? String) != ""  {
            if (UserDefaults.standard.integer(forKey: "limitedCampaign") < (limited[0]["c"] as! Int)) {
                if let vc = storyboard?.instantiateViewController(identifier: "limitedDownload") as? LimitedDownloadViewController {
                    vc.modalPresentationStyle = .overCurrentContext
                    vc.imagePreview = limited[0]["a"]! as? String
                    vc.downloadURL = limited[0]["d"]! as? String
                    vc.campaign = limited[0]["c"]! as? Int
                    present(vc, animated: true)
                    UserDefaults.standard.set(limited[0]["c"] as! Int, forKey: "limitedCampaign")
                }
            }
        } else {
            if UserDefaults.standard.integer(forKey: "limitedCampaign") == 0 {
                print("Setting limitedCampaign to UserDefaults!")
                UserDefaults.standard.set(1, forKey: "limitedCampaign")
            } else {
                print("limitedCampaing already set to \(UserDefaults.standard.integer(forKey: "limitedCampaign")) in UserDefaults!")
            }
        }
    }

Here i check if limitedCampaign exists by verifying if != 0. And i check if the values in the array are not "". I set those values into firebase, and when i don't want the modal to be shown i just put an empty string as values. Else i check if the limitedCampaing is set and set it to 1 if not.

In the firebase the start value of limited[0]["c"] as! Int is 2, an then i just +1 on every new campaing, so the code runs only if the id number greater then the local limitedCampaing.

Am i doing something wrong with the Userdefaults??

I know that ther problem is here because it's the only new code of the new release. And the crashes begun here.

Thank you for your help!

2      

Hi,

it is hard to say since you did not provide details about the crash. What it says, which line crashed etc..

My guess is that some data is missing and either force unwrapping or force casting with ! is causing the crash.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.