理解枚举

发布于 2024-11-04 13:38:24 字数 791 浏览 1 评论 0原文

有人可以填写此枚举行的其余部分吗?如果没有示例,我根本无法理解枚举。我正在尝试按最接近用户的距离排列表格视图。下面的代码是纬度部分,它尝试枚举数据 plist(索引 4 处的对象是存储为 NSNumber 的纬度,clubLocation 和 myLocation 是分别返回地点和用户位置的方法)。枚举如何工作以及它返回什么?数组?如何将数组结果与 plist 的其他部分(例如位置名称)同步?非常感谢。

-(NSArray *)distanceFromLat
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Data3" ofType:@"plist"];
    NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

    NSMutableArray *latitudeArray = [[NSMutableArray alloc] init];
    latitudeArray = [[tempDictionary objectForKey:@"Subtree"]objectAtIndex:4];

    NSEnumerator *enumLat = [latitudeArray objectEnumerator];
    id object;

    while ((object = [enumLat nextObject])) {
        [myLocation distanceFromLocation:clubLocation];
    }

}

Can someone fill in the rest of this enumeration line? I simply don't understand enumeration without an example. I'm trying to arrange a table view by distance closest to the user. The code below is the latitude portion, which is trying to enumerate through a plist of data (object at index 4 is the latitude stored as an NSNumber, clubLocation and myLocation are methods that return the location of the place and the user respectively). How does enumeration work and what does it return? An Array? How do I sync the array results with the other parts of the plist, like the name of the location? Many thanks.

-(NSArray *)distanceFromLat
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Data3" ofType:@"plist"];
    NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

    NSMutableArray *latitudeArray = [[NSMutableArray alloc] init];
    latitudeArray = [[tempDictionary objectForKey:@"Subtree"]objectAtIndex:4];

    NSEnumerator *enumLat = [latitudeArray objectEnumerator];
    id object;

    while ((object = [enumLat nextObject])) {
        [myLocation distanceFromLocation:clubLocation];
    }

}

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

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

发布评论

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

评论(1

醉酒的小男人 2024-11-11 13:38:24

为什么不使用“快速枚举”?

for (id latitude in latitudeArray) {
..
}

您可以将 id 替换为数组中纬度对象的实际类型。根据您的评论,这将是 NSNumber。

NSEnumerator 的枚举方式以同样的方式工作。循环中的每个对象都是数组中的一个对象,该数组被枚举,以便它包含的每个对象都被访问一次。 “快速枚举”更好,因为它实际上更快,并且从概念上讲很容易看出发生了什么 - “对于 y 中的所有 x”。

要让您的纬度按与用户的距离排序以供表格显示,您可以创建一个可变字典,其中包含根据与用户的距离 (NSNumber) 制成的键和一个作为该纬度索引的对象您的latitudeArray (NSNumber)。然后在该字典上使用 allKeys 来获取所有 NSNumber 键的数组,使用 sortedArrayUsingSelector:@selector(compare:) 对其进行排序,并且显示很容易 - 第一个表条目通过获取排序数组中的第一个键,获取它在字典中键的索引,然后使用该索引从latitudeArray中检索正确的纬度来获得。

why not use "fast enumeration"?

for (id latitude in latitudeArray) {
..
}

where you could replace id with the actual type of your latitude objects in the array. According to your comments that would be NSNumber.

The NSEnumerator style of enumeration works in just the same way. Each object in the loop is one object from the array, the array is enumerated so that each object it contains is accessed once. "Fast enumeration" is nicer because it is actually faster and conceptually it's easy to see what's going on - "for all x in y".

To get your latitudes sorted by distance from the user for table display you could create a mutable dictionary with a key made from the distance from the user (NSNumber) and an object which is the index of that latitude in your latitudeArray (NSNumber). Then use allKeys on that dictionary to get an array of all NSNumber keys, sort it with sortedArrayUsingSelector:@selector(compare:), and display is easy - the first table entry would be obtained by taking the first key in the sorted array, getting the index it keys in the dictionary and then using that index to retrieve the right latitude from latitudeArray.

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