NSString conversion question

Discussion in 'Mac Programming' started by Alkerak, Jul 20, 2011.

  1. Alkerak macrumors newbie

    Jul 20, 2011
    Is there a way to convert an NSString to an Array that holds all individual letters of the string? I am planning for example to scramble the letters.

    So I want to input a string, break it down to individual letters and scramble the letters.
    Any ideas?
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Break the problem down into parts. Solve each part. Combine.

    Part 1.
    Break an NSString into parts (substrings). Each substring should consist of one character. Refer to the NSString class reference doc and look for methods that produce substrings. Examples:
    Part 2.
    Put the string parts (substrings) into an array. Refer to the NSMutableArray class reference doc and look for methods that add objects to the array.

    Part 3.
    Scramble the array. This is left as an exercise for the reader.
    Possible search terms related to scramble: shuffle.

    Part 4.
    Combine the scrambled array parts into an NSString. This is also left as an exercise.
    You could start by looking for NSString and NSArray methods that combine parts together.
  3. PatrickCocoa macrumors 6502a

    Dec 2, 2008
    Before chown33

    I like chown33's approach, but would add one step before:

    Part 0.
    Check the Cocoa documentation (probably NSString in this case) to see if the hard working, dedicated software engineers at Apple have already solved your problem. Look at the methods in NSString or NSArray or NSMutuableArray to see if there's a arrayFromString or similarly named method.

    In this case I don't think that method exists, but when it does, yea!
  4. Alkerak thread starter macrumors newbie

    Jul 20, 2011
    Thank you for the advice.
    I think I am getting the hang of it. It seems to my solution is almost complete.
    Here is a preliminary "review" of the situation which works perfectly.
    I just need to store the strings into an Array and thats it.
    My main problem is, coming from a C background, I cannot envision this program working OOP way. I still think of it without OOP.

    #import <Foundation/Foundation.h>
    #import "IOUtility.h"
    int main (int argc, const char * argv[])
       NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
       NSString *string = ReadFromStandardInputWithNewLine(NO);
       NSUInteger intLength = [string length];
       NSInteger i = 0;
       NSRange range;
       range.length = 1;
       while (i <= (intLength - 1))
          range.location = i;
          NSString *tmp = [string substringWithRange:range];
          NSString *singleCharString = [tmp stringByAppendingFormat:@"\n"];
       [pool drain];
       return 0;
  5. gnasher729 macrumors P6


    Nov 25, 2005
    It's no problem in a tiny bit of code like this, but:

    For example, the range that you are extracting is defined in one line, then the length is defined somewhere else, then the start is defined in a third place... To find out what the substring method actually does, I need to check in three places. Can you create a single line that declares and sets "range", just before it is used?

    The variable "i" is worse. There is a dozen lines between initialization and increment. You could just write

    for (NSInteger i = 0; i < intLength; ++i) { ... }


    for (NSInteger i = 0; i < [string length]; ++i) { ... }

    That way the reader can see immediately what the range of the loop is, by checking a single statement, and not three statements that are far apart.
  6. Alkerak thread starter macrumors newbie

    Jul 20, 2011
    How would I create a single line that declares and sets the range?
    It is a structure ...
  7. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
  8. Alkerak thread starter macrumors newbie

    Jul 20, 2011
    Did it.

    Now I will concentrate on adding space detection so it will delimit scrambling on a word basis and then some user interface (text only) then Cocoa interface.

    Thanks for the help! :D

Share This Page