【问题标题】:My iPhone app is crashing and showing [__NSCFDictionary stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance我的 iPhone 应用程序崩溃并显示 [__NSCFDictionary stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance
【发布时间】:2012-09-22 02:50:14
【问题描述】:

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

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

这是我的代码 NSString *strData; AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; if (delegate.respondInsightIndex) { strData = [洞察 objectAtIndex:index]; } 别的 { strData = [洞察 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"]];  

}

【问题讨论】:

    标签: ios string


    【解决方案1】:

    正如 Peter 上面所说,当您从 NSArray 检索对象时,它只是一个指向对象的指针,无论您将其转换为什么。您可以通过各种方式对此进行调查。查看数组内容的设置位置并检查您要添加的内容,NSDictionary 可能被错误地投射到更远的位置。还可以尝试在读取数组之前设置断点,然后在调试器中键入po insights。这将打印对象的内容。崩溃后,您还可以通过执行po &lt;memory address&gt; 来调查导致问题的对象,在本例中为po 0x99f60b0

    防止这种事情的一个好方法是自省(虽然如果你的数组中有错误的对象,这不会解决问题,但会阻止崩溃。)如下:

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

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

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

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

    【讨论】:

    • 请检查我的代码我已经更新了,让我知道出了什么问题
    • 当你断点并单步执行时,哪一行会导致崩溃?是single = [single stringByReplacingOccurrencesOfString:@"%20" withString:@" "]; 吗?
    • 嗯,我找不到那个代码有什么问题,我自己测试过,似乎没问题,尝试清理项目(shift + cmd + k),删除应用程序测试设备/模拟器并重新安装?
    【解决方案2】:

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

    strData 对象不是您想象的 NSString 对象。其实是NSDictionary的对象。

    【讨论】:

      猜你喜欢
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多