Ios 从对象所在的数组中获取对象的属性
我有一个名为Ios 从对象所在的数组中获取对象的属性,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我有一个名为Post的NSObject类。post具有属性URL。当我的应用程序启动时,将使用URL创建新帖子,并将其添加到名为_objects的数组中。选择表格单元格后,我想在\u objects[indexPath.row]中将某些内容设置为post.url 向表中添加: NSMutableArray *newPosts = [[NSMutableArray alloc] initWithCapacity:0]; for (SomeElement *element in postNodes)
Post
的NSObject
类。post
具有属性URL
。当我的应用程序启动时,将使用URL创建新帖子,并将其添加到名为_objects的数组中。选择表格单元格后,我想在\u objects[indexPath.row]
中将某些内容设置为post.url
向表中添加:
NSMutableArray *newPosts = [[NSMutableArray alloc] initWithCapacity:0];
for (SomeElement *element in postNodes) {
Post *post = [[Post alloc] init];
[newPosts addObject:post];
post.title = [[element firstChild] content];
post.url = [element objectForKey:@"href"];
}
_objects = newPosts;
如果我在选择单元格时调用对象[indexPath.row],它会返回或类似的东西。这就是您需要的吗
Post *post = _objects[indexPath.row];
[something setURL:post.url];
您还可以将代码段的第一行替换为以下内容,从而提高效率:
NSMutableArray *newPosts = [[NSMutableArray alloc] initWithCapacity:postNodes.count];
这将在数组中预先分配足够的空间来容纳所有帖子