Search code examples
iosxcodensurlcustom-tag

Add a custom tag to an url in xcode


It's Julian Again. In my project I have around 50 different urls and i need to group them into two groups. So that 25 of them are displayed in VC one and the rest in VC2. I wanted to add a tag, and then based on this tag distinguish the two groups but I don't know how to do that. So that it looks like that:

NSURL tag:@"a"

and then

if(tag == "a")
{// Do the code}

How could I do that Thanks in advance!


Solution

  • You can either using NSDictionary or NSObject to store your URL instance and the associated tag value. I would prefer using NSObject:

    Create a subclass of NSObject called MyNSURLObject.

    For MyNSURLObject.h: (you don't need to modify MyNSURLObject.m file)

    #import <Foundation/Foundation.h>
    
    @interface MyNSURLObject : NSObject
    @property(strong, nonatomic) NSURL *myURL;
    @property(strong, nonatomic) NSString *myTag;
    @end
    

    Create an instance of MyNSURLObject whenever you need to store an URL, ex:

    MyNSURLObject *myNSURLObject = [[MyNSURLObject alloc] init];
    myNSURLObject.myURL = whateverurl;
    myNSURLObject.myTag = @"a"; // or @"b"