Skip to content

Instantly share code, notes, and snippets.

@ryo-takahashi
Last active March 18, 2020 01:33
Show Gist options
  • Save ryo-takahashi/9418ff8813c560be5461ad3ca17e2377 to your computer and use it in GitHub Desktop.
Save ryo-takahashi/9418ff8813c560be5461ad3ca17e2377 to your computer and use it in GitHub Desktop.
7桁の郵便番号数字をランダムに生成し、日本の実在住所が取得できるまで再帰的に実行する
import CoreLocation
let geoCoder = CLGeocoder()
var challangeCount = 0
func recursiveFetchJapanLocation(completion: (() -> ())?) {
let randomAddress = Int.random(in: 0..<9999999)
let randomAddressString = String(format: "%07d", randomAddress)
challangeCount += 1
print("チャレンジ!! \(randomAddressString) \(challangeCount)")
geoCoder.geocodeAddressString("\(randomAddress)") { (placemarks, error) in
if error != nil {
recursiveFetchJapanLocation(completion: completion)
return
}
print(placemarks?.first)
completion?()
}
}
recursiveFetchJapanLocation(completion: {
print("終わったよ。")
})
@ryo-takahashi
Copy link
Author

チャレンジ!! 0331571 5403
チャレンジ!! 7169614 5404
チャレンジ!! 9669716 5405
チャレンジ!! 5168558 5406
Optional(516-8558, Ise-Shi, Mie, Japan 516-8558 @ <+34.46278510,+136.72252010> +/- 100.00m, region CLCircularRegion (identifier:'<+34.46278510,+136.72252010> radius 70.63', center:<+34.46278510,+136.72252010>, radius:70.63m))
終わったよ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment