Reputation: 14314
I need to generate some int value that would never repeat (at least theoretically). I know there is arc4random() fnc but I'm not sure how to use it with some current date or smth :(
Upvotes: 24
Views: 31952
Reputation: 706
A simple version to generate UUID (iOS 6 or later).
Objective-C:
NSString *UUID = [[NSUUID UUID] UUIDString];
Swift 3+:
let uuid = UUID().uuidString
It will generate something like 68753A44-4D6F-1226-9C60-0050E4C00067, which is unique every time you call this function, even across multiple devices and locations.
Upvotes: 42
Reputation: 1414
This returns a unique key very similar to UUID generated in MySQL.
+ (NSString *)uuid
{
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
return [(NSString *)uuidStringRef autorelease];
}
ARC version:
+ (NSString *)uuid
{
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
return (__bridge_transfer NSString *)uuidStringRef;
}
Upvotes: 54
Reputation: 59
If you have a NSDictionary, you could generate a progressive id from the last item:
NSInteger maxKey = -1;
for(NSString *key in [YOUR_DICTIONARY allKeys])
{
NSInteger intKey = [key integerValue];
if(intKey > maxKey)
{
maxKey = intKey;
}
}
NSString *newKey = [NSString stringWithFormat:@"%d", maxKey + 1];
Upvotes: 1
Reputation: 8005
A simple timestamp (milliseconds * 10) should do the trick:
self.uid = [NSNumber numberWithInteger:[NSDate timeIntervalSinceReferenceDate] * 10000];
Upvotes: 2
Reputation: 788
You can create a category of UIApplication , UIDevice or as you prefere like this (ARC example)
@interface UIApplication (utilities)
- (NSString*)getUUID;
@end
@implementation UIApplication (utilities)
- (NSString*)getUUID {
NSUserDefaults *standardUserDefault = [NSUserDefaults standardUserDefaults];
static NSString *uuid = nil;
// try to get the NSUserDefault identifier if exist
if (uuid == nil) {
uuid = [standardUserDefault objectForKey:@"UniversalUniqueIdentifier"];
}
// if there is not NSUserDefault identifier generate one and store it
if (uuid == nil) {
uuid = UUID ();
[standardUserDefault setObject:uuid forKey:@"UniversalUniqueIdentifier"];
[standardUserDefault synchronize];
}
return uuid;
}
@end
UUID () is this function
NSString* UUID () {
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
return (__bridge NSString *)uuidStringRef;
}
this generate an unique identifier stored into the NSUserDefault to be reused whenever the application need it - This identifier will unique related to the application installs not to the device, but can be used for example to take trace about the number devices subscribed the APN service etc...
After that you can use it in this way:
NSString *uuid = [[UIApplication sharedApplication] getUUID];
Upvotes: 2
Reputation: 5765
If you are using CoreData to save the played games, NSManagedObject
's objectID
should serve your purpose without any extra effort.
Upvotes: 3
Reputation: 5935
You have to be careful, especially if you use the increment by 1 routines, that if your app is deleted and reloaded on the iDevice, that you won't have your saved default number anymore. It will start over from the beginning. If you're storing user's scores, you might want to save their highest number too. Better to check the time routines for seconds (or milliseconds) after a certain date. The GUID mentioned above is good too, if you need that kind of uniqueness.
Upvotes: 0
Reputation: 36762
You did not say it must be random. So why not start with some number, and then just add by 1 to the last number you generated.
This method should give you at lest 4 billion unique numbers to start with:
-(NSInteger)nextIdentifies;
{
static NSString* lastID = @"lastID";
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSInteger identifier = [defaults integerForKey:lastID] + 1;
[defaults setInteger:identifier forKey:lastID];
[defaults synchronize];
return identifier;
}
Upvotes: 1