gpt4 book ai didi

ios - while 部分 do-while 语句跳过

转载 作者:行者123 更新时间:2023-11-28 22:11:45 33 4
gpt4 key购买 nike

我正在开发一个语音备忘录应用程序,并且正在将文件保存到表格 View 中。我希望默认文件名显示为“新文件 1”,如果使用“新文件 1”,则它会显示为“新文件 2”,依此类推。

我正在尝试使用 do-while 循环来完成此操作,但是当我单步执行代码时它会跳过 while 语句。这是我所拥有的:

int x = 1;
NSString *firstName = [NSString stringWithFormat:@"New File %d", x];

NSArray *searchPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsPath = [searchPaths objectAtIndex: 0];
NSString *testPath =[NSString stringWithFormat: @"%@/%@", documentsPath, firstName];


do
{x++;
firstName =[NSString stringWithFormat:@"New File %d", x];
searchPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
documentsPath = [searchPaths objectAtIndex: 0];
testPath =[NSString stringWithFormat: @"%@/%@", documentsPath, firstName];
}
while ([[NSFileManager defaultManager] fileExistsAtPath:testPath]);

如果我用类似 (x<10) 的内容替换 while 部分,那么 xcode 会读取 while 语句并且文件名自然会变成 New File 10。但否则,它会跳过 while 语句。

关于为什么会这样有什么想法吗?

谢谢

编辑:

这不是跳过。我误解了进入公式。我后来在我的文件名末尾添加了一个@".mp4",但我没有将它添加到 testPath 中。现在我添加了 .mp4,效果很好。尽管如此,还是感谢您的简化和考虑。

最佳答案

你可以简化它,但这对我有用:

int x = 0;
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [searchPaths objectAtIndex: 0];
NSString *testPath;

do
{
x++;
testPath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"New File %d", x]];
}
while ([[NSFileManager defaultManager] fileExistsAtPath:testPath]);

关于ios - while 部分 do-while 语句跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752255/

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