String category

Discussion in 'Mac Programming' started by mdeh, Mar 13, 2010.

  1. mdeh macrumors 6502

    Joined:
    Jan 3, 2009
    #1
    Hi everyone,
    I have written this small method to help generate characters for a popUpButton. It is also to help me further my ( lack of) understanding of unichar etc :)

    So, here is the code

    Code:
    @interface NSString (characterGenerator)
    
    
    +(NSArray *) characterGenerator;
    
    @end
    
    @implementation NSString (characterGenerator)
    
    +(NSArray *) characterGenerator
    {
    	unichar i;
    	//uint i;
    	NSArray *arr = [NSArray array];
    	unichar array[UPPERLIMIT];
    	NSString *s = nil;
    	
    	for ( i = 0; i < UPPERLIMIT; i++)  // UPPERLIMIT = 256
    	{
    		array[0] = i;
    		array[1] = 'U+0000';
    		s = [NSString localizedStringWithFormat:@"index: %hi character: %S", i, arr[0]];
    		arr = [arr arrayByAddingObject:s];
    		NSLog(@"index: %hi   character: %@", i, [arr lastObject]);
    	}
    	
    	return arr;
    }
    
    @end
    
    current output: character: index: 243 character: 酐烚翿 // repeated for each index.

    Any insight will be greatly appreciated.
    Thank you.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    The two fragments of red-hilited code are wrong. Neither one is doing what you seem to think it's doing.

    You should enable the compiler option that treats all warnings as errors. These both generate a warning when compiled, and it looks like you need the extra quality-assurance of not being able to ignore warnings.

    And if you can't think of a better way to build up an array, you should look at NSMutableArray.

    ADDED:
    Code:
    #import <Foundation/Foundation.h>
    
    #define UPPERLIMIT	256
    
    int main (int argc, const char * argv[]) 
    {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    	
    	unichar i;
    	unichar array[1];  // only holds 1 unichar 
    	
    	for ( i = 0; i < UPPERLIMIT; i++)  // UPPERLIMIT = 256
    	{
    		array[0] = i;
    
    		NSString *ss = [NSString stringWithCharacters:array length:1];
    
    		NSLog(@"index: %hi   character: %@", i, ss);
    	}
    		
        [pool drain];
        return 0;
    }
    
    Example output:
    Code:
    2010-03-13 15:11:52.473 a.out[25105] index: 250   character: ú
    2010-03-13 15:11:52.473 a.out[25105] index: 251   character: û
    2010-03-13 15:11:52.473 a.out[25105] index: 252   character: ü
    2010-03-13 15:11:52.473 a.out[25105] index: 253   character: ý
    
     
  3. mdeh thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #3
    Thanks Chown. As always, insightful! :)

    I usually **DO** pay close attention to the warnings, but somehow those got away from me. Looking more closely at your code, ( which works beautifully) I altered my code to this, which now gives the result that I had hoped for in the first place.

    Code:
    +(NSArray *) characterGenerator
    {
    	unichar i;
    	//uint i;
    	NSArray *arr = [NSArray array];
    	unichar array[10];
    	NSString *s = nil;
    	
    	for ( i = 0; i < UPPERLIMIT; i++)  // UPPERLIMIT = 256
    	{
    		array[0] = i;
    		array[1] = '\0';
    		s = [NSString localizedStringWithFormat:@"index: %hi character: %S", i, array];
    		//s = [NSString stringWithCharacters:array length:1];
    		arr = [arr arrayByAddingObject:s];
    		NSLog(@"index: %hi   character: %@", i, [arr lastObject]);//  to check code
    	}
    	
    	return arr;
    }
    
    **HAD** I paid closer attention to the warnings, I might have figured out that I was passing the element of the first array (array[0]) and not the pointer to the array as intended. It seems that the simple old NULL of '\0' is accepted as a NULL in unicode, so I am curious as to how one would assign a NULL in true unicode fashion?

    Thanks as always for taking the time to answer.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    It's unclear exactly what you mean by "true unicode fashion".

    The typedef involved is 'unichar', not 'unicode'. It represents a single UTF-16 code point. Unicode is a character set, not a specific representation of a character set. For example, UTF-8 is "true Unicode" as much as UTF-16 is.

    One way to represent U+0000 as the unichar type is ((unichar)0). I.e. the binary value zero, typecast to unichar.

    Note that this is NOT technically the same as the wchar_t representation of U+0000, which might be L'\0' (wild-ass guess because I'm too lazy to look it up). One could also use (L""[0]) as the wchar_t representation of the nul terminator (i.e. the first wchar_t in the wide-char array representing the empty string). Or use ((wchar_t)0).
     
  5. mdeh thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #5
    Thanks Chown for your (always) informative insights. They are much appreciated, as is your willingness to help out others whenever you can. That's what makes amateurs like me stick to programming.
     

Share This Page