Does swift playground support UIKit?

前端 未结 10 1162
花落未央
花落未央 2020-12-07 23:54

I tried to create a UILabel in playground but failed. Does playground only support OS X development for now?

相关标签:
10条回答
  • 2020-12-08 00:24

    In Xcode 8 XCPlaygroundPage.currentPage.liveView is deprecated. Instead, use

    import PlaygroundSupport 
    
    PlaygroundPage.current.liveView = view
    
    0 讨论(0)
  • 2020-12-08 00:28

    YES, it does!

    File: New > File... > iOS > Source > Playground

    import UIKit
    let lbl = UILabel(frame: CGRectMake(0, 0, 300, 100))
    lbl.text = "Hello StackOverflow!"
    

    Then, save the file. This will trigger the Playground to interpret UI related things. Sometimes you may need to toss in a trailing newline and save again -- its a beta. At this point, the word "UILabel" should appear on the right-hand side.

    ios playground quickview

    Now, to actually view what you've done, you've got to click on the "Quick View" eye on the right, or the white circle to open it in Assistant Editor:

    Here's a screenshot of some basic things with UIImage working, etc. ios playground example

    0 讨论(0)
  • 2020-12-08 00:30

    Yeah looks like it doesn't support UIkit yet.

    Update: When i wrote this answer in 2014 it didn't support UIKit, now it does. Leaving this for historical reference

    Edit: Actually above answer is incorrect.

    You can create iOS project and add new .playground file inside that project. Then you can import Uikit or another iOS specific framework.

    0 讨论(0)
  • 2020-12-08 00:32

    Press CMD+Option+1 and change the platform to iOS, this will allow you to import UIKit.

    0 讨论(0)
  • 2020-12-08 00:36

    Press Option+Cmd+1 and choose iOS in the Platform setting. Then you can import UIKit and play~

    0 讨论(0)
  • 2020-12-08 00:38

    I found I could add a new playground file in IOS project, and in that file I can import UIKit.

    0 讨论(0)
提交回复
热议问题