views:

45

answers:

3

I want to copy items from an array that are not 0 or @"" (depending if NSNumber or NSString). The following code, however, does not work. What am I doing wrong?

for (int i=0; i < [logbookItems count]; i++) {
    NSLog(@"%@, %@, %@", [[[logbookItems objectAtIndex:i] objectAtIndex:1] class], [[logbookItems objectAtIndex:i] objectAtIndex:0], [[logbookItems objectAtIndex:i] objectAtIndex:1]);
    if ((([[[logbookItems objectAtIndex:i] objectAtIndex:1] isKindOfClass:[NSNumber class]]) && ([[logbookItems objectAtIndex: i] objectAtIndex: 1] > 0))
        || (([[[logbookItems objectAtIndex:i] objectAtIndex:1] isKindOfClass:[NSString class]]) && ([[logbookItems objectAtIndex: i] objectAtIndex: 1] != @""))) {
        [visibleLogbookItems addObject: [logbookItems objectAtIndex: i]];
    }
}

It produces the following log:

2010-10-01 12:46:48.301 PilotsMate[1750:207] (
21,
10,
1985,
"Test AC Mark",
"",
"",
"",
"Test Mission",
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
)
2010-10-01 12:46:52.996 PilotsMate[1750:207] NSCFNumber, Day: , 21
2010-10-01 12:46:52.997 PilotsMate[1750:207] NSCFNumber, Month: , 10
2010-10-01 12:46:52.997 PilotsMate[1750:207] NSCFNumber, Year: , 1985
2010-10-01 12:46:52.998 PilotsMate[1750:207] NSCFString, Aircraft Type: , Test AC Mark
2010-10-01 12:46:52.998 PilotsMate[1750:207] NSCFString, Airframe Number: , 
2010-10-01 12:46:52.999 PilotsMate[1750:207] NSCFString, Pilot: , 
2010-10-01 12:46:53.000 PilotsMate[1750:207] NSCFString, Crew: , 
2010-10-01 12:46:53.000 PilotsMate[1750:207] NSCFString, Mission: , Test Mission
2010-10-01 12:46:53.001 PilotsMate[1750:207] NSCFNumber, Pilot Hours (Day): , 0
2010-10-01 12:46:53.002 PilotsMate[1750:207] NSCFNumber, Crew Hours (Day): , 0
2010-10-01 12:46:53.002 PilotsMate[1750:207] NSCFNumber, Pilot Hours (Night): , 0
2010-10-01 12:46:53.003 PilotsMate[1750:207] NSCFNumber, Crew Hours (Night): , 0
2010-10-01 12:46:53.004 PilotsMate[1750:207] NSCFNumber, Simulator Hours: , 0
2010-10-01 12:46:53.005 PilotsMate[1750:207] NSCFNumber, IF Simulated: , 0
2010-10-01 12:46:53.006 PilotsMate[1750:207] NSCFNumber, IF Actual: , 0
2010-10-01 12:46:53.006 PilotsMate[1750:207] NSCFNumber, No. of SRA: , 0
2010-10-01 12:46:53.008 PilotsMate[1750:207] NSCFNumber, No. of PAR: , 0
2010-10-01 12:46:53.009 PilotsMate[1750:207] NSCFNumber, No. of ILS: , 0
2010-10-01 12:46:53.020 PilotsMate[1750:207] NSCFNumber, No. of GPS: , 0
2010-10-01 12:46:53.023 PilotsMate[1750:207] NSCFNumber, No. of SCA: , 0
2010-10-01 12:46:53.024 PilotsMate[1750:207] NSCFNumber, No. of ELVA: , 0
2010-10-01 12:46:53.025 PilotsMate[1750:207] NSCFNumber, DL (Day): , 0
2010-10-01 12:46:53.028 PilotsMate[1750:207] NSCFNumber, DL (Night - Conventional): , 0
2010-10-01 12:46:53.030 PilotsMate[1750:207] NSCFNumber, DL (Night - NVG): , 0
2010-10-01 12:46:53.030 PilotsMate[1750:207] NSCFNumber, GFP: , 0
2010-10-01 12:46:53.031 PilotsMate[1750:207] NSCFNumber, IFP: , 0
2010-10-01 12:46:53.032 PilotsMate[1750:207] NSCFNumber, NVG: , 0
2010-10-01 12:46:53.032 PilotsMate[1750:207] NSCFNumber, RNFA: , 0
2010-10-01 12:46:53.033 PilotsMate[1750:207] NSCFNumber, Instructional Hours (Day): , 0
2010-10-01 12:46:53.033 PilotsMate[1750:207] NSCFNumber, Instructional Hours (Night): , 0
+1  A: 

Comparisons of NSStrings to @"" using equality are effectively pointer comparisons. Thus, you can't just use someString != @"" to compare against the empty string. Instead, use isEqualToString: or test against a zero length.

Edit: Just for code readability, if you're using Objective C 2.0, you can replace your for loop and object retrieval by index with a foreach...

for (NSArray *logbookItem in logBookItems) {
  NSLog(NSLog(@"%@, %@, %@", [[logBookItem objectAtIndex:1] class], ...
Adam Wright
Thanks, I had forgotten that. It's now much easier to read!
churchill614
+1  A: 

First change this :

([[logbookItems objectAtIndex: i] objectAtIndex: 1] != @"")

to :

(![[[logbookItems objectAtIndex: i] objectAtIndex: 1] isEqualToString:@""]
MathieuF
Magic, I had forgotten that. I still have the problem with 0 numbers being copied into the array though. I must be doing something wrong, but I can't find what...
churchill614
Actually I'd go for `([[[logbookItems objectAtIndex: i] objectAtIndex: 1] length] != 0)`
JeremyP
+1  A: 

Same goes for the NSNumber, you need to call intValue for example on it to compare against 0.

Eric Fortin
Thanks, although if it is stored as an integer/float, as an NSNumber, why do I again need to get the integer/float value when making a comparison?
churchill614
It still is an NSNumber so to retrieve that int/float you stored, you need to call intValue/floatValue.
Eric Fortin
Gotcha, thanks. It's a shame you cant chose 2 answers to be correct, as both were for the different parts.
churchill614
You can still upvote it though. ;)
Eric Fortin