swift-protocols

Swift Protocols with Associated Type Requirement and Default Implementation

无人久伴 提交于 2020-01-01 16:25:49
问题 I have been struggling very hard with Swift Protocols and Associated Types for a long time. I started again with basic to really understand what is going wrong and I followed this article of TypeErasure in Swift Protocols with Associated Type Requirement by Rob Napier but still i have no luck. Find the code below // An Animal can eat protocol Animal { associatedtype Food func feed(food: Food) -> Void } struct AnyAnimal<Food>: Animal { private let _feed: (Food) -> Void init<Base: Animal where

Swift Protocols with Associated Type Requirement and Default Implementation

烂漫一生 提交于 2020-01-01 16:24:09
问题 I have been struggling very hard with Swift Protocols and Associated Types for a long time. I started again with basic to really understand what is going wrong and I followed this article of TypeErasure in Swift Protocols with Associated Type Requirement by Rob Napier but still i have no luck. Find the code below // An Animal can eat protocol Animal { associatedtype Food func feed(food: Food) -> Void } struct AnyAnimal<Food>: Animal { private let _feed: (Food) -> Void init<Base: Animal where

Swift Protocols with Associated Type Requirement and Default Implementation

纵饮孤独 提交于 2020-01-01 16:24:08
问题 I have been struggling very hard with Swift Protocols and Associated Types for a long time. I started again with basic to really understand what is going wrong and I followed this article of TypeErasure in Swift Protocols with Associated Type Requirement by Rob Napier but still i have no luck. Find the code below // An Animal can eat protocol Animal { associatedtype Food func feed(food: Food) -> Void } struct AnyAnimal<Food>: Animal { private let _feed: (Food) -> Void init<Base: Animal where

if-let Any to RawRepresentable<String>

穿精又带淫゛_ 提交于 2019-12-30 11:19:28
问题 Let's assume this: enum MyEnum: String { case value } let possibleEnum: Any = MyEnum.value if let str = stringFromPossibleEnum(possibleEnum: possibleEnum) What's my best bet of implementing stringFromPossibleEnum without knowing enum type name? func stringFromPossibleEnum(possibleEnum: Any) -> String? { // how should this be implemented without knowing enum type name? } UPD: ok, it's getting better, with this I can tell if possibleEnum is an enum: if Mirror(reflecting: possibleEnum)

Value of type NSObject -> () -> **ViewController does not conform to specified type ***Delegate

核能气质少年 提交于 2019-12-25 17:07:11
问题 I am receiving the error below: My protocol looks like this: protocol RecorderDelegate { func finishedRecordingWithUrl(URL: NSURL) } Can someone explain why self is NOT conforming to the protocol when it appears that it is? 回答1: At the time you are setting self as reference, self does not exist. Try to set it later, let say viewDidLoad or make it lazy. Btw make the reference on delegate weak, otherwise you are creating reference cycle and your view controller instance is never going to

Protocol doesn't conform to itself?

做~自己de王妃 提交于 2019-12-25 10:58:12
问题 Why doesn't this Swift code compile? protocol P { } struct S: P { } let arr:[P] = [ S() ] extension Array where Element : P { func test<T>() -> [T] { return [] } } let result : [S] = arr.test() The compiler says: "Type P does not conform to protocol P " (or, in later versions of Swift, "Using 'P' as a concrete type conforming to protocol 'P' is not supported."). Why not? This feels like a hole in the language, somehow. I realize that the problem stems from declaring the array arr as an array

Protocol doesn't conform to itself?

ε祈祈猫儿з 提交于 2019-12-25 10:57:10
问题 Why doesn't this Swift code compile? protocol P { } struct S: P { } let arr:[P] = [ S() ] extension Array where Element : P { func test<T>() -> [T] { return [] } } let result : [S] = arr.test() The compiler says: "Type P does not conform to protocol P " (or, in later versions of Swift, "Using 'P' as a concrete type conforming to protocol 'P' is not supported."). Why not? This feels like a hole in the language, somehow. I realize that the problem stems from declaring the array arr as an array

Why can't I assign this protocol conforming class to a variable of the protocol type?

China☆狼群 提交于 2019-12-25 01:15:34
问题 I've got a toy example here that I can't find any solutions for online. protocol Tree { associatedtype Element // some nice tree functions } class BinaryTree<T> : Tree { typealias Element = T } class RedBlackTree<T> : Tree { typealias Element = T } enum TreeType { case binaryTree case redBlackTree } // Use a generic `F` because Tree has an associated type and it can no // longer be referenced directly as a type. This is probably the source // of the confusion. class TreeManager<E, F:Tree>

How do I expose a private class method of an Objective-C object using protocols in Swift?

偶尔善良 提交于 2019-12-23 10:52:09
问题 Consider two private methods on UIColor : The instance method styleString which returns the RGB string of the color The class method _systemDestructiveTintColor which returns the red color used by destructive buttons. UIColor.h private header for reference For instance methods, I can create an @objc protocol and use unsafeBitCast to expose the private method: @objc protocol UIColorPrivate { func styleString() -> UIColor } let white = UIColor.whiteColor() let whitePrivate = unsafeBitCast(white

protocol extension, does not conform to protocol

独自空忆成欢 提交于 2019-12-23 09:59:58
问题 I am creating a framework named MyFramework containing LoginProtocol.swift which has some default behaviours import UIKit public protocol LoginProtocol { func appBannerImage() -> UIImage? func appLogoImage() -> UIImage? } extension LoginProtocol { func appBannerImage() -> UIImage? { return (UIImage(named: "login_new_top")) } func appLogoImage() -> UIImage? { return (UIImage(named: "appLogo")) } } Next, I am adding a new target to create a demo application named MyDemoApp which is using