Ios 我的iPhone应用程序正在崩溃,并显示[\uu NSCFDictionary stringByReplacingOccurrencesOfString:with string:]:发送到实例的无法识别的选择器

Ios 我的iPhone应用程序正在崩溃,并显示[\uu NSCFDictionary stringByReplacingOccurrencesOfString:with string:]:发送到实例的无法识别的选择器,ios,string,Ios,String,我的iPhone应用程序正在崩溃并显示错误: -[\uu NSCFDictionary stringByReplacingOccurrencesOfString:with string::]:无法识别的选择器发送到实例0x99f60b0 这是我的密码 NSString*标准数据; AppDelegate*委托=(AppDelegate*)[[UIApplication sharedApplication]委托]; if(委托.响应InsightIndex) { strData=[insights

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

-[\uu NSCFDictionary stringByReplacingOccurrencesOfString:with string::]:无法识别的选择器发送到实例0x99f60b0

这是我的密码 NSString*标准数据; AppDelegate*委托=(AppDelegate*)[[UIApplication sharedApplication]委托]; if(委托.响应InsightIndex) { strData=[insights objectAtIndex:index]; } 其他的 { strData=[insights lastObject];
}

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

- (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"]];  
-(void)setComments:(NSArray*)注释{
NSString*htmlString;
如果([UIHelper isPad])
htmlString=@“正文{填充:15px 40px 0 40px;字体系列:Helvetica
“字体大小:14px;”
“颜色:#888888;”
“线条高度:50%。”
"},";
其他的
htmlString=@“正文{填充:8px 20px 0 20px;字体系列:Helvetica
“字体大小:11px;”
“颜色:#888888;”
"},";
for(NSString*注释中的单个){
如果(单个==零)
单一=@;
if([single iskindof类:[NSString类]]){
single=[single StringByReplacingOccurrencesof String:@“%20”和String:@];
}
NSString*str;
如果([UIHelper isPad])
{
str=[NSString stringWithFormat:@“%@

Username

,single]; } 否则{ str=[NSString stringWithFormat:@“%@

Username

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

}

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


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

的一个对象,这意味着您试图在
NSDictionary
的一个实例上使用
-stringByReplacingOccurrencesOfString
方法,当然这是行不通的


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

的一个对象,正如Peter在上面所说的,当您从
NSArray
检索对象时,它只是一个指向对象的指针,而不管您将其投射到什么位置。你可以用不同的方法来调查。查看数组内容的设置位置,并检查您正在添加的内容,可能是
NSDictionary
在链的更上层被误播了。另外,在读取数组之前尝试设置断点,并在调试器中键入
po insights
。这将打印对象的内容。崩溃后,您还可以通过执行
po
(在本例中为
po 0x99f60b0
)来调查导致问题的对象

防止这种情况发生的一个好方法是内省(尽管这不会解决阵列中对象错误的问题,但会阻止崩溃),因此:

我看不到你发布的代码中有任何错误,尽管我注意到了一些事情。您不需要检查
single==nil
。在
NSArray
中不可能有
nil

此外,您正在执行大量指针的重新分配,并且还尝试在数组中循环时编辑数组的内容。在Objective-C中,这不是一个好主意。我已经对它进行了测试,在这种情况下它似乎可以工作,但是如果您正在创建一个新字符串,创建一个新的字符串指针通常是一个好主意,以确保旧字符串使用的内存被正确释放。别误会,代码是有效的,但这种事情很快就会导致内存泄漏,以后很难找到,这只是最佳实践


关于崩溃,我看不到从该方法生成的错误消息,以及您发布的原始代码与您发布的额外代码在哪里匹配?请用更多细节更新您的问题

,正如Peter在上面所说的,当您从
NSArray
检索对象时,它只是指向对象的指针,而不管您将其投射到什么位置。你可以用不同的方法来调查。查看数组内容的设置位置,并检查您正在添加的内容,可能是
NSDictionary
在链的更上层被误播了。另外,在读取数组之前尝试设置断点,并在调试器中键入
po insights
。这将打印对象的内容。崩溃后,您还可以通过执行
po
(在本例中为
po 0x99f60b0
)来调查导致问题的对象

防止这种情况发生的一个好方法是内省(尽管这不会解决阵列中对象错误的问题,但会阻止崩溃),因此:

我看不到你发布的代码中有任何错误,尽管我注意到了一些事情。您不需要检查
single==nil
。在
NSArray
中不可能有
nil

此外,您正在执行大量指针的重新分配,并且还尝试在数组中循环时编辑数组的内容。在Objective-C中,这不是一个好主意。我已经测试过它,它在本例中似乎有效,但它是gen
- (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"]];  
if ([strData isKindOfClass:[NSString class]]) {
    NSString *strReplace = [NSString stringWithFormat:@"%@",[strData stringByReplacingOccurrencesOfString:@"%20" withString:@" "]];   
}