如何生成唯一标识符?

发布于 2024-11-28 20:02:31 字数 88 浏览 3 评论 0原文

我需要生成一些永远不会重复的 int 值(至少理论上如此)。我知道有 arc4random() fnc 但我不知道如何将它与某些当前日期或 smth 一起使用:(

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 :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

等待圉鍢 2024-12-05 20:02:31

这会返回一个与 MySQL 中生成的 UUID 非常相似的唯一键。

+ (NSString *)uuid
{
    CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    CFRelease(uuidRef);
    return [(NSString *)uuidStringRef autorelease];
}

ARC版本:

+ (NSString *)uuid
{
    CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    CFRelease(uuidRef);
    return (__bridge_transfer NSString *)uuidStringRef;
}

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;
}
魔法少女 2024-12-05 20:02:31

生成 UUID 的简单版本(iOS 6 或更高版本)。

Objective-C:

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

Swift 3+:

let uuid = UUID().uuidString

它将生成类似 68753A44-4D6F-1226-9C60-0050E4C00067 的内容,每次调用此函数时它都是唯一的,即使跨多个设备和位置也是如此。

参考:
https://developer.apple.com/library /ios/documentation/Foundation/Reference/NSUUID_Class/Reference/Reference.html

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.

Reference:
https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSUUID_Class/Reference/Reference.html

倦话 2024-12-05 20:02:31

如果您使用 CoreData 保存玩过的游戏,NSManagedObjectobjectID 应该可以满足您的目的,无需任何额外的努力。

If you are using CoreData to save the played games, NSManagedObject's objectID should serve your purpose without any extra effort.

愚人国度 2024-12-05 20:02:31

您可以使用以毫秒为单位的时间或更高级的方式GUID

You can use the time in milliseconds or a more advanced way GUID.

凑诗 2024-12-05 20:02:31

您可以创建 UIApplication 、 UIDevice 的类别,或者像这样(ARC 示例)

@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 () 是这个函数,

NSString* UUID () {

    CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    CFRelease(uuidRef);
    return (__bridge NSString *)uuidStringRef;
}

它会生成一个存储到 NSUserDefault 中的唯一标识符,以便在应用程序需要时重用 - 该标识符将与应用程序不会安装到设备上,但可用于跟踪订阅 APN 服务的设备数量等...

之后,您可以通过以下方式使用它:

    NSString *uuid = [[UIApplication sharedApplication] getUUID];

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];
孤单情人 2024-12-05 20:02:31

一个简单的时间戳(毫秒 * 10)应该可以解决问题:

self.uid = [NSNumber numberWithInteger:[NSDate timeIntervalSinceReferenceDate] * 10000];

A simple timestamp (milliseconds * 10) should do the trick:

self.uid = [NSNumber numberWithInteger:[NSDate timeIntervalSinceReferenceDate] * 10000];
故事和酒 2024-12-05 20:02:31

你没有说它必须是随机的。那么为什么不从某个数字开始,然后将生成的最后一个数字加 1 呢?

此方法应该为您提供至少 40 亿个唯一编号:

-(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;
}

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;
}
感情废物 2024-12-05 20:02:31

如果你有一个 NSDictionary,你可以从最后一项生成一个渐进 id:

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];

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];
能怎样 2024-12-05 20:02:31

你必须小心,特别是如果你使用增量 1 例程,如果你的应用程序被删除并在 iDevice 上重新加载,你将不再拥有保存的默认号码。它将从头开始。如果您要存储用户的分数,您可能也想保存他们的最高分数。最好检查特定日期之后秒(或毫秒)的时间例程。如果您需要这种唯一性,上面提到的 GUID 也很好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文