protocols

Always the default implementation of protocol gets called even after implementing the method in class extension in an XCTest file

十年热恋 提交于 2019-12-11 03:05:58
问题 I have a protocol protocol SomeProtocol { func method() } and its implementation extension SomeProtocol {func method(){--implementation--}} In build target i have a class confirming to this protocol class SomeClass: SomeProtocol { func doSomething() { method() } } What i want is i want to have a different implementation of the protocol method in my test target, in my XCTest file. For that what i did was i extended the SomeClass and wrote my implementation there. extension SomeClass {func

What's the need of Informal Protocols?

China☆狼群 提交于 2019-12-11 02:38:38
问题 I've read the online docs about formal and informal protocols on Apple's documentation site, but I missed the point about informal protocols. I mean, a class cannot conform to an informal protocol, it conforms to it by default since informal protocols are almost always categories of the NSObject class. If you want to implement an informal protocol, you must redeclare the methods you want to implement in your interface file. Another class cannot check if you conform to the informal protocol

Do swift hashable protocol hash functions need to return unique values?

帅比萌擦擦* 提交于 2019-12-11 02:12:55
问题 I am working through an iOS swift Tetris tutorial* and have it completed and working. But I am puzzled about one particular aspect - the Hashable protocol. The function: class Block: Hashable, Printable { [...] var hashValue: Int { return self.column ^ self.row } Rows go 0..9, and Columns 0..20. The notes says of this function "We return the exclusive-or of our row and column properties to generate a unique integer for each Block.". But my understanding is that 0^1 would be the same as 1^0,

Swift struct adopting protocol with static read-write property doesn't conform?

末鹿安然 提交于 2019-12-11 01:59:13
问题 Why doesn't this compile in Swift 1.2? protocol Proto { static var name : String {get set} } struct Struct : Proto { static var name : String = "name" } (In Swift 1.1, just substitute class for static inside the protocol declaration. Same problem.) The compiler complains that I'm not conforming to the protocol. But why am I not? It's easy to prove that the static property name in Struct is both readable and writable, so I've satisfied the spirit of the protocol, surely. I have some additional

Delegation between two container views

主宰稳场 提交于 2019-12-11 01:57:59
问题 I'm trying to send information between two Container Views in Swift , through a delegate and I keep getting unwrapping error when performing the protocol function. my story board layout topContainerViewController.swift import UIKit protocol topContainerDelegate{ func send(text:String) } class topContainerViewController: UIViewController { var delegate: topContainerDelegate! = nil @IBOutlet var textField: UITextField! override func viewDidLoad() { super.viewDidLoad() } @IBAction func

Messaging Protocol

笑着哭i 提交于 2019-12-11 01:23:33
问题 How should I design my message protocol so that I distinguish between user data and termination of the message. For example I take input from user to send it to client over TCP/IP, now the client-side needs to know the length of message, what I thought that I use some kind of termination by which the client recognizes that message end has been reached? How do I something like this in C++. I'm using Windows sockets 回答1: A reasonably common strategy for application-specific protocols is,

Storing objects conforming to a protocol with generics in a typed array

余生颓废 提交于 2019-12-10 23:55:35
问题 I've got a protocol: protocol Adjustable: Equatable { associatedtype T var id: String { get set } var value: T { get set } init(id: String, value: T) } And a struct that conforms to it: struct Adjustment: Adjustable { static func == (lhs: Adjustment, rhs: Adjustment) -> Bool { return lhs.id == rhs.id } typealias T = CGFloat var id: String var value: T } And I'm building a wrapper class that behaves like a Set to handle an ordered list of these properties: struct AdjustmentSet { var

Is there a way to make a function to accept any Enum types that have a rawValue of String?

Deadly 提交于 2019-12-10 23:34:37
问题 One way I came up with is to make a protocol that other Enum must conform to. protocol StringRepresentable { var rawValue: String { get } } struct Endpoint { enum User: String, StringRepresentable { case Login = "/user/login" case Register = "/user/register" } enum Item: String, StringRepresentable { case Like = "/item/like" case Buy = "/item/buy" } } func urlString(endpoint: StringRepresentable) -> String { return "http://www.example.com\(endpoint.rawValue)" } let userLoginEndpoint =

How to set delegate in a protocol extension

我只是一个虾纸丫 提交于 2019-12-10 23:07:06
问题 I have multiple view controllers which shows same kind of cells. I want to set delegate in a protocol extension like this: class ProductsViewController: UIViewController, ProductShowcase { //other properties @IBOutlet weak var productCollectionView: UICollectionView! var dataSource: DataSource! override func viewDidLoad() { super.viewDidLoad() setupDataSource() setupCollectionView() } func didSelectProduct(product: Product) { print(product) } //other functions } protocol ProductShowcase:

Why is my delegate method not being called?

僤鯓⒐⒋嵵緔 提交于 2019-12-10 21:28:56
问题 In this code I'm attempting to pass an array from one tab view to another using protocols. The method itself is a simple get method with one line returning an a mutableArray. Within it's own class it works, within this class it is not even called. - (void)viewDidLoad { [super viewDidLoad]; myLocationEntityArray = [[NSMutableArray alloc] initWithArray:[[self delegate] getMyLocationEntityArray]]; } The header file for the class receiving the data: #import <UIKit/UIKit.h> #import <MapKit/MapKit