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

gonche1124

macrumors newbie
Original poster
Aug 6, 2009
27
0
I have a method that puts the words from a string into a NSMutable array, I'm using a NSscanner, but it keeps throwing me NULL, any ideas??

here is my code:
Code:
-(void)rellenarArrayConPalabras: (NSString *)texto
{
	NSString * textoTemporal =[NSString string];
	textoTemporal=texto;
	NSString * palabraTemporal=[NSString  string];
	NSScanner * nuevoscaner=[NSScanner scannerWithString:textoTemporal];
	NSCharacterSet *espacios=[NSCharacterSet whitespaceCharacterSet];
	
	while( ![nuevoscaner isAtEnd])
	{
		if ( [nuevoscaner scanUpToCharactersFromSet:espacios intoString:&palabraTemporal] )
		{
			[arrayConPalabras addObject:palabraTemporal];
		}
		NSLog(@"%@", arrayConPalabras);
	}
		
}

"arrayConPalabras" is previously defined....
 
What is the value of the parameter texto?

Does arrayConPalabras hold an NSMutableArray ref?

The following code works for me.

Code:
void test2()
{
    NSLog( @"test 2" );

	NSMutableArray* arrayConPalabras = [[[NSMutableArray alloc] init] autorelease];
	NSString* texto = @"words to   be   parsed.";

  // begin of identical code to original post.
	NSString * textoTemporal =[NSString string];
	textoTemporal=texto;
	NSString * palabraTemporal=[NSString  string];
	NSScanner * nuevoscaner=[NSScanner scannerWithString:textoTemporal];
	NSCharacterSet *espacios=[NSCharacterSet whitespaceCharacterSet];
	
	while( ![nuevoscaner isAtEnd])
	{
		if ( [nuevoscaner scanUpToCharactersFromSet:espacios intoString:&palabraTemporal] )
		{
			[arrayConPalabras addObject:palabraTemporal];
		}
		NSLog(@"%@", arrayConPalabras);
	}
 // end of identical code
}

As you can see, I've simply copied and pasted your original code, then made sure it has valid parameters and supporting values (arrayConPalabras).

The output is:
test 2
(words)
(words, to)
(words, to, be)
(words, to, be, "parsed.")

If you're serious about programming, you need to start creating and running tests like this yourself. In a real test, you'd run it using the rellenarArrayConPalabras method. I didn't do that above because I didn't want to create an entire class for one example.

The other important thing here is to Check Preconditions, which in this case means checking that texto is non-nil and contains some useful content, and also checking that arrayConPalabras is non-nil.

You're also doing some strange things by assigning [NSString string] and using extra variables that aren't needed. NSScanner doesn't modify the scanned string, so textoTemporal doesn't need to exist.
 
thanks

Thanks for the tips and the help with the code. As you can see, english is not my first language, that's why all my variables are in spanish. I do want to get serious about programming, nontheless, I'm learning as a go, I took a course a couple of semestres ago about java programmingm, but then, as my career demanded it, I got into more low level stuff (VHDL, and other things).

I would appreciate any help or pointer you give (I've already read a copy of Aaron Hilgrass book an another on objective.C programming), and tips on how to make those tests you did for make code.

I will try the code after a couple of midterms I have....and let you know.

Thank you very much

Andres
 
What is the value of the parameter texto?

Does arrayConPalabras hold an NSMutableArray ref?

The following code works for me.

Code:
void test2()
{
    NSLog( @"test 2" );

	NSMutableArray* arrayConPalabras = [[[NSMutableArray alloc] init] autorelease];
	NSString* texto = @"words to   be   parsed.";

  // begin of identical code to original post.
	NSString * textoTemporal =[NSString string];
	textoTemporal=texto;
	NSString * palabraTemporal=[NSString  string];
	NSScanner * nuevoscaner=[NSScanner scannerWithString:textoTemporal];
	NSCharacterSet *espacios=[NSCharacterSet whitespaceCharacterSet];
	
	while( ![nuevoscaner isAtEnd])
	{
		if ( [nuevoscaner scanUpToCharactersFromSet:espacios intoString:&palabraTemporal] )
		{
			[arrayConPalabras addObject:palabraTemporal];
		}
		NSLog(@"%@", arrayConPalabras);
	}
 // end of identical code
}

As you can see, I've simply copied and pasted your original code, then made sure it has valid parameters and supporting values (arrayConPalabras).

The output is:


If you're serious about programming, you need to start creating and running tests like this yourself. In a real test, you'd run it using the rellenarArrayConPalabras method. I didn't do that above because I didn't want to create an entire class for one example.

The other important thing here is to Check Preconditions, which in this case means checking that texto is non-nil and contains some useful content, and also checking that arrayConPalabras is non-nil.

You're also doing some strange things by assigning [NSString string] and using extra variables that aren't needed. NSScanner doesn't modify the scanned string, so textoTemporal doesn't need to exist.

Thanks for the help, what I did was create the "arrayConPalabras" variable and after it had created the mutableArray, I equaled it to my instance variable.....it is now working fine, but why does alway the last word of the array appears within ""?
 
Thanks for the help, what I did was create the "arrayConPalabras" variable and after it had created the mutableArray, I equaled it to my instance variable.....it is now working fine, but why does alway the last word of the array appears within ""?

Post your output. I can only guess at things I haven't seen.

As I recall, [NSArray description] quotes any item that contains punctuation, spaces, etc. That may be what you're seeing, but it's just a guess.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.