-1

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?

  • why would anyone vote down my question?! – Peter J. Nicholls Jul 07 '16 at 01:06
  • 1
    I didn't downvote, but: You didn't explain what problem you were actually trying to solve. All I get from your question is that you're unhappy with the documentation. Without knowing what end goal you were trying to reach, I can't suggest what else you should read, or what other avenues for help you should investigate. – Kurt Revis Jul 07 '16 at 03:38

2 Answers2

4

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.

andyvn22
  • 14,696
  • 1
  • 52
  • 74
  • I was hoping that as I give an example of where what this particular API says and what it needs but cannot figure out where you discover what the missing link is. Hence I give the link to this particular API (which is quite short) in the hope someone might say "this line / phrase is the bit which means that..." because I've read thought all of the Swift book, I've done everything in it; I've followed through most Apple tutorials, but every time I need to look up an API to extend my knowledge... BANG I hit this kind of impasse time and time again. – Peter J. Nicholls Jul 08 '16 at 11:29
  • "`liveView` - The active live view in the assistant timeline." This line is the part that means that whatever view you set the `liveView` property to will appear in the assistant timeline. Since what you wanted (I gather) is to show a view in the assistant timeline, it is clear from this line of documentation that what you need to do is set the `liveView` property to whatever view you wish to display. – andyvn22 Jul 08 '16 at 12:56
  • *click* aha! Thanks. :) – Peter J. Nicholls Jul 08 '16 at 13:47
1

As @andyvn22 mentions it just takes time to learn how to work with the documentation, understand the way it's trying to communicate to you and experiment. I'll show you my thought process:

  1. The page you linked has a import PlaygroundSupport and I could see there's a class PlaygroundPage - assuming you want to play with rendering UI's in the Playground, I dig into that.
  2. The PlaygroundPage seems to have a property called liveView which seems to let me interact with something in real time, so click through there... https://developer.apple.com/reference/playgroundsupport/playgroundpage
  3. I can see that liveView is a get/set so I know I need to pass it something, perhaps the view object I want to render in the playground? Let's click through to the PlaygroundLiveViewable protocol that this view must conform to... https://developer.apple.com/reference/playgroundsupport/playgroundpage/1964506-liveview
  4. Ah, so a UIView or UIViewController conform to the PlaygroundLiveViewable protocol - if I try and pass it one then it works as you seem to have ascertained in your question https://developer.apple.com/reference/playgroundsupport/playgroundliveviewable

It's not always straightforward because let's face it, writing documentation for a language that's only just settling in is a challenge. I'd recommend solidifying some software engineering foundations like classing, subclassing, the get/set paradigm and the more you learn the more patterns you'll spot and have an easier time following the documentation!

chuckwired
  • 147
  • 1
  • 6