EXC_BAD_ACESS 错误
我在以下行收到错误 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用
%d
或%i
说明符而不是%@
来指定整数。如果%@
与 int 一起使用,那么它将尝试访问 int 指定的地址处的对象。例如,如果i
的值为1,则它正在尝试访问地址1处的对象,这将导致访问错误。而且这里也不需要
alloc
和release
,尽管这不是访问不良的原因。您可以使用方便的构造函数。对
dateVarName
和imageVarName
也执行相同的操作。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 ofi
is one then it is trying to access the object at address one which will cause a bad access.And also you don't need
alloc
andrelease
here, though that is not the reason of bad access. You can use a convenience constructor.Do the same for
dateVarName
andimageVarName
too.假设
i
是一个int
,该行应该是%@
用于 Cocoa 对象,而不是像 int、float 或 bool 这样的基元;Assuming
i
is anint
, that line should be%@
is used for Cocoa objects, not primitives like an int, float or bool;仅对
NSObject
对象使用%@
格式说明符。由于
i
是代码中的整数,因此必须使用%d
或%i
表示整数。此外,不需要使用
%@
包含字符串,您可以直接在格式字符串中使用静态字符串:Use the
%@
format specifier only forNSObject
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: