Is kCFCompareEqualTo not available in swift?

819 Views Asked by At

I am trying to get the phone number of a person in my contact list and I am having issues tring to compare two CFStrings because I don't seem to find the kCFCompareEqualTo property available in swift

In ObjectiveC you could do something like:

ABMultiValueRef phonesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);

for (int i=0; i < ABMultiValueGetCount(phonesRef); i++) {
    CFStringRef currentPhoneLabel = ABMultiValueCopyLabelAtIndex(phonesRef, i);
    CFStringRef currentPhoneValue = ABMultiValueCopyValueAtIndex(phonesRef, i);

    if (CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo) {
        [contactInfoDict setObject:(__bridge NSString *)currentPhoneValue forKey:@"mobileNumber"];
    }

And I tried something like this:

    var phoneRef :ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
    var i = 0
    for i ; i < ABMultiValueGetCount(phoneRef) ; i++ {

        var currentPhoneLabel : CFString = ABMultiValueCopyLabelAtIndex(phoneRef, i).takeRetainedValue()
        var currentPhoneValue : CFString = ABMultiValueCopyValueAtIndex(phoneRef, i).takeRetainedValue() as CFString

        if ( CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == ???)  {

        }

    }

Can you please tell me how to fix this?

Edit: I tried this and for now it's not failing

var smth = CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel,CFStringCompareFlags.CompareCaseInsensitive) as CFComparisonResult
if  smth == CFComparisonResult.CompareEqualTo   {

}
2

There are 2 best solutions below

5
On

Try this:

if ( CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == CFComparisonResult.CompareEqualTo)  {

}

Hope this helps.. :)

0
On

Correct implementation is:

if (CFStringCompare(mediaType as CFString, kUTTypeImage, .CompareCaseInsensitive) == CFComparisonResult.CompareEqualTo ){

}