PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 6, 2022

[FIXED] how to convert a CNPhoneNumber to string in swift4?

 November 06, 2022     casting, contacts, swift     No comments   

Issue

I am using this code for getting contact number from contacts app but when I want to show the number in label I get this warning and doesn't work: Cast from 'CNPhoneNumber' to unrelated type 'String' always fails

func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
    contacts.forEach {(contact) in
        for number in contact.phoneNumbers{
            let phone = number.value
            print(phone)
            numberLabel.text = phone as! String
        }
    }
}

Solution

TRY :

if let phone = number.value as? CNPhoneNumber {
    print(phone.stringValue)
} else {
   print("number.value not of type CNPhoneNumber")
}

also take a look at CNContact , CNPhoneNumber



Answered By - PhillipJacobs
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing