EXC_BAD_ACESS 错误

发布于 2024-12-07 03:24:05 字数 1130 浏览 6 评论 0原文

我在以下行收到错误 EXC_BAD_ACESS:

NSString *titleVarName = [[NSString alloc] initWithFormat:@"%@%@",@"occasionTitle",i];

这是上面代码行所在的 for 循环:

for (i=0; i < count; ++i) 
{

    //Save the occasionS details to NSUserDefaults

    NSString *titleVarName = [[NSString alloc] initWithFormat:@"%@%@",@"occasionTitle",i];

    NSString *dateVarName = [[NSString alloc] initWithFormat:@"%@%@",@"occasionDate",i];

    NSString *imageVarName = [[NSString alloc] initWithFormat:@"%@%@",@"occasionImage",i];


    [[NSUserDefaults standardUserDefaults] setValue:[[[self displayedObjects] objectAtIndex:i]     
title] forKey:titleVarName];

    [[NSUserDefaults standardUserDefaults] setValue:[[[self displayedObjects] objectAtIndex:i]     
date] forKey:dateVarName];

    [[NSUserDefaults standardUserDefaults] setValue:[[[self displayedObjects] objectAtIndex:i] 
imagePath] forKey:imageVarName]; 

    //release

    [titleVarName release];
    [dateVarName release];
    [imageVarName release];


    [self dismissModalViewControllerAnimated:YES];
}

不能在 for 循环中分配对象并释放它们吗?

I get that error EXC_BAD_ACESS at the following line:

NSString *titleVarName = [[NSString alloc] initWithFormat:@"%@%@",@"occasionTitle",i];

Here is the for loop where the above code line is located:

for (i=0; i < count; ++i) 
{

    //Save the occasionS details to NSUserDefaults

    NSString *titleVarName = [[NSString alloc] initWithFormat:@"%@%@",@"occasionTitle",i];

    NSString *dateVarName = [[NSString alloc] initWithFormat:@"%@%@",@"occasionDate",i];

    NSString *imageVarName = [[NSString alloc] initWithFormat:@"%@%@",@"occasionImage",i];


    [[NSUserDefaults standardUserDefaults] setValue:[[[self displayedObjects] objectAtIndex:i]     
title] forKey:titleVarName];

    [[NSUserDefaults standardUserDefaults] setValue:[[[self displayedObjects] objectAtIndex:i]     
date] forKey:dateVarName];

    [[NSUserDefaults standardUserDefaults] setValue:[[[self displayedObjects] objectAtIndex:i] 
imagePath] forKey:imageVarName]; 

    //release

    [titleVarName release];
    [dateVarName release];
    [imageVarName release];


    [self dismissModalViewControllerAnimated:YES];
}

Isn't ok to alloc objects and release them inside a for loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯淡〆 2024-12-14 03:24:05

您需要使用 %d%i 说明符而不是 %@ 来指定整数。如果 %@ 与 int 一起使用,那么它将尝试访问 int 指定的地址处的对象。例如,如果i的值为1,则它正在尝试访问地址1处的对象,这将导致访问错误。

NSString *titleVarName = [[NSString alloc] initWithFormat:@"%@%d",@"occasionTitle",i];

而且这里也不需要 allocrelease,尽管这不是访问不良的原因。您可以使用方便的构造函数。

NSString *titleVarName = [NSString stringWithFormat:@"occasionTitle%d", i];
// release not required

dateVarNameimageVarName 也执行相同的操作。

You need to use %d or %i specifier instead of %@ to specify an integer. If %@ is used with int then it will try to access the object at the address specified by the int. For example, if the value of i is one then it is trying to access the object at address one which will cause a bad access.

NSString *titleVarName = [[NSString alloc] initWithFormat:@"%@%d",@"occasionTitle",i];

And also you don't need alloc and release here, though that is not the reason of bad access. You can use a convenience constructor.

NSString *titleVarName = [NSString stringWithFormat:@"occasionTitle%d", i];
// release not required

Do the same for dateVarName and imageVarName too.

玩物 2024-12-14 03:24:05

假设 i 是一个 int,该行应该是

NSString *titleVarName = [[NSString alloc] initWithFormat:@"%@%i",@"occasionTitle",i];

%@ 用于 Cocoa 对象,而不是像 int、float 或 bool 这样的基元;

Assuming i is an int, that line should be

NSString *titleVarName = [[NSString alloc] initWithFormat:@"%@%i",@"occasionTitle",i];

%@ is used for Cocoa objects, not primitives like an int, float or bool;

浪荡不羁 2024-12-14 03:24:05

仅对 NSObject 对象使用 %@ 格式说明符。

由于 i 是代码中的整数,因此必须使用 %d%i 表示整数。

此外,不需要使用 %@ 包含字符串,您可以直接在格式字符串中使用静态字符串:

NSString *titleVarName = [[NSString alloc] initWithFormat:@"occasionTitle%i",i];

Use the %@ format specifier only for NSObject objects.

As i is an integer in your code, you have to use %d or %i for integers.

Moreover, there is no need to include the string using %@, you can use the static string directly in your format string:

NSString *titleVarName = [[NSString alloc] initWithFormat:@"occasionTitle%i",i];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文