Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save juanarzola/ca49a319ddb3ccfbcb7f425a5e300775 to your computer and use it in GitHub Desktop.
Save juanarzola/ca49a319ddb3ccfbcb7f425a5e300775 to your computer and use it in GitHub Desktop.
A super simple hack to get a short description of SwiftData's PersistentIdentifier suitable for debugging. Format: `{ModelName}/p{Number}`
import SwiftData
extension PersistentIdentifier {
var shortDebugDescription: String {
let description = "\(self)"
let entityNameRange = description.firstMatch(of: self.entityName)?.range
let lastParenthesisRange = description.firstMatch(of: ")")?.range
if let entityNameRange, let lastParenthesisRange,
entityNameRange.lowerBound < lastParenthesisRange.upperBound {
return String(description[entityNameRange.lowerBound..<lastParenthesisRange.lowerBound])
}
return description
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment