首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >部分do-while语句跳过

部分do-while语句跳过
EN

Stack Overflow用户
提问于 2014-03-31 09:26:16
回答 1查看 44关注 0票数 0

我正在开发一个语音备忘录应用程序,我正在将文件保存到表视图中。我希望默认文件名为"New file 1“,如果采用"New File 1”,则它将读取"New File 2“,依此类推。

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

代码语言:javascript
复制
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]);

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

有什么想法可以解释为什么会这样吗?

谢谢

编辑:

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

EN

回答 1

Stack Overflow用户

发布于 2014-03-31 09:43:51

你可以简化这一点,但这对我很有效:

代码语言:javascript
复制
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]);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22752255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档