Development Tip

Swift Playground는 UIKit을 지원합니까?

yourdevel 2020. 11. 19. 22:07
반응형

Swift Playground는 UIKit을 지원합니까?


UILabel놀이터에서 만들려고 했지만 실패했습니다. Playground 는 현재 OS X 개발 만 지원 하나요?


예, 그렇습니다!

파일 : 새로 만들기> 파일 ...> iOS> 소스> 플레이 그라운드

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

그런 다음 파일을 저장 하십시오. 그러면 플레이 그라운드가 UI 관련 사항을 해석하도록 트리거됩니다. 때로는 후행 줄 바꿈을 던져 다시 저장해야 할 수도 있습니다. 베타입니다. 이 시점에서 "UILabel"이라는 단어가 오른쪽에 나타나야합니다.

iOS 놀이터 빨리보기

이제 실제로 수행 한 작업을 보려면 오른쪽의 "Quick View"눈을 클릭하거나 흰색 원을 클릭하여 Assistant Editor에서 열어야합니다.

다음은 UIImage가 작동하는 몇 가지 기본 사항의 스크린 샷입니다. iOS 놀이터 예


Edited @ 2014-11-13 : 새로운 xcode 6이이 문제를 해결 한 것 같습니다.

아니, 그렇지 않습니다. 그러나 UIKit 가져올 있다는 점은 주목할 가치가 있습니다 .

UIKit을 가져 오려면 다음을 따르십시오.

  1. 보기-> 유틸리티-> 파일 검사기 표시 (opt + cmd + 1)
  2. Xcode의 오른쪽에서 "Playground Settings-> Platform"을 OS X에서 iOS로 변경합니다.

그런 다음 UIKit 또는 iOS 용 모듈을 가져올 수 있습니다.

추신. UIImageView를 만들려고했지만 오른쪽에 올바른 이미지가 표시되지 않습니다. UIKit을 가져 오는 것은 쓸모없는 것 같습니다.


엑스 코드 7 년, 지금은 사용할 수 없습니다 Quick Looka의 모양을보고 UIView.

대신 Assistant Editor및 다음을 사용하십시오 .

XCPlaygroundPage.currentPage.liveView = sampleView

이렇게 :

import XCPlayground
import UIKit

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

// Simulate User Interaction, not available in Xcode 7.2
func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

let color = UIColor(red: 1, green: 1, blue: 0, alpha: 1)
let leftMargin = 20
let view = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 667)) // iPhone 6 proportions
view.backgroundColor = UIColor.grayColor()

// LABEL
let label = UILabel(frame: CGRect(x: leftMargin, y: 5, width: 300, height: 44))
label.text = "Hello, playground"
label.textColor = UIColor.whiteColor()
view.addSubview(label)

// TEXTFIELD
let textField = UITextField(frame: CGRect(x: leftMargin, y: 60, width: 300, height: 44))
textField.placeholder = "Edit me…"
textField.backgroundColor = UIColor(white: 1, alpha: 0.5)
textField.textColor = UIColor.whiteColor()
textField.userInteractionEnabled = true
view.addSubview(textField)

XCPlaygroundPage.currentPage.liveView = view

delay(1.0) { () -> () in
    textField.text = "New text!"
}

Xcode 8 XCPlaygroundPage.currentPage.liveView에서는 더 이상 사용되지 않습니다. 대신

import PlaygroundSupport 

PlaygroundPage.current.liveView = view

CMD + Option + 1을 누르고 플랫폼을 iOS로 변경하면 UIKit을 가져올 수 있습니다.


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


please use Command (⌘) + Option(⌥) + 1 combination to switch to iOS platform from OSX in playground to use UIKit .


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


Most simple solution: Xcode 9.2

  • Start with a new Single View playground:

여기에 이미지 설명 입력

  • show Assistance Editor: View -> Assistance Editor -> Show Assistance Editor.

  • here you go. By default you will see printed property: label.text = "Hello World!" in the Live View window


Yeah looks like it doesn't support UIkit yet.

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.

참고 URL : https://stackoverflow.com/questions/24009598/does-swift-playground-support-uikit

반응형