如何从数组中获取给定属性具有相同值的对象?
我正在编写新 iOS SSH 客户端的服务器列表部分,并且我有一个模型 RWServer
目前看起来像这样*:
@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
在我的服务器列表控制器中,我有 -[< ;UITableView数据源> tableView:cellForRowAtIndexPath:]
方法如下:
// self.servers is an instance of NSArray
RWServer *server = [self.servers objectAtIndex:indexPath.row];
cell.textLabel.text = server.hostname;
问题是,当用户配置了多个主机名相同但端口号不同的服务器时,它们无法相互区分。为了解决这个问题,我想将端口号附加到标签的文本中,但我不知道如何检索数组中具有相同主机名的项目,所以有人可以帮助我吗?任何帮助表示赞赏。
*是的,保存时所有内容都会加密。
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 could anyone please help me with this? Any help is appreciated.
*Yes, everything is encrypted when saved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(根据要求重新发布我自己的评论)
我想到的是有一个 NSDictionary,其中键是主机名,对象将是所有服务器及其端口和密码等的数组。
很高兴能提供帮助! :)
(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! :)