gpt4 book ai didi

objective-c - 如何在 Objective-C 中打印单个数组元素?

转载 作者:太空狗 更新时间:2023-10-30 03:50:22 24 4
gpt4 key购买 nike

如何在 Objective-C 中打印特定索引处的数组元素?我的代码如下所示:

NSString *String=[NSString StringWithContentsOFFile:@"/User/Home/myFile.doc"];
NSString *separator = @"\n";
NSArray *array = [String componetntsSeparatedByString:separator];
NSLog(@"%@",array);

我可以一次打印数组的全部内容,但我想将每个索引处的元素分配到一个字符串中,例如...

str1=array[0];
str2=array[1];
str3=array[0];...this continues

我该怎么做?

最佳答案

您需要 objectAtIndex: 方法。示例:

NSString *str1 = [array objectAtIndex:0];
NSString *str2 = [array objectAtIndex:1];
NSString *str3 = [array objectAtIndex:2];

来自documentation :

objectAtIndex:
返回位于 index 的对象。

- (id)objectAtIndex:(NSUInteger)index

参数
索引
接收器范围内的索引。

返回值
对象位于 index

讨论
如果 index 超出数组的末尾(即,如果 index 大于或等于 count 返回的值),一个NSRangeException 被引发。

关于objective-c - 如何在 Objective-C 中打印单个数组元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2205414/

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