Non-strong references not working in playground
The playground is only a playground. It isn't a very exact representative of real life. So you should not be surprised if it treated memory management differently from real life. (Another example: the top level of a playground is clearly not like the top level of a real Swift file, since you can put things there that are not declarations and they work.)
You can use a playground for developing algorithms interactively, but only real life (i.e. a compilable executable .swift file) is real life.
For example, I put this in the App Delegate of an actual iOS app:
import UIKit
class Customer {
let name: String
var card: CreditCard?
init(name: String) {
self.name = name
}
deinit { println("\(name) is being deinitialized") }
}
class CreditCard {
let number: Int
unowned let customer: Customer
init(number: Int, customer: Customer) {
self.number = number
self.customer = customer
}
deinit { println("Card #\(number) is being deinitialized") }
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
var cust = Customer(name:"Matt")
var cc = CreditCard(number:1234, customer:cust)
cust.card = cc
return true
}
}
I saw the two println
messages, proving that there was no retain cycle. If I deleted the unowned
keyword and ran again, I didn't see the two println
messages, proving that there was a retain cycle. Thus we know that unowned
does what it is advertised to do. That's all we need to know. What works or doesn't work in a playground isn't terribly important.
A puzzle: weak reference not working in Swift 4?
I believe this has something to do with how Playgrounds treat code. It doesn't have quite the same lifecycle and you can't expect variables to be deallocated once your code is done running.
If you make your objects be optional, everything deallocates as expected:
do {
let user1: User? = User(name: "John")
let phone1: Phone? = Phone(model: "iPhone7")
user1?.phone = phone1
phone1?.user = user1
}
User John is initialized
Phone iPhone7 is initialized
Phone iPhone7 is deallocated
User John is deallocated
Done
Strong Reference not turning into weak when deinitialize
Playgrounds can hold strong references to your data for the purpose of previews. To get a true depiction of the deinitialization of a piece of code, test it outside of a playground.
Furthermore, when a weak reference exists to an object, the deinitializer isn't actually called immediately when the last weak reference is released. Instead, the object is deinitialized the next time the weak reference is attempted to be accessed. See this blog post for more details.
Why this code doesn't work in Kotlin Playground or other IDEs?
Your code is working but you are not using the result of solution
method.
fun main() {
var inputArray = mutableListOf(3, 6, -2, -5, 7, 3)
val output = solution(inputArray)
// do something with output. At least print it to see
println("output is $output") // output is 21
}
fun solution(inputArray: MutableList<Int>): Int {
return inputArray.zipWithNext(Int::times).maxOrNull() ?: 0
}
Related Topics
How to Capture Depth Data from Camera in iOS 11 and Swift 4
Swift: Optional Text in Optional Value
I Won't Be Able to Return a Value with Alamofire in Swift
How to Retrieve a Random Object from Firebase Using a Sequential Id
New @Convention(C) in Swift 2: How to Use It
Get the Size (In Bytes) of an Object on the Heap
Change Background Color of Texteditor in Swiftui
How to Import a Swift Function Declared in a Compiled .Swiftmodule into Another Swift File
Swift Error: Binary Operator '&&' Cannot Be Applied to Two 'Bool' Operands
Checking If Textfields Are Empty Swift
Viewcontroller Slide Animation
Why Does Swift Playground Shows Wrong Number of Executions
Single-Element Parethesized Expressions/Tuples VS Common Use of Parentheses
Implicit Return in a Closure Causing an Error