How do you set the default value of a UUID attribute in core data xcdatamodel

Viewed 2567

How do you set the default value of a UUID attribute in the core data xcdatamodel. if I set the attribute as non-optional it requires a default value. in code I'd set it using UUID() to assign a value but this doesn't work in the xcdatamodel

3 Answers

You can use a string representation of any UUID() value as a default. For example: 0CF044A3-333C-4B22-8FEB-F68B004B6C96 could be a default.

To quickly generate a new UUID a terminal could be used. Just type swift there and then import Foundation and then print(UUID()). This will give you a new random UUID.

Default UUID in CoreData

It's not necessary to assign a default value in the model.

In the class override awakeFromInsert

override func awakeFromInsert() {
    super.awakeFromInsert()
    uuid = UUID()
} 

Please satisfy the Identifiable protocol to get the UUID() work.

Related