I am trying to convert a legacy Objective-C function to Swift and running into some roadblocks with it. Here is the Objective-C function -
- (SHCardType)cardType:(NSString *)cardNum {
if (cardNum.length <= 0) {
return SHCTUnknown;
}
const char *s = [cardNum UTF8String];
if (sizeof(cardTypes[0]) == 0) {
return SHCTUnknown;
}
int cardsCount = sizeof(cardTypes) / sizeof(cardTypes[0]);
for (int i = 0; i < cardsCount; i++) {
int dig = log10(cardTypes[i].max) + 1;
int n = 0;
for (int j = 0; j < dig; j++) {
n = n * 10 + (s[j] - '0');
}
if ((cardTypes[i].min <= n) && (n <= cardTypes[i].max)) {
SHCardType cardType = cardTypes[i].cardType;
int numLength = 16;
if (cardType == SHCTAmericanExpress) {
// Length is 15. Should confirm its type before all card number is entered.
return cardType;
} else if (cardType == SHCTDinersClubCarteBlanche || cardType == SHCTDinersClubenRoute || cardType == SHCTDinersClubInternational || cardType == SHCTDinersClubUnitedStatesCanada) {
numLength = 14;
}
// For DinersClubCarteBlanche card, the length may be 16.
if (numLength == cardNum.length || cardType == SHCTDinersClubCarteBlanche || cardType == SHCTDinersClubInternational) {
return cardType;
}
}
}
return SHCTUnknown;
}
A couple of lines I am having trouble finding the Swift equivalent's for is
const char *s = [cardNum UTF8String];
Is there a Swift function I can use to convert a String to UTF-8
n = n * 10 + (s[j] - '0');
How do I represent the character '0' in swift. Can I use the '-' operand with it?
Any pointers would be appreciated.