Created
March 7, 2015 10:32
-
-
Save tudormunteanu/98e0a851e7c042715caf to your computer and use it in GitHub Desktop.
UIView and playgounds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Playground - noun: a place where people can play | |
| import UIKit | |
| import XCPlayground | |
| var str = "Hello, playground" | |
| class Person { | |
| var age:Int | |
| var name:String | |
| var gender:String | |
| var profession:String | |
| var location:String | |
| var partner:Person? | |
| init(name:String, gender:String, profession:String, location:String, age:Int) { | |
| self.name = name | |
| self.gender = gender | |
| self.profession = profession | |
| self.location = location | |
| self.age = age | |
| } | |
| func createRelationship(partner:Person) { | |
| self.partner = partner | |
| } | |
| func displayRelationshipMessage() { | |
| if partner != nil { | |
| println("\(name) is in a relationship with \(partner!.name)") | |
| } else { | |
| println("\(name) is single") | |
| } | |
| } | |
| } | |
| var peter = Person(name: "Peter", gender: "Male", profession: "Software Developer", location: "New York City", age:34) | |
| var amy = Person(name: "Amy", gender: "Female", profession: "Professor", location: "San Francisco", age:26) | |
| peter.createRelationship(amy) | |
| peter.displayRelationshipMessage() | |
| amy.displayRelationshipMessage() | |
| class Shape : UIView { | |
| var color:UIColor | |
| init(color:UIColor) { | |
| self.color = color | |
| super.init(frame: CGRectZero) | |
| self.backgroundColor = color | |
| } | |
| required init(coder aDecoder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| func getArea() -> Double { | |
| return 0 | |
| } | |
| } | |
| class Circle: Shape { | |
| var radius:Int | |
| init(radius:Int, color:UIColor) { | |
| self.radius = radius // Circle sets is own radius | |
| super.init(color: color) // But it asks the superclass to set the color, since Shape already knows how to do that | |
| self.frame.size = CGSize(width: radius * 2, height: radius * 2) | |
| self.layer.cornerRadius = CGFloat(radius) | |
| } | |
| required init(coder aDecoder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| } | |
| class Square: Shape { | |
| var width:Int | |
| init(width:Int, color:UIColor) { | |
| self.width = width | |
| super.init(color: color) | |
| self.frame.size = CGSize(width: width, height: width) | |
| } | |
| required init(coder aDecoder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| override func getArea() -> Double { | |
| return (Double(width * width)) | |
| } | |
| } | |
| var circle = Circle(radius: 50, color: UIColor.blueColor()) | |
| var square = Square(width: 100, color: UIColor.redColor()) | |
| square.getArea() | |
| let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 300)) | |
| XCPShowView("Title", view) | |
| square.center = CGPoint(x: 50, y: 50) | |
| view.addSubview(square) | |
| circle.center = CGPoint(x: 140, y: 140) | |
| view.addSubview(circle) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment