Reputation: 1204
I wanted to create an unique string with length not more than 15 characters in objective C (or with the help of Iphone SDK 3.0).
I need this for mysql table. I know mysql auto increment will do for primary key but I really need to send the unique key from Iphone Itself.Every record in my table had to have an unique key.
The Unique string should be unique and alphanumeric and max 15 characters
Thanks in Advance, Sridhar,
Upvotes: 0
Views: 4392
Reputation: 20136
There is a simple answer to this that you are missing. Each trip has a primary key composed of two numbers.
When a trip is created on the iphone it gets a unique number (ie 1,2,3,4,5).
When a trip is synchronised with the server, you give the iphoen a special unique number that goes in front, ie if iphone number is 10421, the trip numbers become 10421.1, 10421.2, etc)
This guarantees that all id's are always unique.
Upvotes: 1
Reputation: 17811
Normally, I would recommend a UUID/GUID (as NWCoder does in his link), but a UUID is 16 bytes of raw data, so it cannot be expressed in 15 bytes of data, far less in 15 alphanumeric characters.
So then the question becomes, how "unique"?
Unique to just the table would be easy, just use an incrementing value and an unsigned int or hex data.
Unique to the world would be more challenging. You could get close by generating a UUID, then hashing it with MD5, then taking as many bits of that as you can and converting it into base (10+26+26 for case sensitive, or 10+26 for case insensitive) and taking the first 15 characters.
Unique to just the user, but (as you describe later) unique in the presence of non-internet connectivity would most easily be done by simply generating a random string of 15 alphanumerics. This would likely be functionally equivalent to the UUID/MD5 method above - essentially the chance of collission would be very very small, and given the user is only going to have a handful of trips, there is essentially no chance of collissions (assuming you seed the random number generator appropriately).
Upvotes: 1