Trying to create an NSArray

Discussion in 'Mac Programming' started by mdeh, Feb 7, 2010.

  1. mdeh macrumors 6502

    Jan 3, 2009
    Hi all,
    This simple task has me stumped.

    I am trying to create an NSArray of all printable characters (in Standard English) (Up to ASCII 128 for example).

    I thought there had to be some simple methods in Cocoa for this.

    So far, have tried:

    Creating an Appropriate CharacterSet, but have not been able to covert this to an array of individual objects of the set.

    Finally, have created an:

    NSString * noSpaceString

    which contains all the characters I need, but stumped as to convert this to an Array.

    This is what I **imagined** I should be looking for, but just about every method I have tried has failed.
    NSArray * arr = [noSpaceString methodThatDividesStringIntoComponentCharacters];

    There has to be a simple answer, but so far, it has eluded me.

    Thanks for you help.
  2. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    I think you are not totally clear on what an NSArray can hold. It cannot hold a primitive type like char. It can only hold NSObjects. So you have some options:
    Store 26 NSStrings that are 1 character long
    Store the chars in an NSValue or NSData objects
    Create a new CharWrap object that has one char instance variable

    there are surely other options. Maybe explain why you want this, and we can offer better suggestions.

    You can also just use a c-style array to store primitives.

  3. mdeh thread starter macrumors 6502

    Jan 3, 2009
    Lee...I misspoke. I meant a character in the sense that each element of the array should hold a string whose length is 1, so if the string was
    then each element of the array would hold


    pretty much as you described above.

    The motivation is this.

    In Hillegass, an app called typing tutor uses an NSArray to hold strings ( of 1 character long). I thought it would be nice to have , instead of 4 strings in the example, a lot more, and that it would be even nicer ( :) ) if I could create that array using my **advanced** programming skills :)

    So, in theory, I thought of simply creating an old style (C) char array, populating the array sequentially, say from an index of 48 --> 127, then using that to create an Obj C array.
    But, ran into problems there.
    So, I figured there had to be a simple way in Obj C to do this, but again ran into a roadblock.
    Does that help?

    Lee...I think I have come up with something:

    NSMutableArray * componentsOfString = [[NSMutableArray alloc] init];
    NSString *s = [[ NSString alloc] init];
    for ( i = 0; i < l; i++)
    s = [noSpaces substringWithRange:NSMakeRange(i, 1)];
    [componentsOfString addObject:s];
    [s release];


    Probably really clumsy, but it seems to work.
  4. HiRez macrumors 603


    Jan 6, 2004
    Western US
    If don't know offhand if there's a more efficient means (Cocoa has lots of ways of overlapping convenience methods for various things), but that seems a reasonable solution, and not all that clumsy.
  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    You have two memory management errors.

    1. You own the value assigned to s outside the loop. It's never released. It should be.

    2. You do not own the value assigned to s outside the loop. It is released. It should not be.

    If the reasons are unclear at all, you should reread the memory management reference guide.
  6. mdeh thread starter macrumors 6502

    Jan 3, 2009

    thanks all.
    Good point Chown.
    Better to just declare s outside the loop, is what I meant to do.

Share This Page