Questions tagged [protocols]

A common means for (un)related objects to communicate with each other.

5592 questions
94
votes
1 answer

Can a category implement a protocol in Objective C?

I have a category on NSDate and it would be convenient if it could implement a protocol I previously created. Is this possible? what's the correct syntax for this?
cfischer
  • 24,452
  • 37
  • 131
  • 214
94
votes
5 answers

What is SOCK_DGRAM and SOCK_STREAM?

I just came across this strange thing I got to see application is that by default they use SOCK_STREAM function. Why is it so? Is this SOCK_STREAM just creating multiple streams? Or is it the standard SOCK_STREAM function available for creating…
echo9
  • 1,159
  • 1
  • 13
  • 15
93
votes
13 answers

How to detect browser's protocol handlers?

I have created a custom URL protocol handler. http:// mailto:// custom:// I have registered a WinForms application to respond accordingly. This all works great. But I would like to be able to gracefully handle the case where the user doesn't have…
Chris Craft
  • 5,285
  • 6
  • 46
  • 63
92
votes
13 answers

How can I send and receive WebSocket messages on the server side?

How can I send and receive messages on the server side using WebSocket, as per the protocol? Why do I get seemingly random bytes at the server when I send data from the browser to the server? It the data encoded somehow? How does the framing work…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
92
votes
6 answers

Calling protocol default implementation from regular method

I'm wondering if it's possible to achieve such a thing. I have a Playground like this: protocol Foo { func testPrint() } extension Foo { func testPrint() { print("Protocol extension call") } } struct Bar: Foo { func…
cojoj
  • 6,405
  • 4
  • 30
  • 52
90
votes
9 answers

Protocol func returning Self

I have a protocol P that returns a copy of the object: protocol P { func copy() -> Self } and a class C that implements P: class C : P { func copy() -> Self { return C() } } However, whether I put the return value as Self I get…
aeubanks
  • 1,261
  • 1
  • 12
  • 12
89
votes
4 answers

Why I can't use let in protocol in Swift?

I have a doubt about protocols in Swift about the use of var and the keywords { get set }. From Apple documentation: If a protocol requires a property to be gettable and settable, that property requirement cannot be fulfilled by a constant stored…
Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54
81
votes
7 answers

What's the difference between a protocol extended from AnyObject and a class-only protocol?

Both this declaration protocol SomeProtocol : AnyObject { } and this declaration protocol SomeProtocol : class { } seem to make it so that only classes can conform to this protocol (i.e. that the instances of the protocol are references to…
user102008
  • 30,736
  • 10
  • 83
  • 104
81
votes
2 answers

Can I have an init func in a protocol?

When I try to implement my protocol this way: protocol Serialization { func init(key keyValue: String, jsonValue: String) } I get an error saying: Expected identifier in function declaration. Why am I getting this error?
Aaron Bratcher
  • 6,051
  • 2
  • 39
  • 70
78
votes
3 answers

What is the technology behind wechat, whatsapp and other messenger apps?

I am eager to know about the architecture of different real-time messenger apps. Are they using any generic protocol/architecture?
viswas
  • 1,007
  • 1
  • 9
  • 13
76
votes
8 answers

Implementing Bittorrent Protocol

I am looking for a tutorial/blog post on how to implement bittorrent protocol step by step. How it works? How do you make requests to peers? and talk to trackers. I do not mind the programming language (java,ruby,perl,c#)
Hamza Yerlikaya
  • 49,047
  • 44
  • 147
  • 241
76
votes
10 answers

Swift Equatable on a protocol

I don't think this can be done but I'll ask anyway. I have a protocol: protocol X {} And a class: class Y:X {} In the rest of my code I refer to everything using the protocol X. In that code I would like to be able to do something like: let a:X =…
drekka
  • 20,957
  • 14
  • 79
  • 135
70
votes
4 answers

How to define initializers in a protocol extension?

protocol Car { var wheels : Int { get set} init(wheels: Int) } extension Car { init(wheels: Int) { self.wheels = wheels } } on self.wheels = wheels i get the error Error: variable 'self' passed by reference before being…
bogen
  • 9,954
  • 9
  • 50
  • 89
70
votes
6 answers

NSNotificationCenter vs delegation( using protocols )?

What are the pros and cons of each of them? Where should I use them specifically?
EEE
  • 4,536
  • 3
  • 28
  • 34
69
votes
6 answers

How to define a protocol as a type for a @ObservedObject property?

I have a swiftui view that depends on a view model, the view model has some published properties. I want define a protocol and default implementation for the view model hierarchy, and make the view dependent on the protocol not the concrete class? I…
M.Serag
  • 1,381
  • 1
  • 11
  • 15