swift開發筆記16

 

、、監聽系統通知(APP進入後臺)ide

NSNotification.Name.UIApplicationDidEnterBackgroundui

、、deinit == de + init,至關於oc中的dellocurl

  deinit {spa

    NotificationCenter.default.removeObserver(self)3d

  }code

、、model的encode和decode。坑:沒有寫繼承, NSCoding。例如:class Album: NSObject, NSCoding {}server

  required init(coder decoder: NSCoder) {繼承

    super.init()ip

    self.title = decoder.decodeObject(forKey: "title") as! Stringrem

    self.artist = decoder.decodeObject(forKey: "artist") as! String

    self.genre = decoder.decodeObject(forKey: "genre") as! String

    self.coverUrl = decoder.decodeObject(forKey: "cover_url") as! String

    self.year = decoder.decodeObject(forKey: "year") as! String

  }

  

  // will be called when Album to be achived

  func encode(with aCoder: NSCoder) {

    aCoder.encode(title, forKey: "title")

    aCoder.encode(artist, forKey: "artist")

    aCoder.encode(genre, forKey: "genre")

    aCoder.encode(coverUrl, forKey: "cover_url")

    aCoder.encode(year, forKey: "year")

  }

、、重寫description

  override var description: String {

    return "title: \(title)" +

           "artist: \(artist)" +

           "genre: \(genre)" +

           "coverUrl: \(coverUrl)" +

           "year: \(year)"

  }

 

、、區別是什麼

  // MARK: - Lifecycle

  override init(frame: CGRect) {

    super.init(frame: frame)

    initializeScrollView()

  }

  

  required init(coder aDecoder: NSCoder) {

    super.init(coder: aDecoder)!

    initializeScrollView()

  }

 

 

  // call reload when added to another view

  override func didMoveToSuperview() {

    reload()

  }

相關文章
相關標籤/搜索