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

Swift default value of nil with optional flag not recognized, fails #1024

Closed
javierjulio opened this issue Oct 15, 2014 · 6 comments
Closed

Comments

@javierjulio
Copy link

If I have:

class FlashCard: RLMObject {
  dynamic var lastReviewedAt: NSDate? = nil
}

And then in a test case if I do the following:

  func test() {
    let realm = realmWithTestPath()

    let firstFlashCard = FlashCard()
    firstFlashCard.definition = "first"

    realm.transactionWithBlock() {
      realm.addObject(firstFlashCard)
    }
  }

I get the following error which is unexpected: failed: caught "RLMException", "No value or default value specified for property 'lastReviewedAt' in 'FlashCard'" since I set the value to nil and expect it at some points that it can be nil. Am I doing something wrong or is this default value not supported? Do I have to instantiate the FlashCard object within the block?

@javierjulio
Copy link
Author

If I create the FlashCard instance within the transaction block nothing changes. I still receive the same error. Is it not possible to allow a default value of nil for a property? In this case not having a date set is important since it indicates that object hasn't been reviewed yet so I won't to allow for that case.

@jpsim
Copy link
Contributor

jpsim commented Oct 15, 2014

Optional properties aren't really supported in Realm Swift. They are, but only if the property has a default value and the property is never set to nil. This is a duplicate of #628.

Relationships can be nil though. i.e. dynamic var ceo: Employee?

@GreatApe
Copy link
Contributor

Optional properties are not supported yet, except for RLMObject properties. It is very high on the list, though. But what you can do is to wrap the date in an RLMObject subclass, and let that property be optional:

class DateObject: RLMObject {
  dynamic var date: NSDate = ...
}
class FlashCard: RLMObject {
  dynamic var lastReviewedAt: DateObject?
}

@javierjulio
Copy link
Author

Thanks @jpsim and @GreatApe! This actually won't be much of a problem as I can work around it by using other properties on the object. Allowing nil would have simplified the queries but testing against other properties is just as clear so this isn't a huge deal. Also in fact its better, rather not have to worry about nils. Closing as a duplicate of #628.

b00gizm added a commit to b00gizm/Kjuu that referenced this issue Feb 7, 2015
@scottfister
Copy link

I am attempting to do this with a float or double and I get the error 'NSUnknownKeyException', reason: '[<__NSCFNumber 0x7fe509f350e0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key float.'.

My code:

class NullableFloat: Object {
    dynamic var float: Float = 0.0
}
class Foo: Object {
  dynamic var bar : NullableFloat?
}

What am I doing wrong?

@jpsim
Copy link
Contributor

jpsim commented Jun 12, 2015

@scottfister could you please open a new issue with step-by-step instructions to reproduce the issue you're seeing? Thanks!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants