swift開發筆記23 BirthDays

@escapingapi

 

簡單的介紹就是若是這個閉包是在這個函數結束前內被調用,就是非逃逸的即noescape。若是這個閉包是在函數執行完後才被調用,調用的地方超過了這函數的範圍,因此叫逃逸閉包。閉包

 

···未解決問題app

NSPredicate.init(format: "birthday is nil", <#T##args: CVarArg...##CVarArg#>)函數

 

closure 閉包fetch

 

try contactStore.enumerateContacts(with: CNContactFetchRequest.init(keysToFetch: keys as! [CNKeyDescriptor]), usingBlock: { (contact, pointer) inui

                        if contact.birthday != nil && contact.birthday?.month == self.currentlySelectedMonthIndex {spa

                            contacts.append(contact)3d

                        }orm

                    })ip

 

if let homeEmail = HomeEmailTF.text {

            let homeEmail = CNLabeledValue.init(label: CNLabelHome, value: homeEmail as NSString)

            newContact.emailAddresses = [homeEmail]

        }

        let birthdayComponents = Calendar.current.dateComponents([Calendar.Component.year,Calendar.Component.month,Calendar.Component.day], from: datePick.date)

        newContact.birthday = birthdayComponents

 

let saveRe = CNSaveRequest()

            saveRe.add(newContact, toContainerWithIdentifier: nil)

            try AppDelegate.appdelegate.contactStore.execute(saveRe)

 

 

try contactStore.enumerateContacts(with: CNContactFetchRequest.init(keysToFetch: keys as! [CNKeyDescriptor]), usingBlock: { (contact, pointer) in })

 

let predicate = CNContact.predicateForContacts(matchingName: self.tfield.text!)

                let keys = [CNContactFormatter.descriptorForRequiredKeys(for: CNContactFormatterStyle.fullName),CNContactBirthdayKey,CNContactEmailAddressesKey] as [Any]

                var contacts = [CNContact]()

                var message: String!

                

                do {

                    contacts = try AppDelegate.appdelegate.contactStore.unifiedContacts(matching: predicate, keysToFetch: keys as! [CNKeyDescriptor])

                    if contacts.count == 0 {

                        message = "No contacts were found matching the given name."

                    }

                } catch {

                    message = "Unable to fetch contacts."

                }

相關文章
相關標籤/搜索