简体   繁体   English

引发iOS异常:无法识别的选择器已发送到实例

[英]iOS Exception was thrown: unrecognized selector sent to instance

I have a problem. 我有个问题。 I am calling a method in another class. 我在另一个类中调用一个方法。 I call it before and it works perfect but I call back in another method near the end of the class and I have this error: 我之前调用过它,并且效果很好,但是在类的结尾附近又调用了另一个方法,但出现了这个错误:

-(void)methodOne:(NSString*)myString
{
    mySecondClasss *second = [[mySecondClasss init] autorelease];
    [second doSomething:myString];
    /*
     more code
     */
}

-(void)methodTwo:(NSString*)myString
{
    mySecondClasss *second = [[mySecondClasss init] autorelease];
    [second doSomething:myString];
    /*
     more code
     */
}

In the second I'm getting this error: 在第二个我得到这个错误:

Exception was thrown: -[mySecondClasss doSomething:]: unrecognized selector sent to instance. 引发了异常:-[mySecondClasss doSomething:]:无法识别的选择器已发送到实例。 I don't understand why works once but not the second time. 我不明白为什么会一次但不能第二次。 Any of you can give me some pointers of how can I fix this? 你们谁能给我一些如何解决此问题的指示?

I'll really appreciate your help. 非常感谢您的帮助。

Since IOS 5 you have ARC (Automatic Reference Counting) which is automatically releasing the object correctly. 从IOS 5开始,您有了ARC(自动参考计数) ,可以自动正确释放对象。 Anyway you are not allocating the memory for the mySecondClasss class. 无论如何,您都没有为mySecondClasss类分配内存。 Not sure why it even worked in the first method. 不知道为什么它甚至可以在第一种方法中工作。

Instead of using 而不是使用

mySecondClasss *second = [[mySecondClasss init] autorelease];

Try using 尝试使用

mySecondClasss *second = [[mySecondClasss alloc] init];

Are you sure that you are sending a string to the function? 您确定要向该函数发送字符串吗? The error implies that the function is not getting a string. 该错误表明该函数未获取字符串。

For example: 例如:

[self methodOne:@"Properly formatted string"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM