简体   繁体   English

访问Objective-C /可可接触阵列

[英]Accessing Objective-C / Cocoa Touch Arrays

I'm using Cocoa Touch to build an iPhone app. 我正在使用Cocoa Touch构建iPhone应用程序。

I have an NSMutableArray called stories, that on printing to the console displays something like this: 我有一个名为Story的NSMutableArray,它在打印到控制台时显示如下内容:

2009-07-20 12:38:30.541 testapp[4797:20b] (
    {
    link = "http://www.testing.com";
    message = "testing";
    username = "test";
},
    {
    link = "http://www.testing2.com";
    message = "testing2";
    username = "test2";
} )

My question is, how can I loop through the array and for example print the value of 'link' each time? 我的问题是,如何循环遍历数组,例如每次打印“ link”的值? In PHP I'm familiar with simply going array[item] - is there any similar way in Objective-C? 在PHP中,我熟悉简单地使用array [item]-在Objective-C中有任何类似的方法吗? I'd like to loop through the array to eventually throw the data into a UITableView. 我想遍历数组以最终将数据放入UITableView中。

Thanks in advance. 提前致谢。

Benji 本治

I think you are conflating 2 things here. 我认为您在这里混淆了两件事。 You want to know how to iterate through a collection (in this case an array), and how to lookup a key in a dictionary. 您想知道如何遍历集合(在本例中为数组),以及如何在字典中查找键。

Objective C provides a collection iteration loop "for (OBJECT in COLLECTION)" to which will loop through every object in an array or a dictionary (in the case of a dictionary it hand you back the key that maps to an object). 目标C提供了一个“用于(对象在集合中)”的集合迭代循环,该循环将遍历数组或字典中的每个对象(如果使用字典,则将返回映射到该对象的键)。

In the case of your array each of the elements is a dictionary, so we can use NSDictionary's -objectForKey: to find out the value of the link and print it: 对于数组,每个元素都是一个字典,因此我们可以使用NSDictionary的-objectForKey:找出链接的值并打印:

for (NSDictionary *story in stories) {
  NSLog(@"%@", [story objectForKey:@"link"]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM