我正在开发一个语音备忘录应用程序,我正在将文件保存到表视图中。我希望默认文件名为"New file 1“,如果采用"New File 1”,则它将读取"New File 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]);如果我将while部分替换为类似(x<10)的内容,那么xcode将读取while语句,文件名自然会变成New File10,否则,它会跳过while语句。
有什么想法可以解释为什么会这样吗?
谢谢
编辑:
这不是跳过。我误解了步入公式的步骤。我后来在我的文件名末尾添加了一个@".mp4“,但我没有将其添加到testPath中。现在我添加了.mp4,它工作得很好。尽管如此,还是要感谢您的简化和考虑。
发布于 2014-03-31 09:43:51
你可以简化这一点,但这对我很有效:
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]);https://stackoverflow.com/questions/22752255
复制相似问题