Can My Class Override Protocol Property Type in Swift

Can my class override protocol property type in Swift?

Requiring AnyObject in the protocol means the children array must be able to accept AnyObject entries. But it sounds like you want Bar's children to be only Foo objects.

Instead, you can give the protocol an associated type:

protocol Parent {
associatedtype Child
var children: [Child] { get set }
}

class Foo { }

class Bar: Parent {
var children = [Foo]()
init() {}
}

Override swift protocol property to optional

The existence of this property in XMLBaseMappable protocol is crucial in order to function correctly the whole library.

Having said that, you can't omit the implementation of this property in your structs and classes but you can "hide" it in a super class. Using this:

class BasicXMLMappable: XMLMappable {
var nodeName: String!

required init(map: XMLMap) {

}

func mapping(map: XMLMap) {

}
}

You can have XMLMappable objects that extends BasicXMLMappable and they don't have to implement nodeName property:

class TestBasicXMLMappable: BasicXMLMappable {

// Your custom properties

required init(map: XMLMap) {
super.init(map: map)
}

override func mapping(map: XMLMap) {
// Map your custom properties
}
}

Edit:
As of version 1.5.1 you can use XMLStaticMappable for implementing XMLMapper in an extension. For example:

class CustomClass {
var property: String?
}

extension CustomClass: XMLStaticMappable {
var nodeName: String! {
get {
return "default"
}
set(newValue) {

}
}

static func objectForMapping(map: XMLMap) -> XMLBaseMappable? {
// Initialize CustomClass somehow
return CustomClass()
}

func mapping(map: XMLMap) {
property <- map["property"]
}
}

Hope this helps.

How to override a protocol extension's default implementation of a func?

The default implementations in the protocols are only called if the class that conforms to these protocols do not implement that method itself. The classes' methods override the default implementations of the protocols, not the other way around.

you could do like this:

import UIKit

protocol MyProtocol : class {
func someFuncWithDefaultImplementation()
func someFunc()
var someInt:Int { get set }
}

extension MyProtocol {
func someFuncWithDefaultImplementation() {
someInt = 5
}

func someFunc() {
someFuncWithDefaultImplementation()
}
}

class MyClass : MyProtocol {
var someInt = 6
}

class MyClass2 : MyProtocol
{
var someInt: Int = 4
func someFuncWithDefaultImplementation()
{
someInt = 7
}

}

let class2 = MyClass2()
class2.someFunc()

or like this:

class MyClass :  MyProtocol {
var someInt = 6
func someFuncWithDefaultImplementation() {
someInt = 8
}
}

class MyClass2 : MyClass
{
override func someFuncWithDefaultImplementation()
{
someInt = 7
}
}

these were what i got with my tests, but you may find some better solution

Swift Protocol Extensions overriding

The short answer is that protocol extensions don't do class polymorphism. This makes a certain sense, because a protocol can be adopted by a struct or enum, and because we wouldn't want the mere adoption of a protocol to introduce dynamic dispatch where it isn't necessary.

Thus, in getColor(), the color instance variable (which may be more accurately written as self.color) doesn't mean what you think it does, because you are thinking class-polymorphically and the protocol is not. So this works:

let colorB = B().color // is "Red color" - OK

...because you are asking a class to resolve color, but this doesn't do what you expect:

let b = B().getColor() // is "Default color" BUT I want it to be "Red color"

...because the getColor method is defined entirely in a protocol extension. You can fix the problem by redefining getColor in B:

class B: A, RedColor {
func getColor() -> String {
return self.color
}
}

Now the class's getColor is called, and it has a polymorphic idea of what self is.

Implementing a concrete type's property override of its protocol extension default in array of metatypes

Include someDefaultInt in the protocol definition if you want it to enjoy dynamic dispatch.

protocol SomeProtocol {
static var myInt: Int { get set }
static var someDefaultInt: Int { get }
}

extension SomeProtocol {
static var someDefaultInt: Int { 1 }
}

Override property with different type

The way it works for Objective C is because of the dynamic nature of the language. You can redeclare the type of super class and instead of synthesizing the property you would make it a dynamic type, which would let you redeclare it.

@protocol MyTableViewDelegate<UITableViewDelegate>

- (void)demoDelegateMethod;

@end

@interface WrapperTableView: UITableView

@property (nonatomic, weak, nullable) id <MyTableViewDelegate> delegate;

@end

@implementation WrapperTableView

@dynamic delegate;

@end

But, I doubt this would be possible with Swift, since, you are changing the type completely. It is because of Swift being strong static language.

Answer Edited

I got your approach. I write above approach in Objective-C and then inherit this class in Swift.
So if I have to override SuperClass property with different type I need to create a wrapper class in Objective-C inherit from desired SuperClass and then finally instead of inheriting directly from desired super class I should inherit from the newly created WrapperClass which is written in Objective-C

class MyTableView: WrapperTableView {
//Now Here the delegate object is of type MyTableViewDelegate
}

This approach is far better then

class MyTableView: UITableView {
private var myDelegate: MyTableViewDelegate?
override var delegate: UITableViewDelegate {
set{
myDelegate = newValue
}
get{
return myDelegate
}

}
}

Swift protocol property of type SetSelf

The way to put constraints in protocols is similar to how you specify protocol conformance (they're the same thing after all)

protocol Groupable: Hashable
{
var parent: Self? { get }
var children: Set<Self> { get }
}

What does `override` mean in a protocol?

It means the protocol declares a new member that replaces an identical member in a parent protocol, though this isn't the same thing as "shadowing" (so it isn't exactly like C#'s new method-modifier keyword, also Swift supports static protocols, something C#s interface can't do).

In the link you gave for public protocol FloatingPoint, we see that FloatingPoint implements SignedNumeric.

FloatingPoint declares override mutating func negate() - but so does SignedNumeric - hence the need to add override.

The official Swift language 5.1 reference states this about the override keyword on classes (but not explicitly protocols), but the preface of the section implies that it applies to protocols insofar as it applies to all declarations:

https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID473

Methods that override a superclass method must be marked with the override declaration modifier. It’s a compile-time error to override a method without the override modifier or to use the override modifier on a method that doesn’t override a superclass method.



Related Topics



Leave a reply



Submit