0

I am new to ObjectiveC and have been working for few years in Swift. Therefore, I don't understand the below explained error in Xcode:

Type arguments cannot be applied to non-class type 'id'

My Protocol:

@protocol ExampleProtocol<NSObject>
@required
        
-(NSString *)title;
-(NSString *)album;
    
@end

My implementation in the MyService.h file:

@interface MyService : NSObject 
@property (nonatomic, assign, readwrite) id<ExampleProtocol> delegate;
@end

The error occurs in the line:

> @property (nonatomic, assign, readwrite) id<ExampleProtocol> delegate;

Additionally:

  • I have imported the required file .h in which the ExampleProtocol code is located in the MyService.m file
  • Added the @class ExampleProtocol; in my MyService.h file at the top.

Also tried:

  • Creating a Swift protocol with @objc and : class imported over the app-Bridging.h gives me the same result with the same error message.

  • Clean build

  • Clean build folder (removed derived data)

    The only thing that did work was to remove the line from the public interface to the private. This doesn't make sense. I wan't to set the delegate from another class and creating a public setter which set the private delegate is ugly workaround.

Any suggestion would be helpfull. I would like to understand why this happens. There are a lot of other protocols in my project written in ObjectiveC which work fine.

Community
  • 1
  • 1
Darkwonder
  • 1,149
  • 1
  • 13
  • 26
  • 2
    Did you try without `@class ExampleProtocol;`? `ExampleProtocol` is a protocol, not a class. You don't need it if you import the header but if you don't import the header, it should be `@protocol ExampleProtocol;` – Willeke Dec 26 '17 at 10:33
  • Your solution is the right answer. Please post it as answer so I can accept and upvote it. – Darkwonder Dec 27 '17 at 16:51

1 Answers1

3

ExampleProtocol is a protocol, not a class. You don't need it if you import the header. If you don't import the header, it should be @protocol ExampleProtocol;

Willeke
  • 14,578
  • 4
  • 19
  • 47