繁体   English   中英

我的iPhone应用程序崩溃并显示[__NSCFDictionary stringByReplacingOccurrencesOfString:withString:]:无法识别的选择器已发送到实例

[英]My iPhone app is crashing and showing [__NSCFDictionary stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance

我的iPhone应用程序崩溃并显示错误:

- [__NSCFDictionary stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x99f60b0

这是我的代码NSString * strData; AppDelegate * delegate =(AppDelegate *)[[UIApplication sharedApplication]委托]; 如果(delegate.respondInsightIndex){strData = [insights objectAtIndex:index]; } else {strData = [insights lastObject];
}

NSString *strReplace = [NSString stringWithFormat:@"%@",[strData stringByReplacingOccurrencesOfString:@"%20" withString:@" "]];

这是我的方法,崩溃再次发生

- (void) setComments:(NSArray*)comments {
NSString * htmlString; 
if ( [UIHelper isPad] )
    htmlString = @"<html><style type=\"text/css\">body{padding:15px 40px 0 40px; font-family: Helvetica;"
                "font-size: 14px;"
                "color: #888888;"
                "line-height: 50%"
                "},</style><body>";
else
    htmlString = @"<html><style type=\"text/css\">body{padding:8px 20px 0 20px; font-family: Helvetica;"
                "font-size: 11px;"
                "color: #888888;"
                "},</style><body>";

for ( NSString * single in comments ) {
    if ( single == nil ) 
        single = @"";

    if ([single isKindOfClass:[NSString class]]) {
        single = [single stringByReplacingOccurrencesOfString:@"%20" withString:@" "];  
    }
    NSString * str ;
    if([UIHelper isPad ])
    {
        str = [NSString stringWithFormat:@"%@<br/><hr color=#555555><p align=\"right\" style=\"font-size:12px;\">Username</p>", single];
    }
    else {
        str = [NSString stringWithFormat:@"%@<br/><hr color=#555555><p align=\"right\" style=\"font-size:9px;\">Username</p>", single];
    }

    htmlString = [NSString stringWithFormat:@"%@%@", htmlString, str];
}
htmlString = [NSString stringWithFormat:@"%@%@", htmlString, @"</body></html>"];
if ( [contentView respondsToSelector:@selector(loadHTMLString:baseURL:)] )
    [contentView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://www.secondprizm.com"]];  

}

因此,这意味着您尝试在NSDictionary实例上使用方法- stringByReplacingOccurrencesOfString ,这当然是行不通的。

对象strData不是您认为的NSString对象。 它实际上是NSDictionary的对象。

如Peter上面所述,当您从NSArray检索对象时,它只是指向对象的指针,无论您将其转换为什么对象。 您可以通过多种方式对此进行调查。 查看数组内容的设置位置,然后检查要添加的内容,则可能会将NSDictionary错误地NSDictionary到整个链的NSDictionary 还可以在读取数组之前尝试设置断点,然后在调试器中输入po insights 这将打印对象的内容。 崩溃之后,您还可以通过执行po <memory address> (在本例中为po 0x99f60b0来调查导致问题的对象。

内省(尽管如果您在数组中有错误的对象,但这不会解决问题,但可以防止崩溃),这是防止此类情况发生的一种好方法:

if ([strData isKindOfClass:[NSString class]]) {
    NSString *strReplace = [NSString stringWithFormat:@"%@",[strData stringByReplacingOccurrencesOfString:@"%20" withString:@" "]];   
}

尽管我注意到了两点,但我看不到您发布的代码中的任何错误。 您无需检查single == nil NSArray不能为nil

另外,您正在做很多指针的重新分配,并且还试图在遍历数组时编辑数组的内容。 在Objective-C中,这不是一个好主意。 我已经对其进行了测试,并且在这种情况下似乎可以正常工作,但是如果要创建新的字符串,通常最好创建一个新的字符串指针,以确保正确释放旧字符串使用的内存。 别误会我的代码可以工作,但是这种事情很快就会导致内存泄漏,后来很难跟踪,这只是最佳实践。

关于崩溃,我看不到该方法生成的错误消息,您发布的原始代码在哪里适合您发布的额外代码? 请详细更新您的问题

暂无
暂无

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

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