gpt4 book ai didi

iOS appendFomat 导致应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 17:43:34 26 4
gpt4 key购买 nike

下面的代码有什么问题吗?它有时会使我的应用程序崩溃。

- (NSString*) getDisplayString
{
NSMutableString* display = [[NSMutableString alloc] initWithString:@""];

for(NSUInteger i = 0; i < [entries count]; i++)
{
Entry* e = [entries objectAtIndex:i];
[display appendFormat:@"%d. %@ %30d\n", i+1, e.title, e.value];

}

return display;
}

最佳答案

找到了我的问题的答案。问题是内存管理,我没有增加在 Entry 对象中找到的 NSString 标题的保留计数。 title 是通过文本字段从用户输入获得的值,因此当 UIView 释放内部完成的字符串时,它不再在内存中,因此它使错误看起来是随机的。

因此总结一下,每当您使用文本字段中的值时,您都需要增加其保留计数。

NSMutableArray* myArray = [[NSMutableArray alloc] init];
NSString* someStringToStore = uiTextField.text;
[someStringToStore retain];
[myArray addObject: someStringToStore];

关于iOS appendFomat 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7094316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com