使用 ObjC 打印文件名有困难
我在打印使用 NSFileManager 检索到的文件名列表时遇到一些问题。文档说它返回一个字符串数组 - 但我的输出是空白的。在下面的代码示例中,执行显示了带有冒号的索引,没有其他内容...我是一名长期的 java 程序员,但对于 c/objc 来说完全是新手。我只是滥用 printf 吗?感谢您的帮助!
输出:
即将打印文件列表------------------------------------
文件数量:29
0: 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28:
- (IBAction)printFileList:(id)sender{
NSFileManager *fm = [[NSFileManager alloc] init];
NSString *path = @"/";
NSArray *files = [fm contentsOfDirectoryAtPath:path error:NULL];
printf("About to print file list ----------------------- \n");
int fileCount = [files count];
printf("Number of files: %d", fileCount);
for(int i = 0; i<fileCount; i++){
printf("%d: %s\n", i, [files objectAtIndex:i]);
}
}
I'm having some trouble printing a list of file names retrieved with NSFileManager. The documentation says it returns an array of strings - but my output is blank. In the code sample below, execution reveals the index with colon and nothing else... I am a long time java programmer but total newb to c/objc. Am I just misusing printf? Thanks for your help!
Output:
About to print file list -----------------------
Number of files: 29
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
- (IBAction)printFileList:(id)sender{
NSFileManager *fm = [[NSFileManager alloc] init];
NSString *path = @"/";
NSArray *files = [fm contentsOfDirectoryAtPath:path error:NULL];
printf("About to print file list ----------------------- \n");
int fileCount = [files count];
printf("Number of files: %d", fileCount);
for(int i = 0; i<fileCount; i++){
printf("%d: %s\n", i, [files objectAtIndex:i]);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用
%s
作为NSString *
的格式说明符,因为NSString *
是一个指针,而不是 C 字符串。将printf
行更改为:You can't use
%s
as a format specifier forNSString *
, sinceNSString *
is a pointer, not a C string. Change yourprintf
line to: