Skip to content

Instantly share code, notes, and snippets.

@perpeer
Created March 10, 2020 06:05
Show Gist options
  • Select an option

  • Save perpeer/7fefde262a354cfc260bd76db9a73d06 to your computer and use it in GitHub Desktop.

Select an option

Save perpeer/7fefde262a354cfc260bd76db9a73d06 to your computer and use it in GitHub Desktop.
import UIKit
final class Element {
init() {
print("Element init is called")
}
deinit {
print("Element deinit is called")
}
}
final class ArrayHandler {
let array = [Element(), Element()]
}
final class Main {
var slice: ArraySlice<Element>
init() {
let arrayHandler = ArrayHandler()
slice = arrayHandler.array.dropLast() // `slice` holds a strong reference of `arrayHandler.array`
}
}
var main = Main()
main = Main()
/*
**Print**
Element init is called
Element init is called
Element init is called
Element init is called
Element deinit is called
Element deinit is called
**Resource**
Marcosantadev: https://marcosantadev.com/arrayslice-in-swift/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment