gpt4 book ai didi

Objective-c数组

转载 作者:太空狗 更新时间:2023-10-30 03:59:38 25 4
gpt4 key购买 nike

我在使用这段代码时有点困惑;

NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/Volumes/" error:nil];

int arraysize = sizeof dirContents;

获取“Volumes”目录的内容数组,但是当我输出数组大小时它说当该目录中只有 4 个文件时该数组有 8 个条目?这不是问题,但因为我一到就使用了 for 循环;

NSString *volume1 = [dirContents objectAtIndex:4];

(4 是 for 循环中的值)应用程序崩溃并拒绝启动?

感谢您的帮助

最佳答案

您应该使用 int arraysize = [dirContents count] 来获得正确的大小。

sizeof 是一个 c 风格的运算符,不能在 Objective-C 对象上正常工作。

关于Objective-c数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8695230/

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