NSScanner & Array Loops

549 views Asked by At

I am trying to use NSScanner is an NSArray enumeration loop. This however fails:

-[NSXMLElement length]: unrecognized selector sent to instance 0x280da30

No, I am not calling length anywhere. It seems like the value of found gets changed before the scanner completes, hence making it blank, causing the length error.

If I remove the loop and hardcode the string (i don't want that, it's just a test) the code below works. (excluding the enumeration of course)

It never gets as far as calling the NSLogs either...

Here's my code:

    for (NSString*found in arr)
    {
        NSLog(@"Found %@",found);

        NSString*search = [NSString stringWithString:found];

        NSString *separatorString = @"\"";                             
        NSString *container = nil;
        NSScanner *aScanner = [NSScanner scannerWithString:search]; 

        [aScanner setScanLocation:0];                                        
        [aScanner scanUpToString:@"src=\"" intoString:nil];          
        [aScanner scanString:@"src=\"" intoString:nil];                     
        [aScanner scanUpToString:separatorString intoString:&container]; 

        NSLog(@"scanned");

        NSLog(@"%@",container);

        NSImage*previewImage = [[NSImage alloc] initWithContentsOfFile:[relativeDir stringByAppendingPathComponent:container]];

        [preview setImage:previewImage];

        [previewImage release];

        progressed = progressed + 1;

        [convertProgress setDoubleValue:[convertProgress doubleValue] + progressed];
    }

How can I get this to work?

Edit:

Here's the input:

    NSXMLDocument*doc = [[NSXMLDocument alloc] initWithData:[NSData dataWithContentsOfFile:webPath] options:NSXMLDocumentTidyHTML error:nil];

    NSArray*arr = [doc nodesForXPath:@"//img[@src]" error:nil];
2

There are 2 answers

1
bbum On BEST ANSWER

From the documentation:

The nodesForXPath:error: method returns an array of NSXMLNode objects

Your array is not full of strings. It is full of various NSXML* objects. They aren't garbage. This is exactly how it is supposed to work.

You may not be calling length, but stringWithString: is probably calling length to figure out how much space to allocate in the new string.


Conversion to a string can mean a lot of things. An XML node may be a standalone tag, it may be some inline CDATA, it may be a tag with various attributes. As well, an XML document is highly structured. It is rarely meaningful to walk through the contents of a document as a straight enumeration of tags/contents without applying some amount of structural meaning.

You are likely going to need to recurse or otherwise walk the tags and interpret their contents appropriately.

The first step would be to remove everything but that initial NSLog() and see what you got. Then figure out how to walk the tree of nodes. Then figure out how to parse 'em appropriately.

7
paulbailey On

Are you sure that your arr contains NSString objects? The fact that NSXMLElement is throwing an error looks fishy to me.

Edit

Yep, looks like the docs say the array from nodesForXPath doesn't contain NSString objects, but NSXMLElement objects.

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSXMLNode_Class/Reference/Reference.html#//apple_ref/occ/instm/NSXMLNode/nodesForXPath:error: