Jump to content
Search Community

Booleans and XMLLoader

mindspaced test
Moderator Tag

Recommended Posts

I have the following function, which is behaving unexpectedly:

	private function _xmlCompleteHandler(event:Event):void {			
		var items:XMLList = (_xmlLoader.content as XML).item;

		for each (var item:XML in items) {
			if(item.@hasSWF){
				trace("Says true"+"::"+item.@hasSWF);
			}else{
				trace("Says false"+"::"+item.@hasSWF);
			}
			if(item.@hasSWF==true){
				trace("Says true"+"::"+item.@hasSWF);
			}else{
				trace("Says false"+"::"+item.@hasSWF);
			}
		}
	}

The results I get are:

Says true::false

Says false::false

Says true::true

Says true::true

 

Why does the first result appear contradictory? Is it related to the XMLLoader, or a syntax issue I'm unaware of?

Link to comment
Share on other sites

Yeah, that's not an error. Remember, XML content is basically text (Strings), so you cannot expect it to be something else, like a Boolean or Number. The String "false" is NOT the same thing as the Boolean value false. If there is ANY String at all, Flash will evaluate this as being true:

 

var myString:String = "false";
if (myString) {
  trace("true :: "+myString);
}

 

But you could do this instead:

 

if (Boolean(myString == "true" || myString == "1")) {
   trace("true :: "+myString);
}

 

Make sense now?

Link to comment
Share on other sites

Sure does. Thanks for the help. This was just a test case, so I realize that it could have been coded more succinctly. I was just trying to sort out why if(item.@hasSWF) was not working the way I expected. Now I know that XML content ends up being a string.

 

Thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...