0

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

3 Answers 3

1

First change this :

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

to :

(![[[logbookItems objectAtIndex: i] objectAtIndex: 1] isEqualToString:@""]
Sign up to request clarification or add additional context in comments.

2 Comments

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...
Actually I'd go for ([[[logbookItems objectAtIndex: i] objectAtIndex: 1] length] != 0)
1

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], ...

Comments

1

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

3 Comments

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?
It still is an NSNumber so to retrieve that int/float you stored, you need to call intValue/floatValue.
Gotcha, thanks. It's a shame you cant chose 2 answers to be correct, as both were for the different parts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.