Xcode question, how would I go about doing this?

Discussion in 'iOS Programming' started by Invasion11, Jan 31, 2012.

  1. Invasion11 macrumors newbie

    Joined:
    Jul 27, 2011
    #1
    Wirelessly posted (Mozilla/5.0 (iPod; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3)

    What I'm wanting is a way a user can enter some text (example, the word "hello") and have those letters of the word be changed into numbers when a button is pressed. (hello, h=10, e=47, l=23, o=9) (final=1047239)and then a way to reverse the process.
     
  2. MattInOz, Jan 31, 2012
    Last edited: Jan 31, 2012

    MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #2
    Have a look at UITextViewDelegate.

    p.s. a more descriptive subject might attracted the keener minds.
     
  3. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
  4. MythicFrost macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #4
    I'd do something like this to convert it from "hello" into "1047239":

    Code:
    NSArray *alphabet = [[NSArray alloc] initWithObjects:@"h", @"e", @"l", @"o", nil];
    NSArray *numbers = [[NSArray alloc] initWithObjects:@"10", @"47", @"23", @"9", nil];
    
    NSString *input = @"hello"; //set to the input from the user
    NSString *newString = @"";
    for (int i = 0; i < input.length; i++) {
        NSString *letter = [input substringToIndex:i];
        letter = [letter substringFromIndex:letter.length-1];
        int index = [alphabet indexOfObjectIdenticalTo:letter];
        NSString *newValue = [number objectAtIndex:index];
        newString = [NSString stringWithFormat:@"%@%@", newString, newValue];
    }
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    I can see the encoding being doable but the decoding seems somewhat problematic. Using the example given (1047239), how would the algorithm know whether each letter comes from one or two digits?
     
  6. TheWatchfulOne macrumors 6502

    TheWatchfulOne

    Joined:
    Jun 19, 2009
    #6
    Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3)

    I can see it as being problematic, but it might be doable. Maybe start by analyzing each digit. 7, 8, and 9 can only be single digit numbers. 0 has to be the second digit in a 2 digit number. Every number greater than 2 cannot be the first digit in a 2 digit number. Maybe you can "eliminate" enough numbers to make enough of a word to find possible matches in a dictionary and make a guess. :)
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    It can't?

     
  8. TheWatchfulOne macrumors 6502

    TheWatchfulOne

    Joined:
    Jun 19, 2009
    #8
    Oh...

    A ha! I didn't read the first post closely enough. I had assumed A = 1; B = 2; ... Z= 26.:eek:

    How are the numeric values being derived for each letter? Are they arbitrary, or are these well-known codes that I'm ignorant of?

    Also, I think I'm gonna need thicker glasses.;)
     
  9. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #9
    If the Numbers are a string of number characters could you encode a zero space character to pad the single digit numbers to two characters.

    www.unicodemap.org/details/0x200B
     

Share This Page