开发者

How to get the objects from an array that have the same value for a given property?

I'm writing the servers list part of a new iOS SSH client, and I have a model RWServer which currently looks like this*:

@interface RWServer : NSObject <NSCoding> {
  NSString *_hostname;
  NSUInteger _port;
  NSString *_password;
}

@property(nonatomic, copy)   NSString *hostname;
@property(nonatomic, assign) NSUInteger port;
@property(nonatomic, copy)   NSString *password;

@end

In my servers list controller I have in the -[<UITableViewDataSource> tableView:cellForRowAtIndexPath:] method the following:

// self.servers is an instance of NSArray
RWServer *server = [self.servers objectAtIndex:indexPath.row];
cell.textLabel.text = server.hostname;

The problem is that when a user has configured multiple servers with the same hostname but a different port number, they cannot distinguish them from eachother. To solve this I want to append the port number to the label's text, but I don't know how to retrieve the items in the array that have the same hostname, so coul开发者_Python百科d anyone please help me with this? Any help is appreciated.


*Yes, everything is encrypted when saved.


(repost of my own comment, on request)

What comes to my mind would be to have an NSDictionary where the key is the hostname, and the object would be an array of all servers with their ports and passwords and whatnot.

Happy to have helped! :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜