0

I want to test if an NSNumber attribute of a NSManageObject Subclass has been set or not. Note, that the attribute is of NSNumber / Integer16 I have tried several approaches but none worked (these below always evaluate to false):

// Note: patient.zyklus_laenge is of NSNumber (Integer16)
// does not work
id value = patient.zyklus_laenge;
if (value == [NSNull null]) {
    self.zyklusLaengeTextField.text = [NSString stringWithFormat:@"%d", 28];
} else {
    self.zyklusLaengeTextField.text = [NSString stringWithFormat:@"%@", patient.zyklus_laenge];
}
// does not work
if (!patient.zyklus_laenge) {
    self.zyklusLaengeTextField.text = [NSString stringWithFormat:@"%d", 28];
} else {
    self.zyklusLaengeTextField.text = [NSString stringWithFormat:@"%@", patient.zyklus_laenge];
}
// does not work
if ([patient.zyklus_laenge isEqual:nil]) {
    self.zyklusLaengeTextField.text = [NSString stringWithFormat:@"%d", 28];
} else {
    self.zyklusLaengeTextField.text = [NSString stringWithFormat:@"%@", patient.zyklus_laenge];
}
// does not work
if (patient.zyklus_laenge == nil) {
    self.zyklusLaengeTextField.text = [NSString stringWithFormat:@"%d", 28];
} else {
    self.zyklusLaengeTextField.text = [NSString stringWithFormat:@"%@", patient.zyklus_laenge];
}

Can anyone enlighten me and explain me why these are not working?

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
mrd
  • 4,561
  • 10
  • 54
  • 92
  • What does `patient.zyklus_laenge` return in command line, when it's nil? – Valentin Shamardin Oct 28 '14 at 15:26
  • NSLog shows: `patient.zyklus_laenge: (null)` – mrd Oct 28 '14 at 15:27
  • Try switching "isEqual:" with "==" – Wyetro Oct 28 '14 at 15:27
  • Tried already, see the last if-else, to no avail. NSLog always shows: `patient.zyklus_laenge: (null)` but the `else` branch never executes – mrd Oct 28 '14 at 15:29
  • Not NSLog, but `po patient.zyklus_laenge` in command line. – Valentin Shamardin Oct 28 '14 at 15:31
  • this should help http://stackoverflow.com/questions/1188413/storing-optional-nsnumber-in-core-data – Flexicoder Oct 28 '14 at 15:36
  • No, this does not help in my case. Because: I am migrating from one model version to a newer. patient objects which were created before migration don't have the attribute yet. Once they migrate, the old patient objects will have `zyklus_laenge` attribute, but not set. Newly created patient objects do have the value set on creation. – mrd Oct 28 '14 at 15:49
  • Try checking if ([patient.zyklus_laenge intValue] == 0). This should return true if the value was not set before. – Mike Taverne Oct 28 '14 at 16:29
  • Did you find a solution ? I am still stuck.. –  Oct 04 '16 at 10:27

3 Answers3

0
id value = patient.zyklus_laenge;
if (value == [NSNull class]) 

to

id value = patient.zyklus_laenge;
if ([value isKindOfClass:[NSNull class]])
ekiyanov
  • 441
  • 3
  • 19
0

The solution is to set the attribute in core data as required with a default value and to test with this code:

if (!patient.zyklus_laenge) {
    self.zyklusLaengeTextField.text = [NSString stringWithFormat:@"%d", 28];
} else {
    self.zyklusLaengeTextField.text = [NSString stringWithFormat:@"%@", patient.zyklus_laenge];
}
mrd
  • 4,561
  • 10
  • 54
  • 92
0

If you have a default value set for the attribute, it will be that default value, which is normally 0, not null.

If so, you can check the following:

if (patient.zyklus_laenge.intValue == 0)

Otherwise, this null check can be used:

if (patient.zyklus_laenge != nil)

ebi
  • 4,862
  • 6
  • 29
  • 40