代码改变世界

swift开发笔记23 BirthDays

2017-12-29 11:31  dengchaojie_learner  阅读(257)  评论(0编辑  收藏  举报

@escaping

 

简单的介绍就是如果这个闭包是在这个函数结束前内被调用,就是非逃逸的即noescape。如果这个闭包是在函数执行完后才被调用,调用的地方超过了这函数的范围,所以叫逃逸闭包。

 

···未解决问题

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

 

closure 闭包

 

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

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

                            contacts.append(contact)

                        }

                    })

 

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."

                }