Swiftpack.co - shawnclovie/SpotFlake as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by shawnclovie.
shawnclovie/SpotFlake 0.3.0
Swift version snowflake (ID generator)
⭐️ 4
🕓 2 weeks ago
iOS macOS
.package(url: "https://github.com/shawnclovie/SpotFlake.git", from: "0.3.0")


Swift version snowflake (unique ID generator).

As an application developer, you create an instance of 'SpotFlake.Node' and then generate unique ID.

Swift version

The latest version of Health works with the 4.2 version of the Swift binaries. You can download this version of the Swift binaries by following this link.


To leverage the SpotFlake package in your Swift application, you should specify a dependency for it in your Package.swift file:

import PackageDescription

let package = Package(name: "MyAwesomeSwiftProject",
    dependencies: [
      .package(url: "https://github.com/shawnclovie/SpotFlake.git", .from: "0.1.0"),

And this is how you create a SpotFlake.Node instance and generate ID:

import SpotFlake


// the index would to mark ID generated source, e.g. server ID or process ID
let nodeIndex: Int64 = 1
// the index should between 0...(-1 ^ (-1 << SpotFlake.nodeBits))
let node = SpotFlake.Node(node: nodeIndex)!


// Generate new ID
let id = node.generate()
// Get date from the ID if needed
node.time(of: id) // ID generating time
node.node(of: id) // ID generator's node index


SpotFlake use epoch as genrating base, also you can change the base epoch:

SpotFlake.epoch = 1514764800000

To calculate the epoch:

import Foundation

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd ZZZ"
print("epoch of custom date:", SpotFlake.Time(formatter.date(from: "2018-01-01 UTC")!).flakeTime)


The type of ID is Int64.

If you want to convert an ID to JSON, it should convert and pass as string, since JavaScript would loss precision for big integer.


Stars: 4
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics