1

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
tbag
  • 1,268
  • 2
  • 16
  • 34
  • Have you tried the automatic conversion offered in Xcode 7? – Aidan Gomez Sep 29 '15 at 16:51
  • @AidanGomez This tool only converts Swift 1 to Swift 2, it doesn't translate Objective-C... – Eric Aya Sep 29 '15 at 16:53
  • well there are some web tools ( like http://iswift.org/try ), question also is, why to do it in first place as you can call Objective-C code from swift very easily ... – PetrV Sep 29 '15 at 16:54
  • If you're going to port over, then check out https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/Migration.html and make sure you're conforming to the guide's approach. – Aidan Gomez Sep 29 '15 at 17:00
  • 2
    Please ask about one specific item per question; that way someone else who has the same issue can easily find your question and the answer they need. – jscs Sep 29 '15 at 18:03

3 Answers3

4

Try this:

let s = cardNum.cStringUsingEncoding(NSUTF8StringEncoding)! // s is an Array<Int8>
...
let CHAR_ZERO : Int8 = 48
n = n * 10 + (s[j] - CHAR_ZERO);

It's cumbersome to convert a character to its integer value in the ASCII table in Swift. So you may as well define it as an integer constant.

Edit: if you want a safer way to do the unwrapping:

let s : Array<Int8>
if cardNum.canBeConvertedToEncoding(NSUTF8StringEncoding) {
    s = cardNum.cStringUsingEncoding(NSUTF8StringEncoding)!
} else {
    s = []
    // Handle error
}
Code Different
  • 90,614
  • 16
  • 144
  • 163
1

From How can I get the Unicode code point(s) of a Character?

let ch: Character = "A"
let s = String(ch).unicodeScalars
s[s.startIndex].value // returns 65

So perhaps try:

let z = "0".unicodeScalars
s[j] - z[z.startIndex].value

Let me know how it goes.

Community
  • 1
  • 1
Aidan Gomez
  • 8,167
  • 5
  • 28
  • 51
1

im not sure but, you can try this:

func cardType(cardNum: String) -> SHCardType {
    if cardNum.length <= 0 {
        return SHCTUnknown
    }
    let s: Character = cardNum.UTF8String()
    if sizeof([0]) == 0 {
        return SHCTUnknown
    }
    var cardsCount: Int = sizeof() / sizeof([0])
    for var i = 0; i < cardsCount; i++ {
        var dig: Int = log10(cardTypes[i].max) + 1
        var n: Int = 0
        for var j = 0; j < dig; j++ {
            n = n * 10 + (s[j] - "0")
        }
        if (cardTypes[i].min <= n) && (n <= cardTypes[i].max) {
            var cardType: SHCardType = cardTypes[i].cardType
            var numLength: Int = 16
            if cardType == SHCTAmericanExpress {
                return cardType
            }
            else {
                if cardType == SHCTDinersClubCarteBlanche || cardType == SHCTDinersClubenRoute || cardType == SHCTDinersClubInternational || cardType == SHCTDinersClubUnitedStatesCanada {
                    numLength = 14
                }
            }
            if numLength == cardNum.length || cardType == SHCTDinersClubCarteBlanche || cardType == SHCTDinersClubInternational {
                return cardType
            }
        }
    }
    return SHCTUnknown
}
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109