Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
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.
 
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.

-Lee
 
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.

-Lee

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
@"aeiou"
then each element of the array would hold

@"a"
@"e"
@"i"
@"o"
@"u"

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.
 
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.
 
Code:
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];
		
	}

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.

http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
 
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.

http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html


thanks all.
Good point Chown.
Better to just declare s outside the loop, is what I meant to do.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.