如何在 Swift 中从 MapKit 中随机选择餐厅名称?

How to pick random restaurant name from MapKit in Swift?

我正在搜索我所在地区附近的“美食”地点,return 值是一堆以他们的名字命名的餐馆。我如何只随机选择其中一个和 return 我的 textLabel 中的名字?现在 return 我的 textLabel.

中的所有餐厅名称
        request.region = map.region
        request.naturalLanguageQuery = "food"
        
        let search = MKLocalSearch(request: request)
        
        search.start(completionHandler: {(response, error) in
           
           if error != nil {
              print("Error occured in search: \(error!.localizedDescription)")
           } else if response!.mapItems.count == 0 {
              print("No matches found")
           } else {
              print("Matches found")
              
              for item in response!.mapItems {
                 
                 print("Name = \(String(describing: item.name))")
                 print("Phone = \(String(describing: item.phoneNumber))")
                 
                 
              
                 
                 let resturantName = SKLabelNode()
                 resturantName.position = CGPoint(x: self.size.width / 2, y: self.size.height / 3.5)
                 resturantName.fontSize = 30
                 resturantName.fontName = "PingFangHK-Semibold"
                 resturantName.text = item.name
                 resturantName.fontColor = UIColor.white
                 resturantName.zPosition = 50
                 self.addChild(resturantName)
           }
        }
     })

这段代码应该可以工作:

    search.start(completionHandler: {(response, error) in
       
       if error != nil {
          print("Error occured in search: \(error!.localizedDescription)")
       } else if response!.mapItems.count == 0 {
          print("No matches found")
       } else {
          print("Matches found")
          
          guard validResponse = response else {return}

          //Instead of looping through all of the items, pick one.
          let item = validResponse.mapItems.randomElement()
             
          print("Name = \(String(describing: item.name))")
          print("Phone = \(String(describing: item.phoneNumber))")
       }
    }