We need a better JSON-parsing interface for Swift
| Originator: | goodwinlabs | ||
| Number: | rdar://17341078 | Date Originated: | 17-Jun-2014 07:53 AM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 6.0b1 |
| Classification: | UI/Usability Issue | Reproducible: | Always |
Summary: Swift's new array and dictionary types expect explicit information about the types of things that will be held in them. An array of AnyObject requires typecasting to do basically anything and kind of defeats the purpose of Swift's type-safety. Ideally, we would have an interface like they do in Go (described here: http://blog.golang.org/json-and-go). Their Unmarshall function allows you to specify a type for each object in the incoming JSON. JSON from web API's can rarely be described as Array<Dictionary<String, String>> as sometimes values in a JSON dictionary are numbers or strings or booleans. However, we *could* describe incoming JSON as Array<User> where User is a custom type to describe the information about users from the JSON API. This would allow us to do something like this: let users: Array<User> = NSJSONSerialization.unmarshall(Array<User>, &jsonError) and no code down the line would need to typecast that to get what they wanted. Steps to Reproduce: 1. Download JSON from an API such as this public one: https://api.github.com/gists 2. Figure out a way to express the incoming information in Swift types. 3. Pull hair out. Expected Results: Take advantage of Swift's type safety and make cool apps. Actual Results: Get confused why you're not just writing in ObjC where the super-dynamic nature works fine here. Version: Xcode 6.0b1 / OSX Yosemite b1
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!