为什么这个for循环不执行?
我有一个选择器视图控制器来选择化学源和可能的浓度。如果源没有集中,它只显示一个选择器。它由一个 NSDictionary
填充,其中源类型名称为 keys
以及我制作的名为 Chemical
的自定义模型对象,该对象具有四个属性,两个 >NSString
,一个 float
和一个 BOOL
。
当我使用具有 2 个组件的字典触发此操作时,我想从所表示的 Chemical
中提取四个值。请注意,我使用前两个属性的值填充选取器,而不是 float
或 BOOL
。我遍历第一个组件中选择的键的数组,并根据键中每个 Chemical
的 chemConcentration
属性检查第二个组件中的字符串/值数组。当 chemConcentration
匹配时,我知道我拥有正确的 Chemical
并且我可以获取其属性并发回。
哇!
问题是,尽管我知道我进入了 for 循环,但它似乎被跳过了。 NSLog 就在它打印之前,但里面的却没有打印。 sourceConstant
和 sourceIsLiquid
保持 0.0
和 NO
- (IBAction)selectedSourceButton {
NSLog(@"selectedSourceButton pressed");
NSInteger sourceRow = [picker selectedRowInComponent:kSourceComponent];
NSString *selectedSource = [self.sources objectAtIndex:sourceRow];
NSArray *selectedChemicalGroup = [dictionaryOfSources objectForKey:selectedSource];
NSInteger concentrationRow = [picker selectedRowInComponent:kConcentrationComponent];
NSString *selectedConcentration = [[NSString alloc] init];
float selectedConstant = 0.0;
BOOL selectedIsLiquid = NO;
if (numberOfComponents == 2) {
NSLog(@"numberOfComponents = 2 if/then chosen"); // <-- This prints.
selectedConcentration = [self.concentrations objectAtIndex:concentrationRow];
NSLog(@"begin selectedConcentration for loop. Number of loops = %d", [selectedChemicalGroup count]); // <-- And so does this.
for (int i; i<[selectedChemicalGroup count]; i++) { // <-- But this doesn't seem to fire!
NSLog(@"selectedConcentration = %@, from selectedChemicalGroup = %@", selectedConcentration, [[selectedChemicalGroup objectAtIndex:i] chemConcentration]); // <-- Because this doesn't print.
if ([selectedConcentration isEqualToString:[[selectedChemicalGroup objectAtIndex:i] chemConcentration]]) {
selectedConstant = [[selectedChemicalGroup objectAtIndex:i] chemConstant];
selectedIsLiquid = [[selectedChemicalGroup objectAtIndex:i] chemIsLiquid];
}
}
}
else {
selectedConcentration = @"";
selectedConstant = [[selectedChemicalGroup objectAtIndex:0] chemConstant];
selectedIsLiquid = [[selectedChemicalGroup objectAtIndex:0] chemIsLiquid];
}
NSLog(@"selectedSourceButton source to return = %@, concentration = %@, sourceConstant = %1.7f, isLiquid = %d", selectedSource, selectedConcentration, selectedConstant, selectedIsLiquid);
if ([self.delegate respondsToSelector:@selector (sourcePickerViewController:didSelectSource:andConcentration:andConstant:andIsLiquid:)]) {
[self.delegate sourcePickerViewController:self didSelectSource:selectedSource andConcentration:selectedConcentration andConstant:selectedConstant andIsLiquid:selectedIsLiquid];
}
}
I have a picker view controller to select a chemical source and possibly a concentration. If the source doesn't have concentrations, it just presents a single picker. It gets populated by an NSDictionary
with source type names as keys
and a custom model object I made called Chemical
that has four properties, two NSString
, one float
and one BOOL
.
When I trigger this with dictionary that has 2 components, I want to extract the four values from the Chemical
that is represented. Note that I populate the picker with values from the first two properties, but not the float
or BOOL
. I run through the array for the key that's selected in the first component and check the string from the second component against the chemConcentration
property from each of the Chemical
s in the key/value array. When the chemConcentration
matches, I know I have the right Chemical
and I can get its properties to send back.
Whew!
The problem is that even though I know I get to the for loop, it seems to get skipped. The NSLog
right before it prints, but the one inside doesn't. sourceConstant
and sourceIsLiquid
stay 0.0
and NO
- (IBAction)selectedSourceButton {
NSLog(@"selectedSourceButton pressed");
NSInteger sourceRow = [picker selectedRowInComponent:kSourceComponent];
NSString *selectedSource = [self.sources objectAtIndex:sourceRow];
NSArray *selectedChemicalGroup = [dictionaryOfSources objectForKey:selectedSource];
NSInteger concentrationRow = [picker selectedRowInComponent:kConcentrationComponent];
NSString *selectedConcentration = [[NSString alloc] init];
float selectedConstant = 0.0;
BOOL selectedIsLiquid = NO;
if (numberOfComponents == 2) {
NSLog(@"numberOfComponents = 2 if/then chosen"); // <-- This prints.
selectedConcentration = [self.concentrations objectAtIndex:concentrationRow];
NSLog(@"begin selectedConcentration for loop. Number of loops = %d", [selectedChemicalGroup count]); // <-- And so does this.
for (int i; i<[selectedChemicalGroup count]; i++) { // <-- But this doesn't seem to fire!
NSLog(@"selectedConcentration = %@, from selectedChemicalGroup = %@", selectedConcentration, [[selectedChemicalGroup objectAtIndex:i] chemConcentration]); // <-- Because this doesn't print.
if ([selectedConcentration isEqualToString:[[selectedChemicalGroup objectAtIndex:i] chemConcentration]]) {
selectedConstant = [[selectedChemicalGroup objectAtIndex:i] chemConstant];
selectedIsLiquid = [[selectedChemicalGroup objectAtIndex:i] chemIsLiquid];
}
}
}
else {
selectedConcentration = @"";
selectedConstant = [[selectedChemicalGroup objectAtIndex:0] chemConstant];
selectedIsLiquid = [[selectedChemicalGroup objectAtIndex:0] chemIsLiquid];
}
NSLog(@"selectedSourceButton source to return = %@, concentration = %@, sourceConstant = %1.7f, isLiquid = %d", selectedSource, selectedConcentration, selectedConstant, selectedIsLiquid);
if ([self.delegate respondsToSelector:@selector (sourcePickerViewController:didSelectSource:andConcentration:andConstant:andIsLiquid:)]) {
[self.delegate sourcePickerViewController:self didSelectSource:selectedSource andConcentration:selectedConcentration andConstant:selectedConstant andIsLiquid:selectedIsLiquid];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要初始化变量
i
:for (int i = 0; ...
但有更好的方法来做到这一点,使用“快速枚举”:
You need to initialize your variable
i
:for (int i = 0; ...
But there's a better way to do this, using "fast enumeration":
初始化循环计数 i
Initialize loop count i
执行以下操作,您就会明白原因:
Do the following and you will understand why: