Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Unique Int id from String

I am using Parse which has an preload User table in the database. I want from each user a unique userId (Int). Parse's objectId is unique but not an Int and username is a String. Username is unique for each user , so can I somehow convert each username into a number ?

I tried .toInt() , Int() but I got nothing.

WHY :
I have an existing table with user's ratings (movies) and I want to extent this table with more ratings. The userId field is a Number value so I must keep it this way.

like image 488
PoolHallJunkie Avatar asked Apr 06 '26 12:04

PoolHallJunkie


1 Answers

Swift String has a hash property. It also conforms to the Hashable protocol. Maybe you can use that.

However, hashValue has the following comment:

Axiom:x == y implies x.hashValue == y.hashValue.

Note: The hash value is not guaranteed to be stable across different invocations of the same program. Do not persist the hash value across program runs.

so, use carefully...

Note: as stated in the comments, the hashValue is not guaranteed to be unique, but collisions should be rare, so it may be a solution anyway.

like image 84
Marcos Crispino Avatar answered Apr 09 '26 04:04

Marcos Crispino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!