swift-protocols

How can I correctly use associatedType in my protocol

最后都变了- 提交于 2020-01-16 02:50:27
问题 I’m trying to come up with an protocol-oriented MVVM for my tableviewcells. I have lots of them. my viewModel protocol PlainTableViewCellModelType { var backgroundColor : UIColor {get} var textColor: UIColor {get} var titleFont : UIFont {get } var accessoryType : UITableViewCellAccessoryType {get} var textLabelNumberOfLines: Int {get} } my view protocol PlainTableViewCellType{ associatedtype viewModel : PlainTableViewCellModelType func setupUI(forViewModel viewModel: viewModel) } my class

Swift - Connect Delegate to Custom Xib Cell

a 夏天 提交于 2020-01-15 05:04:02
问题 [SOLVED] Solution When created a Xib File , I hadn't deleted the start UIView. Whereas I had to delete this view and after add new CollectionViewCell in this xib. Reference: IBAction inside UITableViewCell not called in iOS 9 I use this structure so many times.When I write this delegate with using StoryBoard , it works properly but now it's not. Where is my mistake when use the xib files? print(indexpath) doesn't work! import UIKit class SearchVC: UIViewController { var searchUid:String? var

Make a type itself — not its instances — conform to a protocol

跟風遠走 提交于 2020-01-12 18:51:49
问题 I was wondering if it is possible in Swift to make a type conform to a protocol, so that I can treat the type itself as conforming to a protocol the way one normally treats instances as conforming to a protocol. Example code: protocol P { func f() } class C where C.self: P { // Not actual code static func f() { print("Because C.f: ()->() exists, C.self should satisfy the protocol.") } } 来源: https://stackoverflow.com/questions/42286462/make-a-type-itself-not-its-instances-conform-to-a-protocol

Make a type itself — not its instances — conform to a protocol

假装没事ソ 提交于 2020-01-12 18:51:39
问题 I was wondering if it is possible in Swift to make a type conform to a protocol, so that I can treat the type itself as conforming to a protocol the way one normally treats instances as conforming to a protocol. Example code: protocol P { func f() } class C where C.self: P { // Not actual code static func f() { print("Because C.f: ()->() exists, C.self should satisfy the protocol.") } } 来源: https://stackoverflow.com/questions/42286462/make-a-type-itself-not-its-instances-conform-to-a-protocol

Implementing a function with a default parameter defined in a protocol

拜拜、爱过 提交于 2020-01-09 11:11:06
问题 Swift protocols can provide default implementations for functions and computed properties by adding extensions to them. I've done that plenty of times. It is my understanding that the default implementation is only used as a "fallback" : It's executed when a type conforms to the protocol but doesn't provide its own implementation. At least that's how I read The Swift Programming Language guide: If a conforming type provides its own implementation of a required method or property, that

Implementing a function with a default parameter defined in a protocol

一曲冷凌霜 提交于 2020-01-09 11:09:29
问题 Swift protocols can provide default implementations for functions and computed properties by adding extensions to them. I've done that plenty of times. It is my understanding that the default implementation is only used as a "fallback" : It's executed when a type conforms to the protocol but doesn't provide its own implementation. At least that's how I read The Swift Programming Language guide: If a conforming type provides its own implementation of a required method or property, that

Extension for average to return Double from Numeric generic

喜欢而已 提交于 2020-01-04 05:58:05
问题 Suppose I create a protocol and structure for a Column of homogeneously typed data: protocol Columnizable { associatedtype Item var name: String { get } var values: [Item] { get } } struct Column<T>: Columnizable { var name: String var values = [T]() } I would like to create a Protocol extension that allows Numeric to have an average function that could compute the average of values if the type conforms to Numeric protocol- for instance, namely Double and Int extension Columnizable where Item

Create an array of protocols with constrained associated types

☆樱花仙子☆ 提交于 2020-01-04 00:13:42
问题 This is a basic example of creating an array of protocols with associated types using type erasure: protocol ProtocolA { associatedtype T func doSomething() -> T } struct AnyProtocolA<T>: ProtocolA { private let _doSomething: (() -> T) init<U: ProtocolA>(someProtocolA: U) where U.T == T { _doSomething = someProtocolA.doSomething } func doSomething() -> T { return _doSomething() } } Creating an array of them isn't hard: let x: [AnyProtocolA<Any>] = [] Is there any way way I can create an array

Protocol with associatedtype Protocol for Generic functions

ⅰ亾dé卋堺 提交于 2020-01-03 02:28:26
问题 Is it possible to provide confirming protocol in generic functions of another protocol? I tried make it work like this, but it's impossible, or I made some mistakes. My code: protocol DataModelProtocol { associatedtype ObjectProtocol: Protocol func fetchObjects<T: ObjectProtocol>() -> [T]? func fetch<T: ObjectProtocol>(object: T) -> T? func delete<T: ObjectProtocol>(allObjectOf type: T.Type) func insert<T: ObjectProtocol>(_ object: T) func save<T: ObjectProtocol>(_ object: T) func update<T:

How to check if an variable of any type is an array

会有一股神秘感。 提交于 2020-01-01 19:10:14
问题 I tried to cast a swift protocol array as any array, but failed. protocol SomeProtocol: class{ } class SomeClass: NSObject, SomeProtocol{ } let protocolArray: [SomeProtocol] = [SomeClass()] let value: Any? = protocolArray if let _ = value as? [SomeProtocol]{ print("type check successed") //could enter this line } Above code could work as expected. However, my problem is, I have a lot of protocols, and I don't want to check them one by one. It is not friendly to add new protocol. Is there any