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

SOLVED: Why does @AppStorage not work

Forums > Swift

I used @AppStorage before and it works perfect in that app but i can't get it to work in this app

import SwiftUI
import MapKit
import Foundation

struct SupermarketFinder {

    @AppStorage("supermarkets") var supermarkets: [MKMapItem] = []

    func fetchSupermarkets(for currentLocation: CLLocationCoordinate2D, radius: CLLocationDistance) {
            getSupermarkets(for: currentLocation, radius: radius) { supermarkets in
                self.supermarkets = supermarkets
            }
        }

    func getSupermarkets(for currentLocation: CLLocationCoordinate2D, radius: CLLocationDistance, completion: @escaping ([MKMapItem]) -> Void) {
        let request = MKLocalSearch.Request()
        request.naturalLanguageQuery = "supermarket"
        request.region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: currentLocation.latitude, longitude: currentLocation.longitude), latitudinalMeters: radius, longitudinalMeters: radius)

        let search = MKLocalSearch(request: request)
        search.start { response, error in
            guard let response = response else {
                completion([])
                return
            }
            completion(response.mapItems)
        }
    }
}

2      

@AppStorage is use for small data so thinking that putting an Array will not work!

2      

AppStorage and SceneStorage use the UserDefaults system behind the scenes. UserDefaults only allows a few simple types of data: Bool, Int, Float, Double, URL, String, Date, and a few more I can't recall off the top of my head.

More complex types, like arrays and dictionaries, are allowed as long as their elements are one of the allowed simple types. (Although I think Dictionary is only allowed with the additional caveat that the key must be a String or Int. Don't quote me on that, though.)

MKMapItem is not one of the allowed types. If you can convert MKMapItem into a Data object, that is one of the types that can be stored.

2      

if that the case then if the type is String then this should work!

@AppStorage("supermarkets") var supermarkets: [String] = [] // No exact matches in call to initializer

Yes can be used in UserDefaults

UserDefaults.standard.array(forKey: "supermarkets")

PS Dictionary is [String: Any]

2      

Ah, well, you learn something every day. I guess I never tried to stick an array in AppStorage and just assumed since it uses UserDefaults behind the scenes that you can use all the same types.

There's probably a way you could do it by converting your array to JSON or a Property List and then storing the resulting string, or converting it to Data and storing that. But that may be more trouble than it's worth.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.