NSString 问题
我在 Mac OS X 上使用简单的 NSString 时遇到问题:
NSString *letters = @"abcdefghijklmnopqrstuvwxyz0123456789";
myString = [myString stringByAppendingFormat:@"%c", [letters characterAtIndex:3]];
当我尝试再次访问该字符串时,Xcode 返回 EXC_BAD_ACCESS
当我使用格式 @"%c" 时,就会发生此错误 当我使用 @"%@" 时,有时会出现相同的错误,有时会出现此字符串:control 0x10040a480
,有时会出现以下情况:
{(
<CFRunLoopObserver 0x10015ac60 [0x7fff70731ee0]>{locked = No, valid = Yes, activities = 0x21, repeats = Yes, order = 0, callout = _ZL15FlushAllBuffersP19__CFRunLoopObservermPv (0x7fff88a147d4), context = <CFRunLoopObserver context 0x0>}
)}
即使我不更改代码中的任何内容,错误也会随机发生,并且重新运行它。
我尝试通过以下方式获取随机字符串:
randomString = @"";
NSString *letters = @"abcdefghijklmnopqrstuvwxyz0123456789";
srand(time(NULL));
for (int i=0; i<5; i++)
{
randomString = [randomString stringByAppendingFormat:@"%c", [letters characterAtIndex:(rand()%[letters length])]];
}
randomString 在 header.h 中声明 我也尝试过使用 NSMutableString 但这也不起作用。 每次我尝试通过 @"%@"
访问字符串(或可变字符串)时,我都会收到 EXC_BAD_ACCESS
有什么想法吗? 希望有人能帮助我!
问候, 朱利安
I have problems with a simple NSString on Mac OS X:
NSString *letters = @"abcdefghijklmnopqrstuvwxyz0123456789";
myString = [myString stringByAppendingFormat:@"%c", [letters characterAtIndex:3]];
When I try to access this string again, Xcode is returning EXC_BAD_ACCESS
This error just occures, when I'm using the format @"%c"
When I'm using @"%@", sometimes the same error, sometimes this string: control 0x10040a480
and sometimes this:
{(
<CFRunLoopObserver 0x10015ac60 [0x7fff70731ee0]>{locked = No, valid = Yes, activities = 0x21, repeats = Yes, order = 0, callout = _ZL15FlushAllBuffersP19__CFRunLoopObservermPv (0x7fff88a147d4), context = <CFRunLoopObserver context 0x0>}
)}
The errors occur randomly even, if I don't change anything in the code and re-run it.
I try to get a random String by doing:
randomString = @"";
NSString *letters = @"abcdefghijklmnopqrstuvwxyz0123456789";
srand(time(NULL));
for (int i=0; i<5; i++)
{
randomString = [randomString stringByAppendingFormat:@"%c", [letters characterAtIndex:(rand()%[letters length])]];
}
randomString is declared in header.h
I also tried using a NSMutableString but that didn't work too.
Every time I try to access the string (or mutable string) via @"%@"
I'm getting EXC_BAD_ACCESS
Any idea?
Hope somebody can help me!
Greets,
Julian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是
myString
在您访问它之前会自动释放。您需要将: 更改
为:
完成后记得运行
[myString release];
。Your problem is that
myString
gets autoreleased before you access it.You need to change:
to:
Remember to run
[myString release];
when you are done with it.这对我有用:
在文件顶部定义字母:
然后附加一个随机字符,如下所示:
this works for me:
define letters at top of file:
then append a random character like this: