Removing instances of a class in Swift

I am trying to fully understand the process of creating class objects in Swift. Keeping in mind the MVC idea, I have the following simple class to represent my data model:

// Person.swift

import Foundation

class Person {

    var first: String = "first"
    var last: String = "last"
}

      

In my opinion the controller has two IBOutlets

connected to UITextFields

. Using the method IBAction

, I am setting variables first

and last

with text from text boxes.

//  ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var firstName: UITextField!
    @IBOutlet weak var lastName: UITextField!

    // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - IBAction
    @IBAction func addPerson(sender: AnyObject) {

        let peep = Person()
        peep.first = firstName.text
        peep.last = lastName.text
        println("\(peep.first) \(peep.last)")
    }
}

      

Each time the method is called IBAction

, a new instance of the class is created Person

. For my example, I'm only interested in the latest instance Person

. How can I delete variables of the primary instance?

+3


source to share


1 answer


You don't need to remove them, they are released for you by the compiler.

A Person object is created. Setting some property values ​​and then printing. The object then goes out of scope, at which point, since you have no other strong references to the object, it will eventually be released.



Check out the ARC guide in the Quick Programming Guide for an explanation of the local rules applied to Swift life objects using ARC.

+3


source







All Articles