How to generate random string in Objective C based on core data values

user3564078 picture user3564078 · May 2, 2014 · Viewed 8.6k times · Source

I am developing one iPad application using storyboard and core data.For my application i need to generate one random string like 'M000142140502343524' which are not already exist in the 'data' field of the 'tableA' when click a button.

Answer

Avt picture Avt · May 2, 2014

Common way to generate an unique string is

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

or

NSString *identifier = [[NSProcessInfo processInfo] globallyUniqueString];

But you also could create such string yourself. For example:

+ (NSString *)createRandomName
{
    NSTimeInterval timeStamp = [ [ NSDate date ] timeIntervalSince1970 ];
    NSString *randomName = [ NSString stringWithFormat:@"M%f", timeStamp];
    randomName = [ randomName stringByReplacingOccurrencesOfString:@"." withString:@"" ];
    return randomName;
}