Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //: Playground - noun: a place where people can play
- var str = "Hello, playground"
- print(str)
- // Inicio dos estudos de swift
- var optionalString: String? = "Hello"
- print(optionalString == nil)
- var optionalName: String? = "Jota"
- var greeting = "Hello"
- if let name = optionalName{
- greeting = "Hello, \(name)"
- print(name)
- }
- print(greeting)
- let nickName: String? = "Jotinha"
- let fullName: String = "Jota Freitas Jr"
- let informalGreeting = "Hi \(nickName ?? fullName)"
- print(informalGreeting)
- // PG9
- let vegetable = "red pepper"
- switch vegetable {
- case "celery":
- print("Add some raisins and make ants on a log.")
- case "cucumber", "watercress":
- print("That would make a good tea sandwich.")
- case let x where x.hasSuffix("pepper"):
- print("It is a spicy \(x)?")
- default:
- print("Everything tastes good in this soup.")
- }
- let interestingNumbers = [
- "Prime": [2, 3, 5, 7, 11, 13],
- "Fibonacci": [1, 1, 2, 3, 5, 8],
- "Square": [1, 4, 9, 16, 25]
- ]
- var lagest = 0
- for(kind, numbers) in interestingNumbers {
- for number in numbers {
- if number > lagest {
- lagest = number
- }
- }
- }
- print("The lagest number is \(lagest)")
- var n = 2
- while n < 100 {
- n = n * 2
- }
- print(n)
- var m = 2
- repeat {
- m = m * 2
- } while m < 100
- print(m)
- var total = 0
- for i in 0..<4 {
- total += 1
- }
- print(total)
- for i in 0...4 {
- total += 1
- }
- print(total)
- // Functions
- func greet(person: String, day: String) -> String {
- return "Hello \(person), today is \(day)."
- }
- print(greet(person: "BoB", day: "Wednesday"))
- func greet2(_ person: String, on day: String) -> String {
- return "Hello \(person), today is \(day)."
- }
- print(greet2("Jota", on: "Quarta"))
- func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
- var min = scores[0]
- var max = scores[0]
- var sum = 0
- for score in scores {
- if score > max {
- max = score
- } else if score < min {
- min = score
- }
- sum += score
- }
- return(min, max, sum)
- }
- let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9])
- print(statistics.sum)
- print(statistics.2)
- func sumOf(numbers: Int...) -> Int {
- var sum = 0
- for number in numbers {
- sum += number
- }
- return sum
- }
- print(sumOf(numbers: 42, 32, 56, 79, 100))
- func returnFifteen() -> Int {
- var y = 10
- func add() {
- y += 5
- }
- add()
- return y
- }
- print(returnFifteen())
- // Funcoes sao um tipo, isso significa que elas podem retornar outra funcao, por exemplo:
- func makeIncrementer() -> ((Int) -> Int) {
- func addOne(number: Int) -> Int {
- return 1 + number
- }
- return addOne
- }
- var increment = makeIncrementer()
- print(increment(7))
- // e funcoes podem tambem ser argumentos de funcoes
- func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool {
- for item in list {
- if condition(item) {
- return true
- }
- }
- return false
- }
- func lessThanTen(number: Int) -> Bool {
- return number < 10
- }
- var numbers = [20, 19, 7, 12]
- print(hasAnyMatches(list: numbers, condition: lessThanTen))
- // classes
- class Shape {
- var numberOfSides = 0
- func simpleDescription() -> String {
- return "A shape with \(numberOfSides) sides."
- }
- }
- var shape = Shape()
- shape.numberOfSides = 4
- print(shape.simpleDescription())
- class NamedShape {
- var numberOfSides = 0
- var name : String
- init(name: String) {
- self.name = name
- }
- func simpleDescription() -> String {
- return "A shape called \(name) with \(numberOfSides) sides."
- }
- }
- var namedShape = NamedShape(name: "Quadrado")
- namedShape.numberOfSides = 4
- print(namedShape.simpleDescription())
- class Square : NamedShape {
- var sideLength: Double
- init(sidelength: Double, name: String) {
- self.sideLength = sidelength
- super.init(name: name)
- numberOfSides = 4
- }
- func area() -> Double {
- return sideLength * sideLength
- }
- override func simpleDescription() -> String {
- return "A square with sides of length \(sideLength)."
- }
- }
- let test = Square(sidelength: 5.2, name: "my test square")
- test.area()
- test.simpleDescription()
- class EquilateralTriangle: NamedShape {
- var sideLength: Double = 0
- init(sideLength: Double, name: String) {
- self.sideLength = sideLength
- super.init(name: name)
- numberOfSides = 3
- }
- var perimeter: Double {
- get {
- return 3.0 * sideLength
- }
- set {
- sideLength = newValue / 3.0
- }
- }
- override func simpleDescription() -> String {
- return "A equilateral triangle with sides of length \(sideLength)"
- }
- }
- var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")
- triangle.perimeter = 9
- print(triangle.sideLength)
- print(triangle.perimeter)
- class TriangleAndSquare {
- var triangle: EquilateralTriangle {
- willSet {
- square.sideLength = newValue.sideLength
- }
- }
- var square: Square {
- willSet {
- triangle.sideLength = newValue.sideLength
- }
- }
- init(size: Double, name: String) {
- square = Square(sidelength: size, name: name)
- triangle = EquilateralTriangle(sideLength: size, name: name)
- }
- }
- var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape")
- print(triangleAndSquare.square.sideLength)
- print(triangleAndSquare.triangle.sideLength)
- triangleAndSquare.square = Square(sidelength: 50, name: "large square")
- print(triangleAndSquare.triangle.sideLength)
- let optionalSquare: Square? = Square(sidelength: 12, name: "optional square")
- let side = optionalSquare?.sideLength
- // Enums
- enum Rank: Int {
- case ace = 1
- case two, three, four, five, six, seven, eight, nine, ten
- case jack, queen, king
- func simpleDescription() -> String {
- switch self {
- case .ace:
- return "ace"
- case .jack:
- return "jack"
- case .queen:
- return "queen"
- case .king:
- return "king"
- default:
- return String(self.rawValue)
- }
- }
- }
- let ace = Rank.ace
- let aceRawValue = ace.rawValue
- let three = Rank.eight.simpleDescription()
- enum Suit {
- case spades, hearts, diamonds, clubs
- func simpleDescription() -> String {
- switch self {
- case .spades:
- return "spades"
- case .hearts:
- return "hearts"
- case .diamonds:
- return "diamonds"
- case .clubs:
- return "clubs"
- }
- }
- func color() -> String {
- switch self {
- case .spades, .clubs:
- return "black"
- case .diamonds, .hearts:
- return "red"
- }
- }
- }
- let hearts = Suit.hearts.color()
- print(hearts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement