for循环创建重复项。 iOS,Swift

时间:2017-08-12 18:32:00

标签: ios arrays dictionary for-loop

我有一个循环,它采用数组元素作为邮政编码,并将它们在for循环中更改为纬度,经度,标题和邮政编码,而不是创建和附加结果数组。在原始数组中有3个邮政编码,它用打印显示这个,但由于某种原因,循环不仅循环3次,它做了很多次,这给了我重复的数据,我已经看了删除重复,这在某种程度上工作,留下3个结果,但有时结果是好的,其中邮政编码和标题都不同,但在某些情况下,它返回与2个不同的邮政编码相同的标题。

任何人都能帮助我指出正确的方向吗?

self.places = self.pCodes

// Loop start
for eachAddress in self.places {
                                let geocoder = CLGeocoder()
                                geocoder.geocodeAddressString(eachAddress) {
                                    placemarks, error in
                                    let placemark = placemarks?.first
                                    let lat = placemark?.location?.coordinate.latitude
                                    let lon = placemark?.location?.coordinate.longitude
                                    let postPcode = eachAddress
                                    let locationTitle = self.cNames[self.lt]
                                    let latLon = ["Post Code": postPcode, "title": locationTitle, "latitude":lat!, "longitude": lon!] as [String : Any]

                                    self.posts.append(latLon)

                                 // adding 1 to lt to pick the next title in the array

                                    if self.lt < (self.pCodes.count - 1) {
                                    self.lt += 1

                                    }else {
                                        self.lt = (self.pCodes.count - 1)
                            }


                                // removing duplicates
                                    var set = Set<String>()
                                    let arraySet: [[String : Any]] = self.posts.flatMap {
                                        guard let name = $0["Post Code"] as? String else {return nil }
                                        return set.insert(name).inserted ? $0 : nil
                                    }


                                    self.postsFinal = arraySet
                                    print("Tony Places \(self.places)")
                                    print("Tony: postsFinal \(self.postsFinal)")
                                    self.showSightingsOnMap()
                        }
                    }

这是我得到的打印件。

  

Tony Places [&#34; OL8 2TT&#34;,&#34; PO1 5DS&#34;,&#34; PO1 5JA&#34;]   Tony:postsFinal [[&#34;纬度&#34;:53.514649900000002,&#34;邮政编码&#34;:&#34; OL8 2TT&#34;,&#34;标题&#34;:&#34; JDFG VBF&#34;,&#34;经度&#34;:-2.1052824000000001]]   Tony Places [&#34; OL8 2TT&#34;,&#34; PO1 5DS&#34;,&#34; PO1 5JA&#34;]   Tony:postsFinal [[&#34;纬度&#34;:53.514649900000002,&#34;邮政编码&#34;:&#34; OL8 2TT&#34;,&#34;标题&#34;:&#34; JDFG VBF&#34;,&#34;经度&#34;:-2.1052824000000001]]   Tony Places [&#34; OL8 2TT&#34;,&#34; PO1 5DS&#34;,&#34; PO1 5JA&#34;]   Tony:postsFinal [[&#34;纬度&#34;:53.514649900000002,&#34;邮政编码&#34;:&#34; OL8 2TT&#34;,&#34;标题&#34;:&#34; JDFG VBF&#34;,&#34;经度&#34;:-2.1052824000000001]]   Tony Places [&#34; OL8 2TT&#34;,&#34; PO1 5DS&#34;,&#34; PO1 5JA&#34;]   Tony:postsFinal [[&#34;纬度&#34;:53.514649900000002,&#34;邮政编码&#34;:&#34; OL8 2TT&#34;,&#34;标题&#34;:&#34; JDFG VBF&#34;,&#34;经度&#34;:-2.1052824000000001],[&#34;纬度&#34;:50.800523599999998,&#34;邮政编码&#34;:&#34; PO1 5DS&#34;, &#34; title&#34;:&#34; QWER QWE&#34;,&#34;经度&#34;:-1.0723248000000001]]   Tony Places [&#34; OL8 2TT&#34;,&#34; PO1 5DS&#34;,&#34; PO1 5JA&#34;]   Tony:postsFinal [[&#34;纬度&#34;:53.514649900000002,&#34;邮政编码&#34;:&#34; OL8 2TT&#34;,&#34;标题&#34;:&#34; JDFG VBF&#34;,&#34;经度&#34;:-2.1052824000000001],[&#34;纬度&#34;:50.800523599999998,&#34;邮政编码&#34;:&#34; PO1 5DS&#34;, &#34; title&#34;:&#34; QWER QWE&#34;,&#34;经度&#34;:-1.0723248000000001],[&#34;纬度&#34;:50.802415099999997,&#34;邮政编码& #34;:&#34; PO1 5JA&#34;,&#34; title&#34;:&#34; QWER QWE&#34;,&#34;经度&#34;:-1.0726989]]   Tony Places [&#34; OL8 2TT&#34;,&#34; PO1 5DS&#34;,&#34; PO1 5JA&#34;]   Tony:postsFinal [[&#34;纬度&#34;:53.514649900000002,&#34;邮政编码&#34;:&#34; OL8 2TT&#34;,&#34;标题&#34;:&#34; JDFG VBF&#34;,&#34;经度&#34;:-2.1052824000000001],[&#34;纬度&#34;:50.800523599999998,&#34;邮政编码&#34;:&#34; PO1 5DS&#34;, &#34; title&#34;:&#34; QWER QWE&#34;,&#34;经度&#34;:-1.0723248000000001],[&#34;纬度&#34;:50.802415099999997,&#34;邮政编码& #34;:&#34; PO1 5JA&#34;,&#34; title&#34;:&#34; QWER QWE&#34;,&#34;经度&#34;:-1.0726989]]

1 个答案:

答案 0 :(得分:0)

您在使用此代码时遇到了一些问题。为了帮助您入门,最大的问题是geocodeAddressString。这是一个异步调用,并不保证您调用它的顺序。正在处理异步调用内部的索引,这是导致对象不匹配的原因。

geocodeAddressString完成块内的代码不会按照您编码的顺序执行。让我们假设你在self.places数组中有5个元素。对于每一个,您都可以调用geocodeAddressString。

<div class="chart">
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:40% 0;">4</div>
        <div class="bar macos" style="padding:30% 0;">3</div>
    </div>
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:10% 0;">4</div>
        <div class="bar macos" style="padding:100% 0;">3</div>
    </div>
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:40% 0;">4</div>
        <div class="bar macos" style="padding:100% 0;">3</div>
    </div>
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:20% 0;">4</div>
        <div class="bar macos" style="padding:100% 0;">3</div>
    </div>
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:40% 0;">4</div>
        <div class="bar macos" style="padding:100% 0;">3</div>
    </div>
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:40% 0;">4</div>
        <div class="bar macos" style="padding:100% 0;">3</div>
    </div>
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:40% 0;">4</div>
        <div class="bar macos" style="padding:100% 0;">3</div>
    </div>
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:40% 0;">4</div>
        <div class="bar macos" style="padding:100% 0;">3</div>
    </div>
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:40% 0;">4</div>
        <div class="bar macos" style="padding:100% 0;">3</div>
    </div>
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:40% 0;">4</div>
        <div class="bar macos" style="padding:100% 0;">3</div>
    </div>
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:40% 0;">4</div>
        <div class="bar macos" style="padding:100% 0;">3</div>
    </div>
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:40% 0;">4</div>
        <div class="bar macos" style="padding:100% 0;">3</div>
    </div>
    <div class="bar-container">
    	<div class="ff3 balloon-text">12</div>
    	<img src="https://image.ibb.co/n2Njev/graph_bar_balloon.png" style="width: 35px;">
        <div class="bar windows" style="padding:50% 0;">5</div>
        <div class="bar linux" style="padding:40% 0;">4</div>
        <div class="bar macos" style="padding:100% 0;">3</div>
    </div>
</div>

geocodeAddressString执行完成块的顺序可能是这样的:

geocodeAddressString(1)
geocodeAddressString(2)
geocodeAddressString(3)
geocodeAddressString(4)
geocodeAddressString(5)

这是因为geocodeAddressString正在做一些花费一些时间的工作,可能是网络调用。一旦它具有正确的信息,它将立即返回,这意味着对其进行5次调用将使它们以不同的速率返回。把它想象成一个有5名选手的比赛。