ios - 使用 nsstring 时获取 EXC_BAD_ACCESS

标签 ios iphone objective-c exc-bad-access

我在 4 小时内变得疯狂,我真的需要帮助。这是代码:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    //check if strGroup has prefix and suffix #
    BOOL result;

    result = [strGroup hasPrefix: @"#"];

    if (result)
    {
        result = [strGroup hasSuffix: @"#"];

        if (result)
        {

            NSMutableString* string = [NSMutableString stringWithString: strGroup];
            str = [strGroup substringWithRange: NSMakeRange (1, [string length]-2)];

            strToHoldAllContact = [NSString stringWithFormat:@"%@",str];
        }
    }
    NSLog(@"strToHoldAllContact=%@",strToHoldAllContact);
}

我正确获取了 strToHoldAllContact 的值。但是当我尝试从另一种方法访问 strToHoldAllContact 时,我收到错误:

[CFString respondsToSelector:]: message sent to deallocated instance 0x856f2a0

最佳答案

使用

strToHoldAllContact = [NSString stringWithFormat:@"%@",str];
[[strToHoldAllContact retain] autorelease];

忘记发布。

关于ios - 使用 nsstring 时获取 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10361297/

相关文章:

android - PhoneGap : How to make links from iframes open in InAppBrowser

ios - 一个应用程序中的多个Notification Service Extension

Objective-C 转换为 Swift : Bound Macros?

ios - ios 7工具栏顶部边缘出现一个像素的细线

ios - 方向转换后来自 UIView 的渐变

ios - 在 Swift 语言中有没有什么方法可以将编译后的类发布到已发布的程序中?

ios - CPTScatterPlotInterpolationCurved 类型无法正常工作

iphone - objective-c 源 locationManager 在自己的类中

ios - iphone 应用程序图标 ios 7,8 (3x) for iphone 6

ios - 如何从uibutton的发件人选项中获取值(value)