Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

Append result of CloudKit into array in swift

i declare the contact variable as an empty string array

  var contact = [String] ()

then I made a query to output the results from CloudKit, when I accessed the controller once, var contact succeeded in adding an array

let predicate = NSPredicate(value: true)
    let query = CKQuery(recordType: "Note", predicate: predicate)
    database.perform(query, inZoneWith: nil) { (record, error) in
        for record: CKRecord in record! {
            let name = record.value(forKeyPath: "content") as! String
            print ("There is a note "(name)"")
            self.contact.append(name)
            }
        }
  

   self.contact.append (name)

  print ("There is a note "  (name)  "")

but when accessing for the second time, the var contact becomes empty again

  print ("check all array append  (contact)")

Succes append firstime access the controller

Failed append secondtime access the controller

i use variable contact in other function

func sendSOS() {

if canSendText() {

    
   
    //compese message with google link
    let googleLink = "https://www.google.com/maps/place/" + String(myLatitude!) + "+" + String(myLongtitude!)
    let SMStext = "EMERGENCY!!, Tolong Bantu saya di lokasi Latitude: " + String(myLatitude!) + "
 Longtitude: " + String(myLongtitude!) + "  " + googleLink
    let messsageCompose = MFMessageComposeViewController()
    messsageCompose.messageComposeDelegate = self
    messsageCompose.recipients = contact;
    messsageCompose.body = SMStext
    present(messsageCompose, animated: true, completion: nil)

}else{
    // create the alert
    let alert = UIAlertController(title: "No SMS available.", message: "Please find a better location and try again!", preferredStyle: UIAlertController.Style.alert)

    // add an action (button)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))

    // show the alert
    self.present(alert, animated: true, completion: nil)
    return
}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...