This is my protocol:
protocol LiveTableViewCellProtocol: class {
var data: LiveCellObjectProtocol! { get set }
}
This is my class:
class RepliesTableViewCell: UITableViewCell, LiveTableViewCellProtocol {
var data: RepliesCellObject! //ERROR! does not conform to protocol.
}
RepliesCellObject is defined as:
public class RepliesCellObject: NSObject , LiveCellObjectProtocol{
//basic stuff here.
}
RepliesCellObject is a LiveCellObjectProtocol ... so why doesn't my table cell conform?
It doesn't conform because in an object that conforms to LiveTableViewCellProtocol, you can set data to any LiveCellObjectProtocol, including one that isn't an NSObject. In RepliesTableViewCell, you can't do that. The data must be set to a LiveCellObjectProtocol that is also an NSObject.
Therefore RepliesTableViewCell doesn't conform to LiveTableViewCellProtocol.
It must specifically be the same as the protocol says. What you're doing is not allowed because it is not the exact same. Remember, you can use as! to make it a RepleisCellObject if you're sure it is.
associatedtype can help here
protocol LiveTableViewCellProtocol: class {
associatedtype Data: LiveCellObjectProtocol
var data: Data! { get set }
}
You should use associated type
protocol LiveTableViewCellProtocol: class {
associatedtype Object : LiveCellObjectProtocol
var data: Object! { get set }
}
Related
I have this sample protocol having a default valued property with extension.
protocol SampleProtocol{
var sample:String?{get set}
}
extension SampleProtocol{
var sample:String?{ get { return nil } set{} }
}
Now, my TestClass implements SampleProtocol as below.
class TestClass:SampleProtocol {
var sample: String?{
return "TestClass"
}
}
And there's one helper method that prints a sample value from the SampleProtocol.
func printValue(_ value: SampleProtocol){
print(value.sample)
}
Now the problem is
let testObj = TestClass()
print(testObj.sample) // prints "TestClass"
printValue(testObj) // prints nil
From the above result, I need to understand why when the testObj is typecasted into SampleProtcol, it is considering default implementation from extension instead of the implementation from the TestClass?
The problem is that the TestClass is not implementing the SampleProtocol so when you pass it in the printValue method that takes a SampleProtocol, it will call the default implementation in the protocol extension(since the instance you pass does not implement the protocol method).
In the SampleProtocol you have defined the variable as {get set} but in the TestClass variable you have only provided a getter, this does not match the requirements (If you remove the protocol extension you will see an error that the class does not conform to the protocol).
It will work if you provide a setter:
class TestClass: SampleProtocol {
var sample: String? {
get {
return "TestClass"
} set { }
}
}
I'm trying to learn protocols and associatedtypes. I have couple of protocols which declare associatedtypes, starting with:
protocol MasterViewModel {
associatedtype Item: AWMediaItem
...
}
AWMediaItem is another protocol
protocol AWMediaItem {
var name: String { get }
var source: AWMediaSource { get }
}
And AWAlbum is yet another protocol which inherits from AWMediaItem
protocol AWAlbum: AWMediaItem {
var albumName: String { get }
...
}
For some reason, in a class implementing MasterViewModel protocol, I cannot set the AWAlbum to be the Item.
final class AlbumsMasterViewModel: MasterViewModel {
typealias Item = AWAlbum // Error
...
}
The warning I get is
Possibly intended match 'AlbumsMasterViewModel.Item' (aka 'AWAlbum') does not conform to 'AWMediaItem'
If I understand correctly, all AWAlbum's will implement AWMediaItem so why is this not working?
I think you meant to write
final class AlbumsMasterViewModel<Item: AWAlbum>: MasterViewModel {
}
I assume that when you write :
typealias Item = AWAlbum // Error
you want AlbumsMasterViewModel's item to conform to your AWAlbum protocol but you just create a typelias meaning that Item is just an alias for AWAlbum .
If you want to use a type alias you need a concrete type conforming to AWMediaItem, not a protocol inheriting from it. eg :
class ConcreteAlbum: AWAlbum {
var albumName: String
var name: String
var source: AWMediaSource
...
}
final class AlbumsMasterViewModel: MasterViewModel {
typealias Item = ConcreteAlbum // No Error
}
Edit
if you want to use AlbumsMasterViewModel with multiple Item types you can also declare it that way :
final class AlbumsMasterViewModel<Item: AWMediaItem>: MasterViewModel {
}
What's the difference between Protocols and class-bound Protocols, and which one we should use in Swift?
protocol A : class { ... }
protocol A { ... }
We get an error when attempting to add a weak delegate when the Protocol is not defined as : class:
protocol A { ... }
weak var delegate: A
Gives the error:
'weak' cannot be applied to non-class type
or
'weak' must not be applied to non-class-bound 'A'; consider adding a protocol conformance that has a class bound
Swift >= 4:
protocol A : AnyObject { ... {
Swift < 4:
protocol A : class { ... }
defines a "class-only protocol": Only class types (and not structures or enumerations) can adopt this protocol.
Weak references are only defined for reference types. Classes
are reference types, structures and enumerations are value types.
(Closures are reference types as well, but closures cannot adopt
a protocol, so they are irrelevant in this context.)
Therefore, if the object conforming to the protocol needs to be stored in a weak property then the protocol must be a class-only protocol.
Here is another example which requires a class-only protocol:
protocol A {
var name : String { get set }
}
func foo(a : A) {
a.name = "bar" // error: cannot assign to property: 'a' is a 'let' constant
}
This does not compile because for instances of structures and enumerations, a.name = "bar" is a mutation of a. If you define
the protocol as
protocol A : class {
var name : String { get set }
}
then the compiler knows that a is an instance of a class type to that
a is a reference to the object storage,
and a.name = "bar" modifies the referenced object, but not a.
So generally, you would define a class-only protocol if you need
the types adopting the protocol to be reference types and not value types.
If you are using Swift 4 or later, use AnyObject:
protocol A : AnyObject { ... }
Using class as before gives the warning and fix-it:
Using 'class' keyword to define a class-constrained protocol is deprecated; use 'AnyObject' instead
Replace 'class' with 'AnyObject'
You can make the protocol derive from any class type like NSObject or AnyObject:
protocol TopNewsTableDelegate : AnyObject {
func topNewsTableDidLoadedStories()
}
Or you can type like this
#objc protocol A { ... }
then you can make a weak delegate reference
protocol CustomProtocolName : NSObjectProtocol {
// ...
}
protocol BasePresenterProtocol : class {}
protocol DashboardPresenterProtocol : BasePresenterProtocol {}
final class DashboardPresenter {
weak var view: DashboardPresenterProtocol?
init() {
self.view = DashboardViewController()
}
func test() {
print("Hello")
}
}
extension DashboardPresenter: DashboardViewProtocol { }
protocol BaseViewProtocol : class {
weak var view: BasePresenterProtocol? { get set }
}
protocol DashboardViewProtocol : BaseViewProtocol {
}
class DashboardViewController {
}
extension DashboardViewController: DashboardPresenterProtocol { }
In the above code, I get an error at following line
extension DashboardPresenter: DashboardViewProtocol { }
that, DashboardPresenter doesn't confirm to protocol DashboardViewProtocol, but I have declared weak var view: DashboardPresenterProtocol? in DashboardPresenter . Although I have declared
Why am I getting this error ? Please let me know what I am doing wrong in this code.
You cannot implement a read-write property requirement of type BasePresenterProtocol? with a property of type DashboardPresenterProtocol?.
Consider what would happen if this were possible, and you upcast an instance of DashboardPresenter to DashboardViewProtocol. You would be able to assign anything that conforms to BasePresenterProtocol to a property of type DashboardPresenterProtocol? – which would be illegal.
For this reason, a read-write property requirement has to be invariant (although it's worth noting that a readable-only property requirement should be able to be covariant – but this currently isn't supported).
The following code should explain what I want to do reasonably clearly. Of course, this produces a compile time error.
What is the correct way to do what the below code intends?
protocol FilterableDataSource {
var dataClass: AnyClass { get }
var data: [dataClass.dynamicType] { get }
}
You can't use generics with protocols, but if you try, Swift gives you a hint - associated types. Code rarely explains intent clearly, but I'm guessing what you want to achieve is along the lines of...
protocol FilterableDataSource {
typealias T
var data: [T] { get }
}
class MyData { }
class MyClass: FilterableDataSource {
typealias T = MyData
var data: [MyData] { return [MyData]() }
}
print(MyClass().data.count)
I would use AnyObject instead of AnyClass. Then you can just cast it into whatever you want.
I believe what you want to do is define a typealias requirement in your protocol.
protocol FilterableDataSource
{
typealias DataClass : AnyObject
var data: [DataClass] { get }
}
class PersonDataSource:FilterableDataSource
{
typealias DataClass = Person
var data[Person] = []
}
So instead of requiring your filterable data sources to devine a variable containing the class, you require them to define a typealias for the elements of the data[] variable. Ideally your data classes have a common ancestor other than AnyObject which you can use as the typealias restriction.