如何生成唯一标识符?
我需要生成一些永远不会重复的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这会返回一个与 MySQL 中生成的 UUID 非常相似的唯一键。
ARC版本:
This returns a unique key very similar to UUID generated in MySQL.
ARC version:
生成 UUID 的简单版本(iOS 6 或更高版本)。
Objective-C:
Swift 3+:
它将生成类似 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:
Swift 3+:
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
如果您使用 CoreData 保存玩过的游戏,
NSManagedObject
的objectID
应该可以满足您的目的,无需任何额外的努力。If you are using CoreData to save the played games,
NSManagedObject
'sobjectID
should serve your purpose without any extra effort.您可以使用以毫秒为单位的时间或更高级的方式GUID。
You can use the time in milliseconds or a more advanced way GUID.
您可以创建 UIApplication 、 UIDevice 的类别,或者像这样(ARC 示例)
UUID () 是这个函数,
它会生成一个存储到 NSUserDefault 中的唯一标识符,以便在应用程序需要时重用 - 该标识符将与应用程序不会安装到设备上,但可用于跟踪订阅 APN 服务的设备数量等...
之后,您可以通过以下方式使用它:
You can create a category of UIApplication , UIDevice or as you prefere like this (ARC example)
UUID () is this function
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:
一个简单的时间戳(毫秒 * 10)应该可以解决问题:
A simple timestamp (milliseconds * 10) should do the trick:
你没有说它必须是随机的。那么为什么不从某个数字开始,然后将生成的最后一个数字加 1 呢?
此方法应该为您提供至少 40 亿个唯一编号:
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:
如果你有一个 NSDictionary,你可以从最后一项生成一个渐进 id:
If you have a NSDictionary, you could generate a progressive id from the last item:
你必须小心,特别是如果你使用增量 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.