从数组中检索 NSNumber

发布于 2024-08-16 11:59:41 字数 679 浏览 7 评论 0原文

我对 Objective C 比较陌生,需要一些数组帮助。

我有一个 plist,其中包含一个字典和一个 NSNumber 数组,还有更多数组 稍后添加。

NSMutableDictionary *mainArray = [[NSMutableDictionary alloc]initWithContentsOfFile:filePath];

NSArray *scoresArray = [mainArray objectForKey:@"scores"];

我需要从数组中检索所有值并将它们连接到 10 个 UILabels 我已经在界面生成器中设置了。我已完成以下操作将 NSNumber 转换为字符串。

NSNumber *numberOne = [scoresArray objectAtIndex:0];  
NSUInteger  intOne = [numberOne intValue];  
NSString *stringOne = [NSString stringWithFormat:@"%d",intOne];  
scoreLabel1.text = stringOne;

这似乎是一个非常冗长的方法,我必须重复上面的 4 行十次才能检索所有数组值。我可以使用 for 循环来迭代数组,并将所有值在输出处转换为字符串吗?

任何信息将不胜感激。

I am relatively new to Objective C and need some array help.

I have a plist which contains a Dictionary and an NSNumber Array, with more arrays to
be added later on.

NSMutableDictionary *mainArray = [[NSMutableDictionary alloc]initWithContentsOfFile:filePath];

NSArray *scoresArray = [mainArray objectForKey:@"scores"];

I need to retrieve all the values from the array and connect them to 10 UILabels which
I've set up in interface builder. I've done the following to cast the NSNumber to a String.

NSNumber *numberOne = [scoresArray objectAtIndex:0];  
NSUInteger  intOne = [numberOne intValue];  
NSString *stringOne = [NSString stringWithFormat:@"%d",intOne];  
scoreLabel1.text = stringOne;

This seems a very long winded approach, I'd have to repeat the 4 lines above ten times to retrieve all the array values. Could I use a for loop to iterate through the array with all of the values converted to Strings at the output?

Any info would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

勿忘心安 2024-08-23 11:59:41
// create NSMutableArray* of score UILabel items, called "scoreLabels"
NSMutableArray *scoreLabels = [NSMutableArray arrayWithCapacity:10];
[scoreLabels addObject:scoreLabel1];
[scoreLabels addObject:scoreLabel2];
// ...

NSUInteger _index = 0;
for (NSNumber *_number in scoresArray) {
    UILabel *_label = [scoreLabels objectAtIndex:_index];
    _label.text = [NSString stringWithFormat:@"%d", [_number intValue]];
    _index++;
}

编辑

我不确定您为什么要注释掉_index++。我还没有测试过这段代码,所以也许我在某个地方遗漏了一些东西。但我没有发现 _index++ 有什么问题——这是一种非常标准的递增计数器的方法。

作为创建 scoreLabels 数组的替代方法,您确实可以检索视图控制器子视图的 tag 属性(在本例中为 UILabel您在 Interface Builder 中添加 tag 值的实例)。

假设 tag 值是可预测的 - 例如,从 scoreLabel1scoreLabel10 的每个 UILabel 都标有 >tag 等于我们在 for 循环中使用的 _index 值(0 到 9) - 那么您可以引用 UILabel 直接:

// no need to create the NSMutableArray* scoreLabels here
NSUInteger _index = 0;
for (NSNumber *_number in scoresArray) {
    UILabel *_label = (UILabel *)[self.view viewWithTag:_index];
    _label.text = [NSString stringWithFormat:@"%d", [_number intValue]];
    _index++;
}

实现这一点的关键是 tag 值对于 UILabel 必须是唯一的,并且必须是您可以使用 -viewWithTag 引用的内容:

上面的代码非常简单地假设 tag 值与 _index 值相同,但这不是必需的。 (它还假设 UILabel 实例是视图控制器的 view 属性的子视图,这取决于您在 Interface Builder 中设置界面的方式。)

有些人编写的函数添加 1000 或一些其他整数,允许您将子视图的类型分组在一起 - UILabel 实例获得 1000、1001 等,UIButton 实例获得 2000、2001 等。

// create NSMutableArray* of score UILabel items, called "scoreLabels"
NSMutableArray *scoreLabels = [NSMutableArray arrayWithCapacity:10];
[scoreLabels addObject:scoreLabel1];
[scoreLabels addObject:scoreLabel2];
// ...

NSUInteger _index = 0;
for (NSNumber *_number in scoresArray) {
    UILabel *_label = [scoreLabels objectAtIndex:_index];
    _label.text = [NSString stringWithFormat:@"%d", [_number intValue]];
    _index++;
}

EDIT

I'm not sure why you'd want to comment out _index++. I haven't tested this code, so maybe I'm missing something somewhere. But I don't see anything wrong with _index++ — that's a pretty standard way to increment a counter.

As an alternative to creating the scoreLabels array, you could indeed retrieve the tag property of the subviews of the view controller (in this case, UILabel instances that you add a tag value to in Interface Builder).

Assuming that the tag value is predictable — e.g., each UILabel from scoreLabel1 through scoreLabel10 is labeled with a tag equal to the values of _index that we use in the for loop (0 through 9) — then you could reference the UILabel directly:

// no need to create the NSMutableArray* scoreLabels here
NSUInteger _index = 0;
for (NSNumber *_number in scoresArray) {
    UILabel *_label = (UILabel *)[self.view viewWithTag:_index];
    _label.text = [NSString stringWithFormat:@"%d", [_number intValue]];
    _index++;
}

The key to making that work is that the tag value has to be unique for the UILabel and must be something you can reference with -viewWithTag:.

The code above very simply assumes that the tag values are the same as the _index values, but that isn't required. (It also assumes the UILabel instances are subviews of the view controller's view property, which will depend on how you set up your interface in Interface Builder.)

Some people write functions that add 1000 or some other integer that allows you group types of subviews together — UILabel instances get 1000, 1001, and so on, and UIButton instances would get 2000, 2001, etc.

娇柔作态 2024-08-23 11:59:41

尝试使用 stringValue...

scoreLabel1.text = [(NSNumber *)[scoresArray objectAtIndex:0] stringValue];

try using stringValue...

scoreLabel1.text = [(NSNumber *)[scoresArray objectAtIndex:0] stringValue];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文