검색결과 리스트
needsIndefiniteExecution에 해당되는 글 1건
- 2015.10.27 [swift2.1] Xcode7.1 Playground 변경사항
글
[swift2.1] Xcode7.1 Playground 변경사항
Swift
2015. 10. 27. 21:19
Xcode7.1 에서 변경된 Playground 기능에 대해 알아보겠습니다.
1. 파일, 이미지, 컬러를 editor로 드래그해서 객체 리터럴을 생성할 수 있습니다.
- 리터럴은 플랫폼 특정 타입으로 아래와 같이 변환됩니다.
- Color 리터럴 -> NSColor 또는 UIColor
- File 리터럴 -> NSURL
- Image 리터럴 -> NSImage 또는 UIImage
이미지 리터럴
- Resources 폴더에서 이미지 파일을 에디터로 드래그 하면 그림과 같은 리터럴 형태로 추가됩니다.
UIImage 객체의 imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) 메소드는 템플릿 형태로 렌더링 된 UIImage를 반환합니다. 검정-투명 으로만 이루어진 이미지에서 black 영역을 UIImageView 의 tintColor 속성을 사용해서 원하는 색으로 변경할수 있습니다.
컬러 리터럴
- Editor > Insert Color Literal 메뉴를 선택하면 컬러 선택창이 나타나 컬러를 변경할 수 있습니다.
파일 리터럴
- Resources 폴더의 파일을 드래그하면, 아래와 같이 파일 리터럴 형태로 추가됩니다.
2. XCPlayground 가 제공하는 API가 상당수 변경되었습니다.
- API들이 전역함수에서 XCPlaygroundPage 클래스의 메소드 형태로 변경되었습니다.
API 변경사항으로 XCPCaptureValue, XCPShowView, XCPSetExecutionShouldContinueIndefinitely, XCPExecutionShouldContinueIndefinitely 함수와 XCPSharedDataDirectoryPath 전역상수는 deprecated 되었음. Xcode 이후 버전에서 제거될 예정.
1) 현재 페이지에 대한 참조는 XCPlaygroundPage.currentPage 를 사용합니다.
import XCPlayground
XCPlaygroundPage.currentPage
2) 타임라인으로 값을 캡처하려면XCPlaygroundPage.captureValue(_:withIdentifier:) 을 사용합니다.
// XCPCaptureValue("식별자", value: i) : deprecated
for i in 1...10 {
XCPlaygroundPage.currentPage.captureValue(i, withIdentifier: "식별자")
}
3) 비동기 코드를 포함할 경우 XCPlaygroundPage.needsIndefiniteExecution 을 사용하세요.
- XCPlaygroundPage.needsIndefiniteExecution 를 true로 설정하면, 실행흐름이 playground 파일 끝에 도달해도 실행을 중단하지 않고, 대기합니다.
// XCPSetExecutionShouldContinueIndefinitely(true) : deprecated
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
4) XCPlaygroundPage.liveView 를 설정하면 타임라인에 노출됩니다.
- liveView 속성에 nil이 아닌 값을 설정하면 .needsIndefiniteExecution 가 true 로 설정됩니다.
- StackView 를 타임라인에 노출하는 예제입니다.
class StackView: UIView {
var kElementHeight = CGFloat(50)
var elements:[Int] = []
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.blackColor()
super.layer.borderColor = UIColor.blueColor().CGColor
super.layer.borderWidth = 1
self.layout()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func layout() {
self.subviews.map { $0.removeFromSuperview() }
for i in 0..<self.elements.count {
let value = self.elements[i]
let elementLabel = UILabel(frame:CGRectZero)
elementLabel.backgroundColor = UIColor.greenColor()
elementLabel.text = "\(value)"
elementLabel.textAlignment = NSTextAlignment.Center
elementLabel.font = UIFont.systemFontOfSize(15)
elementLabel.textColor = UIColor.whiteColor()
self.addSubview(elementLabel)
elementLabel.frame = CGRectMake(8, self.frame.size.height-(8+kElementHeight)*CGFloat(i+1), self.frame.size.width-8*2, kElementHeight)
}
}
func push(element:Int) {
self.elements.append(element)
self.layout()
}
func pop() -> Int? {
if self.elements.count > 0 {
let topElement = self.elements.removeLast()
self.layout()
return topElement
} else {
return nil
}
}
}
let bounds = CGRectMake(0, 0, 320, 400)
let view = UIView(frame: bounds)
view.backgroundColor = UIColor.whiteColor()
// liveView 속성을 설정하면, Timeline 에 표시됩니다.
// XCPlaygroundPage.currentPage.needsIndefiniteExecution = true 가 자동으로 설정됩니다.
XCPlaygroundPage.currentPage.liveView = view
let stackView = StackView(frame: CGRectInset(view.bounds, 30, 30))
view.addSubview(stackView)
stackView.push(1)
stackView.push(2)
stackView.push(3)
5) NSURL 객체인 XCPlaygroundSharedDataDirectoryURL 전역상수가 추가되었습니다.
플랫폼 설정이 OS X 인 경우에만 유효한 상수입니다.
- XCPlaygroundSharedDataDirectoryURL 는 playground 공유 데이터 디렉토리 경로를 나타냅니다.
- OS X 에서는 ~/Document 경로에 Shared Playground Data 디렉토리를 수동으로 생성해야 합니다.
- iOS 플랫폼에서는 playground 파일마다 경로가 다르기 때문에 데이터를 공유할 수 없습니다.
3. XCPlayground는 라이브 뷰로 뷰컨트롤러를 지원합니다.
LiveView 는 XCPlaygroundLiveViewable 프로토콜을 구현하는 객체입니다.
- UIView, UIViewController 는 디폴트로 XCPlaygroundLiveViewable 프로토콜을 구현합니다.
- XCPlaygroundLiveViewable 프로토콜 명세는 아래와 같습니다.
public protocol XCPlaygroundLiveViewable {
public func playgroundLiveViewRepresentation() -> XCPlayground.XCPlaygroundLiveViewRepresentation
}
public enum XCPlaygroundLiveViewRepresentation {
// 루트뷰를 갖지 않는 최상위뷰
case View(UIView)
// 루트 뷰컨트롤러를 갖지 않는 최상위 뷰컨트롤러
case ViewController(UIViewController)
}
- 위에서 구현했던 StackView 를 활용하여 네비게이션 컨트롤러를 라이브 뷰로 설정하는 예제입니다.
let stackView = StackView(frame: CGRectMake(0, 0, 200, 400))
stackView.push(1)
stackView.push(2)
stackView.push(3)
let contentViewController = UIViewController()
contentViewController.title = "StackView"
contentViewController.view.addSubview(stackView)
let liveViewController = UINavigationController(rootViewController: contentViewController)
XCPlaygroundPage.currentPage.liveView = liveViewController
4. XCPlaygroundPage.currentPage.finishExecution()를 호출해서 프로그래밍적으로 playground 실행을 중단 할 수 있습니다.
- 이 메서드는 Xcode가 현재 playground 페이지 실행을 중단하도록 지시하여, 적절히 클린업을 수행하고 종료할 수 있도록 합니다.
XCPlaygroundPage.currentPage.finishExecution()
'Swift' 카테고리의 다른 글
| [swift2] Currying 함수 (0) | 2015.11.25 |
|---|---|
| [swift2] Core Image Filter 예제 (0) | 2015.11.10 |
| [swift2.1] Xcode7.1 Playground 변경사항 (0) | 2015.10.27 |
| [swift2] Functor 와 Monad (0) | 2015.10.25 |
| [swift2] 타입변환 연산자 (is, as, as?, as!) (0) | 2015.10.18 |
| [swift] Swift 코딩환경 Playground (0) | 2015.10.05 |
needsIndefiniteExecution,
playground,
Swift,
swift2.1,
XCPlayground,
XCPlaygroundPage.liveView,
스위프트,
이미지리터럴,
컬러리터럴,
파일리터럴,
플레이그라운드