Sorry, maybe the title seemed hard to understand.
Here is the situation:
A structure with google API URL properties, which are wrapped together and will be stored as Data in userDefaults.
A function with a parameter, Its job is to retrieve the googleApiLibrary from UserDefault, decode back to structure and get the corresponding URL based on the parameter key provided.
The problem that has bothered me for a long time is, how can I use the String parameter key, to access the structure's property (the appropriate URL to the key).
Here is the situation:
A structure with google API URL properties, which are wrapped together and will be stored as Data in userDefaults.
Code Block struct googleApiLibrary: Codable { let authUrl: URL let tokenUrl: URL let userinfoUrl: URL }
A function with a parameter, Its job is to retrieve the googleApiLibrary from UserDefault, decode back to structure and get the corresponding URL based on the parameter key provided.
Code Block func loadGmailApiUrl(forKey key: String) -> URL { guard let encodedValue = defaults.object(forKey: "gmailApiLibrary") as? Data, let decodedValue = try? JSONDecoder().decode(GmailApiLibrary.self, from: encodedValue) else { return } return decodedValue.<???> }
The problem that has bothered me for a long time is, how can I use the String parameter key, to access the structure's property (the appropriate URL to the key).