从联系Swift获取街道地址
发布时间:2020-12-14 04:35:55 所属栏目:百科 来源:网络整理
导读:我创建了一个应用程序,用户可以选择联系人然后获取姓名,生日和街道地址. 我能得到显示名称和生日.但我不能得到街道地址 这是我的代码 @IBOutlet weak var names: UILabel!@IBOutlet weak var birthday: UILabel!@IBOutlet weak var address: UILabel!var sto
我创建了一个应用程序,用户可以选择联系人然后获取姓名,生日和街道地址.
我能得到&显示名称和生日.但我不能得到街道地址 这是我的代码 @IBOutlet weak var names: UILabel! @IBOutlet weak var birthday: UILabel! @IBOutlet weak var address: UILabel! var storeNames = "" var storeBirthday = "" var storeAddress = CNLabeledValue() override func viewWillAppear(animated: Bool) { super.viewWillAppear(true) names.text = storeNames birthday.text = storeBirthday address.text = String(storeAddress) } func getDateStringFromComponents(dateComponents: NSDateComponents) -> String! { if let date = NSCalendar.currentCalendar().dateFromComponents(dateComponents) { let dateFormatter = NSDateFormatter() dateFormatter.locale = NSLocale.currentLocale() dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle let dateString = dateFormatter.stringFromDate(date) return dateString } return nil } @IBAction func showContacts(sender: AnyObject) { let contactPickerViewController = CNContactPickerViewController() contactPickerViewController.predicateForEnablingContact = NSPredicate(format: "birthday != nil") contactPickerViewController.delegate = self presentViewController(contactPickerViewController,animated: true,completion: nil) } func contactPicker(picker: CNContactPickerViewController,didSelectContact contact: CNContact) { storeNames = contact.givenName //Get Names if let bday = contact.birthday?.date as NSDate! { let formatter = NSDateFormatter() formatter.timeZone = NSTimeZone(name: "UTC") formatter.dateFormat = "dd/MM/yyyy" let stringDate = formatter.stringFromDate(contact.birthday!.date!) //Their birthday as a String: storeBirthday = stringDate //Get Birthday } storeAddress = contact.postalAddresses[0] print(storeAddress) } 它打印像这样 < CNLabeledValue:0x7fca63c12780:identifier = 150C06A9-8008-4496-B27A-CE2648622793,label = _ $!< Work>!$_,value =< CNPostalAddress:0x7fca63c0dce0:street = 165 Davis Street,city = Hillsborough,state = CA,postalCode = 94010,country =,countryCode = us,formattedAddress =(null)>> 对不起,我不知道如何获得街道地址 解决方法
您必须转换storeAddress的值,它是一个CNPostalAddress对象,具有您需要的属性
if let address = storeAddress.value as? CNPostalAddress { let city = address.city ... } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |