I need to call a method through the object created by the interface ie
我需要通过接口创建的对象调用方法,即
id<MyProtocol>obj;
Now i have created this obj in my main class where i am not implementing the methods of this protocol but i need to access the method which is already implemented in someother class.I am now calling the method as follows
现在我已经在我的主类中创建了这个obj,我没有实现这个协议的方法,但是我需要访问已经在其他类中实现的方法。我现在调用的方法如下
[obj load];
in my main class in the applicationDidFinishLaunching, but i not able to access the method? Pls do suggest me the correct way of calling the methods through protocols...
在我的主要类applicationDidFinishLaunching中,但我无法访问该方法?请告诉我通过协议调用方法的正确方法...
2 个解决方案
#1
2
A protocol implements nothing. It only describes a set of messages that the object should respond to. Your obj
object belongs to some class. This class needs to implement methods described in MyProtocol
.
协议什么都不实现。它仅描述了对象应响应的一组消息。你的obj对象属于某个类。该类需要实现MyProtocol中描述的方法。
Edit
A protocol is not implemented by a specific class. Any class that claims to conform to a protocol must implement its methods. Any object that claims to conform to a protocol must belong to a class that implements its methods.
协议不是由特定类实现的。声称符合协议的任何类都必须实现其方法。声称符合协议的任何对象必须属于实现其方法的类。
In your case, obj
is a ClassB
, so ClassB
must implement methods described by MyProtocol
, either directly or through inheritance.
在您的情况下,obj是ClassB,因此ClassB必须直接或通过继承实现MyProtocol描述的方法。
#2
0
[obj load]
is OK. If you want to shut up the compiler you can cast it to id
:
[obj load]没问题。如果要关闭编译器,可以将其转换为id:
[(id)obj load];
But if you know you'll need to call the -load
method, maybe you should add the -load
method to the protocol, or make another protocol that has the -load
method e.g.
但是如果你知道你需要调用-load方法,也许你应该将-load方法添加到协议中,或者制作另一个具有-load方法的协议,例如:
@protocol Loadable
-(void)load;
@end
...
id<MyProtocol, Loadable> obj;
#1
2
A protocol implements nothing. It only describes a set of messages that the object should respond to. Your obj
object belongs to some class. This class needs to implement methods described in MyProtocol
.
协议什么都不实现。它仅描述了对象应响应的一组消息。你的obj对象属于某个类。该类需要实现MyProtocol中描述的方法。
Edit
A protocol is not implemented by a specific class. Any class that claims to conform to a protocol must implement its methods. Any object that claims to conform to a protocol must belong to a class that implements its methods.
协议不是由特定类实现的。声称符合协议的任何类都必须实现其方法。声称符合协议的任何对象必须属于实现其方法的类。
In your case, obj
is a ClassB
, so ClassB
must implement methods described by MyProtocol
, either directly or through inheritance.
在您的情况下,obj是ClassB,因此ClassB必须直接或通过继承实现MyProtocol描述的方法。
#2
0
[obj load]
is OK. If you want to shut up the compiler you can cast it to id
:
[obj load]没问题。如果要关闭编译器,可以将其转换为id:
[(id)obj load];
But if you know you'll need to call the -load
method, maybe you should add the -load
method to the protocol, or make another protocol that has the -load
method e.g.
但是如果你知道你需要调用-load方法,也许你应该将-load方法添加到协议中,或者制作另一个具有-load方法的协议,例如:
@protocol Loadable
-(void)load;
@end
...
id<MyProtocol, Loadable> obj;