I'm pretty new to Objective-c and I'm still not familiar with some basic concepts. As of my question, I want to manage a list of urls'. When a new url is being add I want to add it iff it is not already in the list. The trivial way for me to implement this will be:
NSMutableSet<NSURL*>* setOfURLs = /* some set of urls*/;
NSURL* url = [NSURL URLWithString:@"some string"];
[setOfUrls addObject:url];
This approach won't work (will it?) because the set is holding an object instances of NSURL. And it is possible that two different objects will have the same url path. Another approach would be to hold a set of strings but I think maybe there is other / more convenient way to implement this. Any tips/tricks will be appreciated.
The addObject method adds an object to a set only if the set does not already contain it (see Apple documentation).
So, the following code
NSMutableSet<NSURL*>* setOfURLs = [NSMutableSet setWithArray:@[[NSURL URLWithString:@"http://www.first.com"], [NSURL URLWithString:@"http://www.second.com"]]];
NSURL* url = [NSURL URLWithString:@"http://www.third.com"];
[setOfURLs addObject:url];
NSURL* url2 = [NSURL URLWithString:@"http://www.first.com"];
[setOfURLs addObject:url2];
NSLog(@"%@", setOfURLs);
Produces the following output:
{(
http://www.first.com,
http://www.second.com,
http://www.third.com
)}
The result is that http://www.first.com isn't added a second time, because it is already contained in the NSMutableSet