Swift: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 99: Line 99:
</syntaxhighlight>
</syntaxhighlight>
==Optional Properties==
==Optional Properties==
We can make properties optional. Like most languages we need to test
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
class BlogPost {
class BlogPost {
   var title:String?
   var title:String?
   var body:String?
   var body = "hey"
   var counter = 0
   var counter = 0
}
}
let post = BlogPost()
// We test like this.
if let actualTitle = post.title {
  print(actualTitle)
}
</syntaxhighlight>
==Initialise (Constructor)==
==Initialise (Constructor)==
We use self in place of this for other languages. Don't forget about argument labels. Note more than one init() permitted
We use self in place of this for other languages. Don't forget about argument labels. Note more than one init() permitted

Revision as of 01:32, 24 June 2022

Introduction

Here is my first dip into Apple and swift Swift Cheat Sheet

Data Types

Here we have the following Primatives

  • Int
  • Float
  • Double
  • Str
  • Bool

Control

If Statements

No surprises

if a < 4 {
  print("we are here 1")
}
else if a == 4 && a < 3 {
  print("we are here 2")
}
else {
  print("we are here 3")
}

Switch Statements

var aCharacter = "a"
switch aCharacter {
   case "a":
      print("we are here a")
   case "b":
      print("we are here b")
   default:
      print("we are here not a or b")
}

Loops

Example for loop

var sum = 0
for index in 1..5 {
   sum += index 
   print(sum)
}

Example for while loop

var counter = 5
while counter  > 0 {
   print("hello")
   counter -= 1  
}

Example for repeat while loop

var counter = 5
repeat {
   print("hello")
   counter -= 1  
} while counter  > 0

Functions

Simple example

func foo() {
  print("Fred")
}
foo()

The argument label, the first argument, is just a name to use in place or variable name

func addTwoNumbers(
  arg1 para1:Int,
  arg2 para2:Int) -> Int {
  return para1 + para2
}
let sum = addTwoNumbers(arg: 2)
print(sum)

Classes

Base Claas

class BlogPost {
  var title = ""
  var body = ""
  var counter = 0
  func addCounter() {
    counter += 1 
  }
}

let myPost = BlogPost()
myPost.title = "Hello"
myPost.body = "My Body"
myPost.addCounter()
print(myPost.counter)

Optional Properties

We can make properties optional. Like most languages we need to test

class BlogPost {
  var title:String?
  var body = "hey"
  var counter = 0
}
let post = BlogPost()

// We test like this.
if let actualTitle = post.title {
   print(actualTitle)
}

Initialise (Constructor)

We use self in place of this for other languages. Don't forget about argument labels. Note more than one init() permitted

class Person {
  var name = ""
  var age = 0

  func init(_ name: String) {
    self.name = name
    self.age = 22
  }

  func init(_ name: String, _ age: Init) {
    self.name = name
    self.age = age
  }
}
var a Person("Bill", 21)

Inheritance

We do this using a colon

class ImprovedBlog : BlogPost {
  var title = ""
  var body = ""
  var numberOfLikes = 0
  func addNumberOfLikes() {
    numberOfLikes += 1 
  }
}

Override can be used and you can call the base implementation

class NewCar : Car {
  override func drive() {
    print("Printing my stuff")
    // Print base stuff 
    super.drive()
  }
}