我使用以下代码替换文件的扩展名:
NSString *fileOriginal = [url lastPathComponent];
NSString *fileSource = @"";
NSRange range = [fileOriginal rangeOfString:@"." options:NSBackwardsSearch];
if (range.length > 0) {
range.length = fileOriginal.length - range.location;
fileSource = [fileOriginal stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@".%@", @"cpp"]];
} else {
fileSource = [fileOriginal stringByAppendingString:[NSString stringWithFormat:@".%@", gFileExtension]];
}
代码工作正常。但是调试器在末尾显示垃圾NSString
:
如果我尝试添加retain
到字符串,结果相同。到底发生了什么?