为什么我无法调用这个方法?

发布于 2025-01-04 05:33:06 字数 345 浏览 2 评论 0原文

我这里有一个方法签名:

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别低头,皇冠会掉 2025-01-11 05:33:06

当你执行[array somemethod]时..它将在该数组数据类型类中查找方法(即在NSMutableArray类中。

当你自己有一个方法时class.并且它不是一个类别...那么您必须以

[self somemethod]

这种方式调用它,它将在您调用该方法的类中查找。

when you do [array somemethod] ..it will look for the method in that array data type class( that is in NSMutableArray class.

when you have a method in your own class.and it is not a category...then you must call

[self somemethod]

this way it will look in the class that is in which you are calling the method.

甜尕妞 2025-01-11 05:33:06

更改为:

.h

-(void)addObjectsToArray:(NSMutableArray*)a;

.m

- (void)viewDidLoad {
     NSMutableArray * array = [[NSMutableArray alloc]init];
     [self addObjectsToArray:array];
}
- (void)addObjectsToArray:(NSMutableArray*)a {
     //some code here…
     [a addObject:[NSString stringWithFormat:@"Example String Object"]]; // example way to add objects to the array
}

Change to:

.h

-(void)addObjectsToArray:(NSMutableArray*)a;

.m

- (void)viewDidLoad {
     NSMutableArray * array = [[NSMutableArray alloc]init];
     [self addObjectsToArray:array];
}
- (void)addObjectsToArray:(NSMutableArray*)a {
     //some code here…
     [a addObject:[NSString stringWithFormat:@"Example String Object"]]; // example way to add objects to the array
}
殊姿 2025-01-11 05:33:06

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

你是年少的欢喜 2025-01-11 05:33:06

在您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文