获取NSArray长度/计数-目标C

时间:2022-09-06 21:16:20

Well I've looked at similar problems over the site but haven't reached a solution thus far so I must be doing something wrong.

我在网站上看过类似的问题,但到目前为止还没有找到解决办法,所以我一定是做错了什么。

Essentially, I am importing a text file, then splitting each line into an element of an array. Since the text file will be updated etc.. I won't every know the exact amount of lines in the file and therefore how many elements in the array. I know in Java you can do .length() etc.. and supposedly in Objective C you can use 'count' but i'm having no luck returning the length of my array... suggestions?

本质上,我正在导入一个文本文件,然后将每一行分割成一个数组的元素。因为文本文件会被更新等等。我不会知道文件中确切的行数以及数组中的元素个数。我知道在Java中你可以做。length()等。在Objective - C中,你可以用count,但是我不能返回数组的长度…建议吗?

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"allshows" 
                                                     ofType:@"txt"];
 NSString *fileString = [NSString stringWithContentsOfFile:filePath];

NSArray *lines = [fileString componentsSeparatedByString:@"\n"];  

NSUInteger *elements = [lines count];
NSLog(@"Number of Shows : ", elements);

and what is being output is NOTHING. as in "Number of Shows : " - blank, like it didn't even count at all.

输出什么都不是。就像在“秀的数量:”-空白,好像它根本不算数。

Thank you for any help!

谢谢你的帮助!

3 个解决方案

#1


16  

You're missing the format string placeholder. It should be:

您缺少格式字符串占位符。应该是:

NSLog(@"Number of shows: %lu", elements);

#2


5  

You need to use a format specifier to print an integer (%d):

您需要使用格式说明符打印一个整数(%d):

NSLog(@"Number of Shows : %d", elements);

#3


0  

Looking at your other post, it seems like you are a Java developer. In Java's System.out, you just append the variables. In Objective-C, I suggest you look at "print format specifiers". Objective-C uses the same format.

看看你的另一篇文章,你似乎是一个Java开发人员。在Java的系统。你只需添加变量。在Objective-C中,我建议您查看“打印格式说明符”。Objective-C使用相同的格式。

#1


16  

You're missing the format string placeholder. It should be:

您缺少格式字符串占位符。应该是:

NSLog(@"Number of shows: %lu", elements);

#2


5  

You need to use a format specifier to print an integer (%d):

您需要使用格式说明符打印一个整数(%d):

NSLog(@"Number of Shows : %d", elements);

#3


0  

Looking at your other post, it seems like you are a Java developer. In Java's System.out, you just append the variables. In Objective-C, I suggest you look at "print format specifiers". Objective-C uses the same format.

看看你的另一篇文章,你似乎是一个Java开发人员。在Java的系统。你只需添加变量。在Objective-C中,我建议您查看“打印格式说明符”。Objective-C使用相同的格式。