Quang Dam
Quang Dam

Reputation: 325

Using weak property not like a protocol

As you know, when we apply a protocol which must declare a delegate.

@property (weak) id<NameOfProtocol> delegate;

But, Xcode shows the fault message when I declare like this (not use protocol)

@property (weak) id<NameOfObject> pointer;

What's wrong with this?

Upvotes: 0

Views: 93

Answers (2)

Daniel
Daniel

Reputation: 83

@property (weak) id<NameOfProtocol> delegate; means the variable can be any type if it conforms to NameOfProtocol

@property (weak) id<NameOfObject> pointer; means the variable can be any type but it type must be NameOfObject

so it have no sense;

instead of @property (weak) NameOfObject *pointer;

Upvotes: 1

Fonix
Fonix

Reputation: 11597

So when you go

@property (weak) id<NameOfProtocol> delegate;

you are saying i want a variable that points some type that conforms to NameOfProtocol. you need it to be a id type because you dont actually know what the variables type is going to be (and you dont care so long as it implements the methods from NameOfProtocol)

while

@property (weak) id<NameOfObject> pointer;

doesnt make sense because you are saying i want a variable, but i dont know the type therefore i need id but it conforms to NameOfObject... which is contradictory since you have the type

therefore you should have just a normal weak variable in this case

@property (weak) NameOfObject *pointer;

Upvotes: 2

Related Questions