为什么我无法调用这个方法?
我这里有一个方法签名:
.h
-(void)addToArray
我想在这里使用它:
.m
-(void)viewDidLoad{
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addToArray];
}
-(void)addToArray{
//some code here…
}
问题是当我执行 [array addToArray]; 时它返回给我一条消息,内容为“找不到实例方法'-addToArray'(返回类型默认为'id')” 我该如何解决?
I have a method signature here:
.h
-(void)addToArray
And I want to use it here:
.m
-(void)viewDidLoad{
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addToArray];
}
-(void)addToArray{
//some code here…
}
The problem is that when I do [array addToArray]; it returns to me a message that says "Instance method '-addToArray' not found (return type defaults to 'id')"
How can I solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当你执行
[array somemethod]
时..它将在该数组数据类型类中查找方法(即在NSMutableArray
类中。当你自己有一个方法时class.并且它不是一个类别...那么您必须以
这种方式调用它,它将在您调用该方法的类中查找。
when you do
[array somemethod]
..it will look for the method in that array data type class( that is inNSMutableArray
class.when you have a method in your own class.and it is not a category...then you must call
this way it will look in the class that is in which you are calling the method.
更改为:
.h
.m
Change to:
.h
.m
Shubhank 的答案确实是正确的,但是如果您想完成最初描述的内容,即让 NSMutableArray 实例响应您的方法,您可以通过使用类别来实现。
https://developer.apple.com/ library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html
此博客详细介绍了向 NSArray 和/或 NSMutableArray 添加类别。
http://it.toolbox.com/blogs /macsploitation/extending-classes-in-objectivec-with-categories-27447
Shubhank's answer is indeed correct, however if you want to accomplish what you'd initially described, that is have the have the NSMutableArray instance respond to your method, you can achieve that through the use of categories.
https://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html
This blog details adding a category to NSArray and/or NSMutableArray.
http://it.toolbox.com/blogs/macsploitation/extending-classes-in-objectivec-with-categories-27447
在您的 m 文件中,尝试执行以下操作:#import "whatever it called.h"(如果还没有)。如果您提供更多代码我可以提供更多帮助,但我希望这会有所帮助。
In your m file, try doing this: #import "whatever its called.h" if you haven't yet. If you provide more code I can give more help, but I hope this helps.