Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...