Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to specify default value of attribute? #17

Closed
radianttap opened this issue Sep 28, 2023 · 5 comments
Closed

How to specify default value of attribute? #17

radianttap opened this issue Sep 28, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@radianttap
Copy link
Contributor

If I do this in @Model class,

var followers: Int = 0

I get @NSManaged property cannot have an initial value.

I see in the expanded macro that schema metadata picks this up correctly but not sure how to force the compiler to allow this.

static let schemaMetadata : [ CoreData.NSManagedObjectModel.PropertyMetadata ] = [
    .init(name: "followers", keypath: \Artist.followers,
          defaultValue: 0,
          metadata: CoreData.NSAttributeDescription(name: "followers", valueType: Int .self)),
@radianttap
Copy link
Contributor Author

Maybe some @Attribute option to specify default value, like

@Attribute(.default, 0)
var followers: Int

@helje5
Copy link
Contributor

helje5 commented Sep 28, 2023

The develop branch should actually support defaultValue now! :-)

Like: @Attribute(defaultValue: 0) var followers : Int

@helje5 helje5 added the enhancement New feature or request label Sep 28, 2023
@helje5
Copy link
Contributor

helje5 commented Sep 28, 2023

You can also set the default values in an initialiser, like:

convenience init(followers: Int = 0) {
  self.init(context: nil)
  self.followers = followers
}

@helje5
Copy link
Contributor

helje5 commented Sep 28, 2023

Issue #14 is related, it is about adding support for direct Swift initialisers, like:

var followers = 0

Which might be possible, not 100% sure yet.

@radianttap
Copy link
Contributor Author

The develop branch should actually support defaultValue now! :-)

Like: @Attribute(defaultValue: 0) var followers : Int

This is working, project now compiles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants