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
2
votes
3 answers

When should I use conformsToProtocol and respondsToSelector methods?

What is the use of conformsToProtocol and respondsToSelector method?
Mayank
  • 643
  • 8
  • 19
2
votes
0 answers

Should a replacement for a deprecated delegate method fall through to the old version?

I have a proper delegate protocol created in Objective-C and I'm updating it. One of the updates requires a method be deprecated, but it is technically still valid for the time being. Here is the original delegate method. Notice it's optional, so…
2
votes
3 answers

What are the key reasons for using @protocols in Objective C?

Why would I want to use a protocol rather than create a subclass and inherit the methods..? Please explain it to me, i'm to confused about this topic, i'm not very pleased with the explanation in the book im reading. Where do I use protocols…
MNY
  • 1,506
  • 5
  • 21
  • 34
2
votes
2 answers

Is there a way to only allow UIViewControllers to adhere to a protocol?

Is there any way to do something like this (below doesn't work): @protocol ElementPicker - (id)initWithFile:(NSFileWrapper *)file andInfo:(NSString *)info; @property (nonatomic, weak) NSObject
Luke
  • 13,678
  • 7
  • 45
  • 79
2
votes
3 answers

Porting @optional selectors in protocols to Java interfaces

So, let's say you have this: if ([foo respondsToSelector:@selector(bar:)]) { [foo bar:abc]; } else { [qux mux:abc]; } And both bar: and mux: have side effects. How would you port that to Java, where there is no such things as @optional…
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
1
vote
1 answer

Throwing method cannot be an implementation of an @objc requirement because it returns a value of type `Bool`

I have IKabaSDK.h which is a (Objective-C) protocol : @import Foundation; @import MobileSdk; NS_ASSUME_NONNULL_BEGIN @protocol IKabaSDK - (BOOL)isStarted:(NSError* _Nullable __autoreleasing *…
1
vote
2 answers

Getting protocol to an object via functions parameters

Let's say we have following C++ code: struct ISomeInterface { virtual ~ISomeInterface() {} virtual void f() = 0; }; class SomeClass : public ISomeInterface { public: void f() override { std::cout << "Hi"; } }; void…
Andrey Lyubimov
  • 663
  • 6
  • 22
1
vote
1 answer

Clarification needed about the rules of properties declared in protocols

I have a few questions about properties declared in protocols. Return type variance @protocol IHaveProperties @required @property (nonatomic, strong) IAmOfTypeX *propertyOfProtocolType; @property (nonatomic, strong) NSArray…
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126
1
vote
0 answers

What view do I return for NSTextFinderClient's contentViewAtIndex:effectiveCharacterRange protocol method?

I've implemented an object that conforms to the NSTextFinderClient protocol. The textFinder's find bar container is a WebView's WebDynamicScrollBarsView, which is an NSScrollView. When I show the Find Bar in my WebView it calls the…
JuJoDi
  • 14,627
  • 23
  • 80
  • 126
1
vote
1 answer

Find Bar does not show up with WebView's Scroll View as the findBarContainer

I have a TextFinderController object that implements the NSTextFinderClient protocol, and a WebView's WebDynamicScrollBarsView (which is an NSScrollView) set as the NSTextFinder's findBarContainer. When I try to make the findBar visible in the…
JuJoDi
  • 14,627
  • 23
  • 80
  • 126
1
vote
4 answers

Defining protocols without parameters

I am trying to define a protocol method without adding parameters but couldn't find the correct syntax. Here is the definition (it has a syntax error) - (void)cameraOverlayView:(CameraOverlayView *)cameraOverlay didTakePhoto; I don't want to pass…
Élodie Petit
  • 5,774
  • 6
  • 50
  • 88
0
votes
3 answers

How to define a `Class` object type conforming to a protocol?

Consider the following Objective-C protocol declaration, which requires only class methods: @protocol TDWMethoding + (void)foo; + (void)bar; @end Assuming I need to return an instance of a Class which conforms to this protocol from a…
0
votes
1 answer

How to implement an objective-C delegate function of an objective-C class in a swift extension

I'm trying to conform to an Objective-C delegate method on an Objective-C class via a swift extension on said class. The problem is I am given no autocomplete options when defining the body for this function in the swift extension below. I had to…
0
votes
0 answers

Declare protocol conformance in objective-c protocol forward-declaration

Assume a class has been defined like this in OriginalObject.h: @protocol OriginalDelegate; @interface OriginalObject : NSObject {} @property (nullable, nonatomic, weak) id delegate; @end @protocol OriginalDelegate //…
George WS
  • 3,903
  • 5
  • 27
  • 39
0
votes
1 answer

Any way to get a compiler error/warning when a function is being deleted from a protocol?

@protocol MyProtocol - (void)foo; - (void)bar; @end @interface MyClass : NSObject < MyProtocol > @end @implementation MyClass // My Protocol implementation - (void)foo { NSLog(@"foo implementation."); } - (void)bar { NSLog(@"foo…
Sanich
  • 1,739
  • 6
  • 25
  • 43