如何获取一个联系人的生日–swift iOS 9
How to fetch one contact's birthday–swift iOS 9
我对 swift 和 iOS 比较陌生,在与联系人集成时遇到了一些问题。我用过苹果的资源(https://developer.apple.com/library/prerelease/mac/documentation/Contacts/Reference/Contacts_Framework/index.html),但我不知道如何获取单个联系人的生日。我想获取用户输入的姓名并将匹配联系人的生日输出到标签。我正在使用 let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName("\(fullnamefield.text!) \(lastnamefield.text!) \(suffixfield.text!)"), keysToFetch:[CNContactBirthdayKey])
,但这会导致我无法理解的数组。
非常感谢您使用新的联系人框架在swift中查找特定联系人的生日。
数组的类型是 [CNContact]
,我相信。您需要遍历它并检索 birthday
属性,但如果您只找到一个联系人,您可以从数组中获取第一项并获取它的生日:
let store = CNContactStore()
//This line retrieves all contacts for the current name and gets the birthday and name properties
let contacts:[CNContact] = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName("\(fullnamefield.text!) \(lastnamefield.text!) \(suffixfield.text!)"), keysToFetch:[CNContactBirthdayKey, CNContactGivenNameKey])
//Get the first contact in the array of contacts (since you're only looking for 1 you don't need to loop through the contacts)
let contact = contacts[0]
//Check if the birthday field is set
if let bday = contact.birthday?.date as NSDate! {
//Use the NSDateFormatter to convert their birthday (an NSDate) to a String
let formatter = NSDateFormatter()
formatter.timeZone = NSTimeZone(name: "UTC") // You must set the time zone from your default time zone to UTC +0, which is what birthdays in Contacts are set to.
formatter.dateFormat = "dd/MM/yyyy" //Set the format of the date converter
let stringDate = formatter.stringFromDate(contact.birthday!.date!)
//Their birthday as a String:
print(stringDate)
}
我对 swift 和 iOS 比较陌生,在与联系人集成时遇到了一些问题。我用过苹果的资源(https://developer.apple.com/library/prerelease/mac/documentation/Contacts/Reference/Contacts_Framework/index.html),但我不知道如何获取单个联系人的生日。我想获取用户输入的姓名并将匹配联系人的生日输出到标签。我正在使用 let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName("\(fullnamefield.text!) \(lastnamefield.text!) \(suffixfield.text!)"), keysToFetch:[CNContactBirthdayKey])
,但这会导致我无法理解的数组。
非常感谢您使用新的联系人框架在swift中查找特定联系人的生日。
数组的类型是 [CNContact]
,我相信。您需要遍历它并检索 birthday
属性,但如果您只找到一个联系人,您可以从数组中获取第一项并获取它的生日:
let store = CNContactStore()
//This line retrieves all contacts for the current name and gets the birthday and name properties
let contacts:[CNContact] = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName("\(fullnamefield.text!) \(lastnamefield.text!) \(suffixfield.text!)"), keysToFetch:[CNContactBirthdayKey, CNContactGivenNameKey])
//Get the first contact in the array of contacts (since you're only looking for 1 you don't need to loop through the contacts)
let contact = contacts[0]
//Check if the birthday field is set
if let bday = contact.birthday?.date as NSDate! {
//Use the NSDateFormatter to convert their birthday (an NSDate) to a String
let formatter = NSDateFormatter()
formatter.timeZone = NSTimeZone(name: "UTC") // You must set the time zone from your default time zone to UTC +0, which is what birthdays in Contacts are set to.
formatter.dateFormat = "dd/MM/yyyy" //Set the format of the date converter
let stringDate = formatter.stringFromDate(contact.birthday!.date!)
//Their birthday as a String:
print(stringDate)
}