Created
August 7, 2021 05:03
-
-
Save JarvisTheAvenger/a3d8e681b8b97b55714a40a8970a68c5 to your computer and use it in GitHub Desktop.
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
import Foundation | |
// Class vs Struct | |
// reference vs value type | |
// Class supports inheritance, struct don't support inheritance | |
// Class supports ARC, in struct you don't have to worry about memory. | |
// Structs are thread safe | |
// Only class provides objective c interoperability | |
// Class provides built in identity operator === | |
// Class can be deinitialized | |
// In case of struct compiler take care of initializer if not provided | |
class Person { | |
var name : String | |
weak var address: Address? | |
init(_ name: String) { | |
self.name = name | |
} | |
deinit { | |
print("Person class is deinitialiased") | |
} | |
func doSomething() { | |
self.name = "rahul" | |
} | |
} | |
class Address { | |
var address : String | |
var person : Person? | |
init(_ address: String) { | |
self.address = address | |
} | |
deinit { | |
print("Address class is deinitialiased") | |
} | |
} | |
var address : Address? = Address("Pune") | |
address?.address = "" | |
var person : Person? = Person("Rahul") | |
person?.address = address | |
address?.person = person | |
person = nil | |
address = nil | |
struct B { | |
var b : String | |
mutating func doSomething() { | |
self.b = "" | |
} | |
} | |
var bObject = B(b: "") | |
bObject.b = "e" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment