Objective-C:使用带有 Plist 值的 if/else 语句

发布于 2024-11-09 14:30:30 字数 477 浏览 0 评论 0原文

我确信这确实是基本的,但我看不出我做错了什么。有人可以帮我理解我哪里出错了吗?我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷爱 2024-11-16 14:30:30

代码应为:

   NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
   gross.text = [array objectAtIndex:7];

   if ([[array objectAtIndex:7] isEqualToString:@"0"]) {
        gross.hidden = YES;
   } else {
        gross.hidden = NO;
   }
   [array release];

这假定数组索引 7 处的对象存在并且是一个字符串。如果它实际上是一个 NSNumber,那么您应该使用条件

if ([[array objectAtIndex:7] intValue] == 0) {

注释注意上面的行适用于文本包含 int 的字符串,例如 @"0"@"7"< /代码>。

Code should read:

   NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
   gross.text = [array objectAtIndex:7];

   if ([[array objectAtIndex:7] isEqualToString:@"0"]) {
        gross.hidden = YES;
   } else {
        gross.hidden = NO;
   }
   [array release];

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

if ([[array objectAtIndex:7] intValue] == 0) {

Note the above line works for a string where the text contains an int, such as @"0" or @"7".

羞稚 2024-11-16 14:30:30
if ([[array objectAtIndex:7] intValue] == 0)

第一个错误是关闭 ] 的位置。第二个是你可能在数组中有 NSString,因为你已经在 text 属性中分配了它。因此,您需要使用intValue将其转换为int。

if ([[array objectAtIndex:7] intValue] == 0)

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 using intValue.

笑脸一如从前 2024-11-16 14:30:30

如果您的数组包含 nsstring 那么您的条件应该如下所示:

if ([[array objectAtIndex:7] intValue] == 0) {
...

或者

if ([[array objectAtIndex:7] isEqualToString:@"0"]) {

如果您的数组包含 NSNumbers,第一个条件也将起作用(在您的情况下不太可能,因为您将数组元素分配给文本属性),但如果字符串不是有效数字,则第一个条件将失败 -在这种情况下,intValue 也将返回 0。

如果您确定您的元素是字符串并且您想与@“0”进行精确比较,第二个条件就可以正常工作。


Your condition is equivalent to

if ([array objectAtIndex:0]) 

因为 == 运算符具有更高的优先级并且计算结果为 0。直接将数组的元素与 0 进行比较也没有意义,因为 NSArray 无论如何都不能包含 nil 对象

If your array contains nsstring then your condition should look like:

if ([[array objectAtIndex:7] intValue] == 0) {
...

or

if ([[array objectAtIndex:7] isEqualToString:@"0"]) {

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

if ([array objectAtIndex:0]) 

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

高跟鞋的旋律 2024-11-16 14:30:30

首先获取数组的长度并确保它有足够的元素,然后开始访问元素本身可能会更容易。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文