Company recently another app LIVE is successfully launched, finally have some time to see what they want to know, Swift also out for so long, had just watched the grammar, and didn’t have a chance to use the company projects, unknowingly have already forgot, planning to brush up on the Swift today, imitation may be some other app to headphones. I will slowly record the imitation process later. Also write a simple note, convenient to come back to read.

1. Possible circular references caused by closures and solutions

Closures are very similar to OC blocks, as anyone who has studied swift syntax knows. First, to simulate A cyclic reference scenario, such as navigation controller A push to B, we return A from B so that B is destroyed. But how does swift know that it’s destroyed? Well, in OC you can override the dealloc method, it’s destroyed when it’s called, and then there’s no method in Swift, but there’s another method deinit that’s called when it’s destroyed.

First we define a method in controller B that passes a closure parameter, defined as follows

Func loadNewData(finish:()->()) {print(" I want to load data, this is a long time ")Copy the code

And then we call it in viewDidLoad in controller B, like this

// Call loadNewData {print(" I update UI here ")}Copy the code

Rewrite the deinit method

Deinit {print(" I was destroyed ")}Copy the code

I’m going to push to B and then I’m going to call deinit because we don’t have a circular reference here. Finally we define a closure property in B as follows

var finish:(()->())?Copy the code

Change the loadNewData method

Func loadNewData(finish:)->()) {print(" I want to load data, this is a long time ") // I go back to the main thread to update UI self.finish = Finish Finish ()}Copy the code

I’ll change the call as well

/ / call

LoadNewData {print (" I'm here to update the UI ") the self. The backgroundColor = UIColor. RedColor ()}Copy the code

And then we push again, and then we go back and we find that deinit is not called. This indicates that a circular reference has occurred.

The solution is as follows

/ / call

Weak var weakSelf = self loadNewData {print(" I update UI here ") weakSelf! .view.backgroundColor = UIColor.redColor() }Copy the code

How do I override setter and getter methods?

class Student: NSObject {

var _name:String?
var name:String? {

    get {

        return _name
    }

    set {

        _name = newValue
    }
}
}Copy the code

I have to make fun of it. It really hurts. So if we need to do something in a setter method, we usually write it like this

var age:Int? DidSet {print("\(age)")}} didSet {print("\(age)")}}Copy the code

Note: If we override only the getter method, it is equivalent to the READonly property in OC

var gender:String? Get {return "1"}} get {return "1"}Copy the code

How to use KVC

To define dictionary-model methods in Swift, you must call super.init. Properties of object types can be optional, but properties of primitive data types such as Int must be initialized, otherwise they will crash.

I started by defining two properties, an object property and a base data property, as follows

var name:String?
var age :Int?Copy the code

Dictionary to model method

Init (dict: [String: AnyObject]) {/ / use in swift kvo. Call the super init () setValuesForKeysWithDictionary (dict)}Copy the code

Runtime crash error…

I’ll just change the age property to the following,

  var name:String?
var age: Int = 0Copy the code

How to write lazy loading?

In OC lazy load almost everywhere kick, how to write swift? There is a lazy keyword, written as follows

   lazy var dataList:[String] = {

    return ["1","2","3"]
}()Copy the code

Note that the parentheses after the braces are not missing!!

At the end

These are the records of some pits I met in Swift when I came from OC. Please correct me if there is any mistake. Thank you.