Last active
August 8, 2021 11:09
-
-
Save Gabbrolee/b40ccbee9da51f3412ab2cd1f07598b0 to your computer and use it in GitHub Desktop.
dayFour.swift
This file contains 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
// struct and classes have their own method and its called properties | |
// two kind of properties observer and it is willset and didset | |
//willset provides a new value for the properties | |
//Didset gives old value | |
import UIKit | |
struct Person { | |
var clothes: String { | |
willSet { | |
updateUI(msg: "I'm changing from \(clothes) to \(newValue)") | |
} | |
didSet { | |
updateUI(msg: "I just change from \(oldValue) to \(clothes)") | |
} | |
} | |
func updateUI(msg: String){ | |
print(msg) | |
} | |
} | |
var taylor = Person(clothes: "T-shirts") | |
taylor.clothes = "short skirts" | |
// computed properties | |
struct Person1{ | |
var age: Int | |
var ageInDogYears: Int { | |
get { | |
return age * 7 | |
} | |
} | |
} | |
var fan = Person1(age: 25) | |
print(fan.ageInDogYears) | |
//MARKS:- STATIC PROPERTIES AND METHODS | |
// swift allow one to create property | |
//that belong to a type | |
// it use the keyword static | |
struct TaylorFan { | |
static var favoriteSong = "Look What You Made Me Do" | |
var name: String | |
var age: Int | |
} | |
let fan1 = TaylorFan(name: "James", age: 25 ) | |
print(TaylorFan.favoriteSong) | |
//MARKS:-ACCESS CONTROL | |
//let one specify code inside struct and classes | |
//that need to be expose or not to the outside world | |
// private propety means only swift code in same file | |
//as a type can read the property or its extensions | |
//code within the file can be access | |
//to make a property private you just write private | |
//public means everyone can read and write the property | |
class Person3{ | |
private var name : String? | |
} | |
//MARKS:- POLYMORPHISM AND TYPECASTING | |
class Album { | |
var name: String | |
init(name: String){ | |
self.name = name | |
} | |
func getPerformance() -> String{ | |
return "The album \(name) sold lots" | |
} | |
} | |
class StudioAlbum: Album { | |
var studio: String | |
init(name:String, studio: String){ | |
self.studio = studio | |
super.init(name: name) | |
} | |
override func getPerformance() -> String { | |
return "The album \(name) sold lots" | |
} | |
} | |
class LiveAlbum: Album { | |
var location: String | |
init(name: String, location: String){ | |
self.location = location | |
super.init(name: name) | |
} | |
override func getPerformance() -> String { | |
return "The album \(name) sold lots" | |
} | |
} | |
var taylorSwift = StudioAlbum(name: "Taylor Swift", studio: "The Castles Studio") | |
var fearless = StudioAlbum(name: "Fearless", studio: "Aimeeland Studio") | |
var iTunesLive = LiveAlbum(name: "iTumes Live from SoHo", location: "New York") | |
var allAlbums: [Album] = [taylorSwift, fearless, iTunesLive] | |
for album in allAlbums { | |
print(album.getPerformance()) | |
if let studioAlbum = album as? StudioAlbum{ // typecasting | |
print(studioAlbum.studio) | |
if let liveAlbum = album as? LiveAlbum{ // typecasting | |
print(liveAlbum.location) | |
} | |
} | |
} | |
// the Album class serve as a single interface to the subclasses without | |
//the subclasses losing their original class at the same time . | |
//MARKS:-TYPE CASTING | |
//This is a scenario of converting an object of one type to another | |
//optional down casting (?) this is use when not sure of conversion | |
//force down casting (!) this is when one is certain of conversion | |
// when it is force down casted and it is not true then app can crash | |
//MARKS;- CLOSURE | |
// closure are use to hold lines of swift code | |
// the copy of values use inside them can be taken | |
// below is a cocoa touch example | |
let vw = UIView() | |
UIView.animate(withDuration: 0.5, animations:{ vw.alpha = 0 | |
}) | |
// the UIView use the animate property inside it to set duration to | |
// 5seconds and use the closure animations to set opacity (alpha) | |
// to zero then continue doing that after interval of 5secs | |
UIView.animate(withDuration: 0.5) { | |
vw.alpha = 0 | |
} | |
// above is a trailing closure, it express the code in a shorter | |
// and neater way |
Author
Gabbrolee
commented
Aug 8, 2021
via email
Noted ..thanks boss
…On Sun, 8 Aug 2021, 11:51 Waheed Afolabi, ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
On line 1, it reads: Structs and classes have their methods and its called
properties.
This expression is not correct: Structs and classes have members that
could either be *properties* or *methods*!
What are methods?
Methods are simply functions that live in a struct or class.
So, they are different things.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/b40ccbee9da51f3412ab2cd1f07598b0#gistcomment-3850430>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AUFQTP7GR32KVPSMRKDUN5DT3ZOTLANCNFSM5BYHTLQA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment