我有Swift类的问题.我有一个UITableViewController类和UITableViewCell类的swift文件.我的问题是UITableViewCell类和出口.这个类有一个错误类"HomeCell"没有初始化器,我不明白这个问题.
谢谢你的回复.
import Foundation
import UIKit
class HomeTable: UITableViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableViex: UITableView!
var items: [(String, String, String)] = [
("Test", "123", "1.jpeg"),
("Test2", "236", "2.jpeg"),
("Test3", "678", "3.jpeg")
]
override func viewDidLoad() {
super.viewDidLoad()
var nib = UINib(nibName: "HomeCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "bookCell")
}
// Number row
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
// Style Cell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { …Run Code Online (Sandbox Code Playgroud) 我有一辆车和一个司机.他们互相引用.在汽车的init()中,我创建了一个驱动程序并将其分配给驱动程序成员.驱动程序成员有一个didSet方法,该方法应该设置驱动程序的汽车,从而将它们相互链接.
class GmDriver {
var car: GmCar! = nil
}
class GmCar {
var driver: GmDriver {
didSet {
driver.car = self
}
}
init() {
driver = GmDriver()
}
}
let myCar = GmCar()
println(myCar.driver.car) // nil
Run Code Online (Sandbox Code Playgroud)
但是,didSet永远不会触发.为什么?
我有我的Swift类作为下面的简单代码:
class FavoriteView: UIView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
convenience init() {
self.init(frame: CGRectZero)
}
private func commonInit() {
// init something
}
// MY PROBLEM IS HERE
var favoriteCount: Int = 0 {
didSet {
// update the view
}
}
}
Run Code Online (Sandbox Code Playgroud)
逻辑是每当favoriteCount设置为新值时,didSet运行代码.
它运行良好.但问题是代码(in didSet)不是第一次运行.我的意思是当一个新FavoriteView实例初始化时,我认为它也可以运行,但事实并非如此.
有没有办法didSet第一次运行该代码(in ).
谢谢!
我在玩 SwiftUI,我有一个看起来像这样的类:
class Foo: ObservableObject {
@Published var foo: Int! { // Implicit unwrapped optional
didSet {
print("foo")
}
}
init() {
self.foo = 1
}
}
Run Code Online (Sandbox Code Playgroud)
didSet 总是被调用。根据Apple 文档,不应调用它。@Published属性包装器有什么特别之处吗?
我已经看过
对我来说它不起作用..
我在下面创建类的项目中工作
protocol FileManagerHelper {
var fileName:String {get}
var fileCategory:FileCategory {get set}
var isFileExitsAtPath:Bool {get}
var filePath:String {get}
var fileType:FileTypes {get set}
}
class FileManager:FileManagerHelper {
// Other property
//STORED PROPERY INIT WHEN OBJECT WILL CREATED WITH FileCategory OBJECT
var fileCategory:FileCategory {
didSet {
switch fileCategory {
case .XYZ:
print("Test")
... other cases
}
}
required init(fileCategory:FileCategory,fileType:FileTypes = .Image) {
self.fileCategory = fileCategory
self.path = self.folderPath + self.fileName
}
}
Run Code Online (Sandbox Code Playgroud)
没有设置方法没有调用 fileCategory
注意:我不想给出默认值,我想从init方法中传递它运行时 …