gpt4 book ai didi

objective-c - 这是在循环中创建/销毁字符串的正确方法吗?

转载 作者:搜寻专家 更新时间:2023-10-30 19:55:57 24 4
gpt4 key购买 nike

只是好奇这是否是这样做的方式,只是想确保它没有泄漏,尽管我认为我只是在修改字符串内容。

NSMutableString *newPath = [[NSMutableString alloc] init];

for(fileName in [manager enumeratorAtPath:rootPath]){
if ([[fileName pathExtension] isEqual:@"exr"]) {
[fileArray addObject:fileName];

// THIS BIT
[newPath setString:rootPath];
[newPath appendString:@"/"];
[newPath appendString:fileName];
// HERE
attrDir = [manager attributesOfItemAtPath:newPath error:&myError];

fileSize = [attrDir objectForKey: @"NSFileSize"];
NSLog(@"File: /%@ Size: %@", fileName, fileSize);
}
}
[newPath release];

加里

最佳答案

这看起来很好泄漏。如果您运行的是 Xcode 3.2,您可以构建 -> 构建和分析器让 Clang 来检查这类事情。

请记住,您只需释放您分配的、新的、复制的或保留的东西。

考虑使用 stringByAppendingPathComponent,而不是对 @"/" 路径分隔符进行硬编码。 NSString 有一个 number of methods像这样专门用于处理路径。

NSString* fullPath = [rootPath stringByAppendingPathComponent:fileName];

关于objective-c - 这是在循环中创建/销毁字符串的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1488995/

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