I have read the relevant sections of Apple Swift iBook (Type Casting and Protocols), but I can find a way to indicate that the object is an instance of a particular class that conforms to a specific protocol.
As an example, in tableView(_: , cellForRowAt: )
I would like to pass a cell returned tableView.dequeueReusableCell(withIdentifier: reuseID, for: indexPath)
as a subclass UITableViewCell
that conforms to the protocol RLMEntityCapableCell
(Just indicates that the conformers have a variable named item
this instance Object
, or one of its subclasses).
This route works, but double casting seems excessive:
protocol RLMEntityCapableCell: class {
var item: Object { get set }
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: reuseID, for: indexPath) as! RLMEntityCapableCell
cell.item = items[indexPath.row]
return cell as! UITableViewCell
}
This other approach:
var cell = tableView.dequeueReusableCell(withIdentifier: reuseID, for: indexPath)
as! RLMEntityCapableCell, UITableViewCell
gives this error:
missing template
it’s so clear that this is not the right way to do this.
, UITableViewCell
UICollectionViewCell
, .
Edit:
, Realm, , Array
Dictionary
. , , , , , UITableViewCell
, RLMEntityCapableCell
. , , , ( Object
), , .