有条件地从另一个数组填充数组 - 目标C.

时间:2010-10-01 11:52:37

标签: objective-c nsmutablearray nsarray

我想从数组中复制非0或@“”的项(取决于NSNumber或NSString)。但是,以下代码不起作用。我做错了什么?

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]];
    }
}

它产生以下日志:

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 个答案:

答案 0 :(得分:1)

使用相等性将NSStrings与@""进行比较实际上是指针比较。因此,您不能只使用someString != @""来比较空字符串。相反,请使用isEqualToString:或针对零length进行测试。

编辑:只是为了代码可读性,如果你使用的是Objective C 2.0,你可以用foreach替换你的for循环和对象检索......

for (NSArray *logbookItem in logBookItems) {
  NSLog(NSLog(@"%@, %@, %@", [[logBookItem objectAtIndex:1] class], ...

答案 1 :(得分:1)

首先改变这个:

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

到:

(![[[logbookItems objectAtIndex: i] objectAtIndex: 1] isEqualToString:@""]

答案 2 :(得分:1)

同样适用于NSNumber,你需要在其上调用intValue来与0进行比较。