PDA

View Full Version : element returning NULL




gonche1124
Sep 12, 2009, 05:50 PM
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:

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



chown33
Sep 13, 2009, 12:57 PM
What is the value of the parameter texto?

Does arrayConPalabras hold an NSMutableArray ref?

The following code works for me.

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.

gonche1124
Sep 13, 2009, 08:44 PM
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

gonche1124
Sep 15, 2009, 07:57 PM
What is the value of the parameter texto?

Does arrayConPalabras hold an NSMutableArray ref?

The following code works for me.

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 ""?

chown33
Sep 15, 2009, 08:27 PM
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.