Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何从NSArray中选择一个对象?_Iphone_Objective C_C_Arrays - Fatal编程技术网

Iphone 如何从NSArray中选择一个对象?

Iphone 如何从NSArray中选择一个对象?,iphone,objective-c,c,arrays,Iphone,Objective C,C,Arrays,首先祝大家圣诞快乐!!!目前我有一个NSArray,它解析了其中的内容。当我对数组进行NSLog时,它会打印出20个对象,其中包含我需要的解析内容。像这样: 2010-12-24 20:27:32.170 TestProject[48914:298] SomeContent 2010-12-24 20:27:32.172 TestProject[48914:298] SomeContent1 2010-12-24 20:27:32.172 TestProject[48914:298] SomeC

首先祝大家圣诞快乐!!!目前我有一个NSArray,它解析了其中的内容。当我对数组进行NSLog时,它会打印出20个对象,其中包含我需要的解析内容。像这样:

2010-12-24 20:27:32.170 TestProject[48914:298] SomeContent
2010-12-24 20:27:32.172 TestProject[48914:298] SomeContent1
2010-12-24 20:27:32.172 TestProject[48914:298] SomeContent2
2010-12-24 20:27:32.173 TestProject[48914:298] SomeContent3
2010-12-24 20:27:32.173 TestProject[48914:298] SomeContent4
2010-12-24 20:27:32.173 TestProject[48914:298] SomeContent5
2010-12-24 20:27:32.174 TestProject[48914:298] SomeContent6
2010-12-24 20:27:32.175 TestProject[48914:298] SomeContent7
2010-12-24 20:27:32.176 TestProject[48914:298] SomeContent8
2010-12-24 20:27:32.176 TestProject[48914:298] SomeContent9
2010-12-24 20:27:32.177 TestProject[48914:298] SomeContent10
2010-12-24 20:27:32.177 TestProject[48914:298] SomeContent11
2010-12-24 20:27:32.177 TestProject[48914:298] SomeContent12
2010-12-24 20:27:32.179 TestProject[48914:298] SomeContent13
2010-12-24 20:27:32.179 TestProject[48914:298] SomeContent14
2010-12-24 20:27:32.180 TestProject[48914:298] SomeContent15
2010-12-24 20:27:32.180 TestProject[48914:298] SomeContent16
2010-12-24 20:27:32.181 TestProject[48914:298] SomeContent17
2010-12-24 20:27:32.181 TestProject[48914:298] SomeContent18
2010-12-24 20:27:32.190 TestProject[48914:298] SomeContent19
但是,我不需要同时使用所有对象。我需要能够一次选择一个对象,这样我就可以将每个对象放入自己的字符串中。如果有人知道一种更简单的方法,那么请让我知道我想要做的。无论如何,我需要能够根据我需要的对象选择一个对象。例如,假设我只需要对象5,而不是所有数组,如何才能将其放入字符串中?我想我可能必须使用索引功能,但我不确定如何正确设置它。这是我的NSArray,我正在与之合作:

NSArray* myArray = [document selectElements: @"div.someContent"];
NSMutableArray* results = [NSMutableArray array];
for (Element* element in myArray){
    NSString* snipet = [element contentsSource];
    [results addObject: snipet];
    NSLog(@"%@", snipet);
}
NSLog(@"%i",myArray.count);

我已经花了几个小时试图实现这一点,但即使我阅读了文档,我对阵列的知识也有限:非常感谢您的帮助。感谢查看文档中的-objectAtIndex:

以检索数组中的第一个对象:

id obj = [array objectAtIndex:0];
id obj = [array objectAtIndex:arc4random_uniform(array.count)];
要检索数组中的随机对象,请执行以下操作:

id obj = [array objectAtIndex:0];
id obj = [array objectAtIndex:arc4random_uniform(array.count)];
请参阅和


有关如何使用nsarray的详细信息,请参阅。

是否有其他方法可以向我展示一段示例代码?我已经知道我可能必须使用它,但永远无法让它正常工作。感谢应用程序不断崩溃。有一条警告指出,“NSString”可能不响应“-contentsSource”或“NSString”可能不响应“-objectAtIndex”,这是我正在使用的代码NSLog@%@,[snipet objectAtIndex:5]@OSX使用[myArray objectAtIndex:5]。谢谢,我以前试过,它一直打印出所有对象,我认为这是错误的,但我在{}中使用了它,所以它重复了20次。我的错,谢谢雅各布!雅各布又一次拯救了世界+1.