Questions tagged [objective-c-protocol]

Protocols declare methods that can be implemented by any class. They can be used to force the implementation of necessarily used methods to assure the proper execution.

Protocols declare methods that can be implemented by any class. Protocols are useful in at least three situations:

  • To declare methods that others are expected to implement
  • To declare the interface to an object while concealing its class
  • To capture similarities among classes that are not hierarchically related

Protocols can be made optional specifically, otherwise the implementation is required for compiling successfully.

Developer Documentation for further information and usage

48 questions
39
votes
7 answers

Protocol versus Category

Can anyone explain the differences between Protocols and Categories in Objective-C? When do you use one over the other?
Coocoo4Cocoa
  • 48,756
  • 50
  • 150
  • 175
25
votes
1 answer

Type "SwiftClass" cannot conform to protocol "ObjcProtocol" because it has requirements that cannot be satisfied

I have an Objective-C protocol which I'm trying to implement in a Swift class. For example: @class AnObjcClass; @protocol ObjcProtocol - (void)somethingWithAnArgument:(AnObjcClass *)arg; @end When I try to conform to it in a Swift…
Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
11
votes
1 answer

How to create class methods that conform to a protocol shared between Swift and Objective-C?

I've been learning Swift lately. I decided to write a hybrid Swift/Objective-C app that did compute-intensive tasks using the same algorithm implemented in both languages. The program calculates a large array of prime numbers. I defined a protocol…
Duncan C
  • 128,072
  • 22
  • 173
  • 272
10
votes
4 answers

Objective c protocol generics

Can Objective-C protocol be generic? Following this tutorial, I'm basically looking for something like that: @protocol ItemsStore<__covariant ObjectType> -(NSArray *)items; @end Which is a generic protocol for some…
mllm
  • 17,068
  • 15
  • 53
  • 64
9
votes
2 answers

Creating a category for classes that implement a specific protocol in Objective-C?

Short problem description Can I extend UIView with a category, but have it only work on subclasses that implement a specific protocol (WritableView)? I.e. can I do something like the following? @interface UIView (foo) // SYNTAX ERROR -…
Senseful
  • 86,719
  • 67
  • 308
  • 465
8
votes
5 answers

Using @class to get access to a delegate protocol declaration

I've read that you should try to use @class in your header file instead of #import but this doesn't work when your @class contains a delegate protocol that you're trying to use. MyView.h #import @class MyCustomClass; // <-- doesn't…
8
votes
3 answers

Is there any introspection method to get all adopted protocols for a class in Objective-C?

There is an -[NSObject conformsToProtocol:] method to check whether a specific protocol is adopted or not. Is there any method to get all adopted protocols for a class, rather than checking a list?
5
votes
3 answers

What does a variable defined as `Class myClass` mean?

I am used to seeing things like id myVar or MyObject myVar, where we are stating that the variable in question can happily have NSCopying methods called on it without the compiler throwing a wobbly. But I recently spotted some…
Barjavel
  • 1,626
  • 3
  • 19
  • 31
4
votes
2 answers

Replacement of @protocol() in swift

I am trying to use NSXPCConnection in swift. So, this line: _connectionToService = [[NSXPCConnection alloc] initWithServiceName:@"SampleXPC"]; can be replaced by this line: _connectionToService = NSXPCConnection(serviceName: "SampleXPC") And,…
Devarshi
  • 16,440
  • 13
  • 72
  • 125
4
votes
1 answer

Defining a protocol to require one method only if another is implemented

I've got a fairly complicated protocol which I'm adding methods to. Most of the new methods are @optional, but they are paired. For example, these two methods work together: @optional - (BOOL) shouldIDoSomethingHere; - (CGPoint)…
Ryan Dignard
  • 639
  • 1
  • 5
  • 16
3
votes
2 answers

Why do properties modified by @optional become immutable?

I have an Objective-C protocol that contains a property as follows: #import @protocol Playback @optional @property (nonatomic, nonnull) NSURL *assetURL; @end PlayerController has a property of type…
3
votes
3 answers

What is the purpose of protocols if all methods are optional?

I understand what purpose protocols serve (to have a type conform to a set list of methods or/and properties), but I don't understand what the purpose is of a protocol with all optional methods. One example would be UITextFieldDelegate. If all…
3
votes
2 answers

Is it possible to define a property with Class type that conforms to protocol?

For example, I have MyFancyData protocol. How can I specify that MyFancyDataClass property accepts only classes that conforms to this protocol. @interface MyObject : NSObject @property Class MyFancyDataClass;
user1561346
  • 502
  • 3
  • 13
  • 28
2
votes
0 answers

Swift & Objective C protocols: throwing function declaration

I have a Obj C protocol in a library... @protocol DataWriter - (void) writeData:(NSData*)data; @end ...that I would like to use in Swift 3.1: class Streamer: NSObject, DataWriter { ... // MARK: - DataWriter func write(_…
doubleL
  • 21
  • 3
2
votes
0 answers

Adding optional method of objective-C class to swift extension

To an already extensive Objective-C app I wanted to add Core Data functionality. Recent additions are in Swift, and this worked well, up until now. Because my Objective-C AppDelegate already contains quite some stuff, I decided to write a Swift…
1
2 3 4