I have recently integrated GoogleSignIn in my app which upon successful authorisation returns GIDGoogleUser which is their own interface, written in Objc (and I have never worked in Objc) and I wanted to convert it to dictionary (without manually writing any parsers) so that all the profile data that is coming from Google can be sent to the backend as-is. Is there a way to do so?
Asked
Active
Viewed 38 times
0
-
Not quite sure what you are asking. Is it that you don't know how to import the API? Or is it that you don't now how to access Objective-C properties from Swift? Or do you really just want to take all the properties and store them is kv pairs in a dictionary? – JeremyP Jan 05 '23 at 14:05
-
If it's really just a case of "how do I access these Objective-C classes from Swift, all you have to do is add a package dependency for `https://github.com/google/GoogleSignIn-iOS.git` to your project and then you can import it like a Swift module. – JeremyP Jan 05 '23 at 14:09
-
@JeremyP I just want all the properties and store them as kv pairs in a dictionary – user121095 Jan 06 '23 at 06:27
1 Answers
-1
You should use the method described in this thread:
How can I iterate over a class keys in objective-C?
When iterating over the properties you can store them to an NSDictionary which can be accessed in Swift in the same manner as a native Swift dictionary.
Since some of the properties are custom objects, you will probably need to use recursion.
In my opinion it is easier to fetch only the properties that you want instead of iterating over everything. It would require much less code and it is much simpler.
Arik Segal
- 2,963
- 2
- 17
- 29
-
The answer to which you refer isn't really satisfactory because it is a link only answer. Furthermore, it's probably the "wrong" answer. Given that the API docs linked include Swift function signatures, it can probably be used directly with Swift without converting into a dictionary (yes, I know this is what was asked for in the question). – JeremyP Jan 05 '23 at 13:46