Search code examples
iosxcodeswiftswift-playgroundxcode8

How do I translate Apple API documentation in to real life code?


I find this aspect of learning new things in Swift and Xcode very puzzling.

I cannot seem to decipher the actual code I need to write from what I read in the Apple API documentation; In my specific case: the API Reference for 'PlaygroundSupport', and the actual code I need, which is:

import PlaygroundSupport
let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 667.0))
PlaygroundPage.current.liveView = containerView

To me this seems like 'hidden knowledge' that I cannot divine from the API. There is no example usage given.

How would I find out from the Apple API that I needed to add UIView (as containerView) in the manner required? How can I learn to read Apple API documentation in the way that is assumed?


Solution

  • Reading API documentation is a lot like reading a math textbook: you need to understand every word you're reading, or else you might miss what you need. In this case, inspecting that documentation, you'd probably check out the PlaygroundPage class, and you'd see the liveView property described as "The active live view in the assistant timeline". If you're not sure what a "live view" is, or what "the assistant timeline" is, then some further searching might be required. If you are aware of those terms, however, you should instantly recognize this as the property you're looking for!

    Keep browsing through the documentation and make sure you understand enough of each method or property to definitely rule it out or look into it further. It can be overwhelming at first, but as you read the Swift iBook, follow tutorials, and continue to read the documentation, you will find yourself understanding more and more of what it says.