Saturday, June 9, 2018

Playground examples for XCode 10 Beta 1

Playground Support for iOS12 and Swift 4.2
iOS.playground    Select all
import UIKit import PlaygroundSupport //: **Markup** //: ### Define UIView class MyView : UIView { @objc public func changeTitle(_ sender:UIButton!) { sender.setTitle("Welcome to WWDC2018", for: []) } } let myView = MyView(frame: CGRect(x:0, y:0, width:500, height:500)) var button = UIButton(type: .system) button.frame = CGRect(x:100, y:100, width:300, height:200) button.setTitle("Hi Press me!", for: []) button.tintColor = .blue button.setTitleColor(.orange, for: []) button.addTarget(myView, action: #selector(MyView.changeTitle(_:)), for: .touchUpInside) myView.addSubview(button) PlaygroundPage.current.liveView = myView




Playground Icon Drawings in iOS and Swift 4.2
macOS.playground    Select all
import UIKit //: Define IconView class IconView: UIView { override func draw(_ rect: CGRect) { drawRawBackgroundWithBaseColor(strokeColor: UIColor.orange, backgroundRectangle: self.bounds) let textAttributes: [NSAttributedString.Key : Any] = [ NSAttributedString.Key.foregroundColor: UIColor.red, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 32.0)] let FString: String = "Hello World" let distanceX: CGFloat = -12.0 let distanceY: CGFloat = 0.0 let centerX = self.bounds.midX let centerY = self.bounds.midY FString.draw(at: CGPoint(x:centerX+distanceX, y:centerY+distanceY), withAttributes: textAttributes) } } func drawRawBackgroundWithBaseColor(strokeColor: UIColor, backgroundRectangle:CGRect) { let lineWidth = backgroundRectangle.width/36.0 let cornerRadius = backgroundRectangle.width/16.0 let tileRectangle = backgroundRectangle.insetBy(dx: lineWidth/2.0, dy: lineWidth/2.0) // Stroke Drawing let strokePath = UIBezierPath(roundedRect:tileRectangle, cornerRadius:cornerRadius) strokeColor.setStroke() strokePath.lineWidth = lineWidth strokePath.stroke() // Draw an ellipse let ovalPath = UIBezierPath(ovalIn: backgroundRectangle.insetBy(dx: lineWidth*1.5, dy: lineWidth*1.5)) UIColor.blue.setStroke() ovalPath.lineWidth = lineWidth ovalPath.stroke() let context:CGContext = UIGraphicsGetCurrentContext()! context.setFillColor(UIColor.green.cgColor) context.addRect(CGRect(x: 100.0, y: 100.0, width: 60.0, height: 60.0)) context.fillPath() } //: Instantiate the UIView let rect = CGRect(x: 0.0, y: 0.0, width: 420.0, height: 320.0) let icon = IconView(frame: rect) icon.backgroundColor = UIColor.clear




CreateML for macOS 10.14 Beta 1 (requires macOS 10.14 Mojave)
macOS.playground    Select all
import Cocoa import CreateML //: Specify Data /* Input as CSV mycsv.csv: beds,baths,squareFt, price 2,2,2000,400000 4,3,2500,500000 3,2,1800,450000 3,2,1500,300000 let houseData = try MLDataTable(contentsOf: URL(fileURLWithPath: "mycsv.csv")) */ //: Input as dictionary let mydata : [String: MLDataValueConvertible] = [ "beds": [2,4,3,3], "baths": [2,3,2,2], "squareFt": [2000,2500,1800,1500], "price": [400000,500000,450000,300000] ] let houseData = try MLDataTable(dictionary:mydata) let (trainingData, testData) = houseData.randomSplit(by: 0.8, seed: 0) //: Create Model let pricer = try MLRegressor(trainingData: houseData, targetColumn: "price") //: Evaluate Model let evalator = pricer.evaluation(on: testData) print(pricer) //: Save Model try pricer.write(to:URL(fileURLWithPath: "HousePricer.mlmodel"))




No comments: