Objective-C:使用带有 Plist 值的 if/else 语句
我确信这确实是基本的,但我看不出我做错了什么。有人可以帮我理解我哪里出错了吗?我在 xcode 中工作。我试图根据属性列表中保存的值显示视图的不同部分。如果分配给特定 UITextField 的值等于零,那么我想隐藏该 UITextField。我正在尝试这样做。 gross 是 UITextField 的名称:
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
gross.text = [array objectAtIndex:7];
if ([array objectAtIndex:7 == 0]) {
gross.hidden = YES;
}
else {
gross.hidden = NO;
}
[array release];
我认为问题与我编写 if/else 语句的方式有关。我知道这确实很基本,但我不太明白我哪里出错了。所以非常感谢您的帮助。
I'm sure this is really basic but I can't see what I'm doing wrong. Can someone help me understand where I'm going wrong please? I'm working in xcode. I'm trying to make different parts of my view appear depending on values saved in a property list. If the value assigned to a particular UITextField is equal to zero then I want to hide that UITextField. I'm trying to do this like this. gross is the name of a UITextField:
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
gross.text = [array objectAtIndex:7];
if ([array objectAtIndex:7 == 0]) {
gross.hidden = YES;
}
else {
gross.hidden = NO;
}
[array release];
I think the problem is something to do with how I've wrote the if/else statement. I know this is really basic but I don't quite understand where I'm going wrong. So Your help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
代码应为:
这假定数组索引 7 处的对象存在并且是一个字符串。如果它实际上是一个 NSNumber,那么您应该使用条件
注释注意上面的行适用于文本包含 int 的字符串,例如
@"0"
或@"7"< /代码>。
Code should read:
This assumes that the object at index 7 of your array exists and is a string. If it's actually an NSNumber, then you should instead use the conditional
Note the above line works for a string where the text contains an int, such as
@"0"
or@"7"
.第一个错误是关闭
]
的位置。第二个是你可能在数组中有 NSString,因为你已经在 text 属性中分配了它。因此,您需要使用intValue
将其转换为int。First mistake is position of closing
]
. And second one is you probably have NSString in array, as you have assigned that in text property. So you need to convert it to int by usingintValue
.如果您的数组包含 nsstring 那么您的条件应该如下所示:
或者
如果您的数组包含 NSNumbers,第一个条件也将起作用(在您的情况下不太可能,因为您将数组元素分配给文本属性),但如果字符串不是有效数字,则第一个条件将失败 -在这种情况下,intValue 也将返回 0。
如果您确定您的元素是字符串并且您想与@“0”进行精确比较,第二个条件就可以正常工作。
Your condition is equivalent to
因为 == 运算符具有更高的优先级并且计算结果为 0。直接将数组的元素与 0 进行比较也没有意义,因为 NSArray 无论如何都不能包含 nil 对象
If your array contains nsstring then your condition should look like:
or
1st condition will work also if your array contains NSNumbers (not likely in your case as you assign array elements to text property), but will fail if string is not a valid number - in that case intValue will return 0 as well.
2nd condition will work fine if you're sure that your elements are strings and you want to compare exactly with @"0".
Your condition is equivalent to
because == operator has greater priority and evaluates to 0. Comparing array's element to 0 directly also does not make sense as NSArray cannot contain nil objects anyway
首先获取数组的长度并确保它有足够的元素,然后开始访问元素本身可能会更容易。
It might be easier to get the length of the array first and make sure that it has enough elements and then start accessing the elements themselves.