Question

I am using NSUUID for unique ids in my app like so:

[[NSUUID UUID] UUIDString]

and as is the expected result I get an id like this one: 102A21AD-7216-4517-8A79-39776B767E72

For backend reasons I need the letters in the uuid to be lowercase. I tried to call

[[NSUUID UUID] UUIDString].lowercaseString 

but the returned string is empty.

Do I really have to iterate over all of the characters in the string and convert the appropriate ones to lowercase? If so does anyone have any advice of the most efficient way to do this?

EDIT:

The way I was trying to implement this was by subclassing NSUUID and then overriding the

-(NSString*) UUIDString; 

method.

My implementation of this was

-(NSString*) UUIDString{
    return [super UUIDString].lowercaseString;
}

The accepted answer explains why this doesn't work.

Was it helpful?

Solution

Based on your edit to the question a little investigation is in order...

It looks like NSUUID behaves like a class cluster and you cannot sub-class it without implementing it's key methods and providing the functionality of UUID generation yourself. If you do sub-class it you get a parent class whose UUIDString is the empty string. While a standard init of the class gives you back an instance of __NSConcreteUUID whose UUIDString is more useful!

If the above is confusing the following partial implementation shows one way to do this:

@interface LowerUUID : NSUUID
@end

@implementation LowerUUID
{
   NSUUID *real;
}

- (id) init
{
   self = [super init];
   if (self)
      real = NSUUID.new;
   return self;
}

- (NSString *) UUIDString
{
   NSString *original = [real UUIDString];
   NSString *lower = original.lowercaseString;
   return lower;
}

@end

To be complete you also need to provide implementations of the other methods.

For this particular class it is unlikely you'll find this worth it, but for class clusters like NSMutableArray it does make sense.

You could submit a bug report to Apple stating the documentation does not state you cannot trivially sub-class NSUUID.

OTHER TIPS

This works for me:

NSString *lower = [[[NSUUID UUID] UUIDString] lowercaseString];

I tried that and it works:

NSString *str = [[NSUUID UUID] UUIDString];
NSLog(@"1: %@", str);
NSLog(@"2: %@", str.lowercaseString);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top