a problem when subclassing CNContact using swift

Hello There;

Using macOS 12.5 & Xcode 13.4 I am trying to subclass CNContact and bumping into an issue that I do not understand!

class MyContact: CNContact {
	var newVariable :String = ""
...
}

I have loaded contactArray:[CNContact] now as I iterate over this array I would like to typecast the instance of CNContact to an instance of MyContact.

for thisContact in contactArray {
	if let myK:MyContact = thisContact as? MyContact {
      //do something
   }
}

program flow simply never proceeds into the if let statement...

if I try to force this issue:

	let myK:MyContact = thisContact as! MyContact

I get the error:

Could not cast value of type 'CNContact' (0x2057a35b8) to 'XYZ.MyContact' (0x102484490).

Please help me accomplish my goal - what am I not understanding here? All help appreciated!

Steve

Post not yet marked as solved Up vote post of SwampDog Down vote post of SwampDog
696 views
  • I believe the question I need answered is how to I initialize an instance of MyContact using thisContact as the 'base' CNContact.

    The contactArray was obtained using 'cnstore.unifiedContacts(matching: , keysToFetch: ). As such this array is full of CNContacts as the API specifies...

    How do I convert an instance of CNContact to MyContact? I'm thinking that it has to do with the initializer for MyContact but I'm just not seeing how to use 'thisContact' as the object to initialize from...

Add a Comment