I’ve been reading the book Learn Objective C on the Mac, and I was looking at this bit of sample code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSFileManager *manager;
manager = [NSFileManager defaultManager];
NSString *home;
home = [@"~" stringByExpandingTildeInPath];
NSDirectoryEnumerator *direnum;
direnum = [manager enumeratorAtPath: home];
NSMutableArray *files;
files = [NSMutableArray arrayWithCapacity: 42];
// “Slow” enumeration
NSString *filename;
while ( filename = [direnum nextObject] )
{
if ([[filename pathExtension] isEqualTo: @”jpg”])
{
[files addObject: filename];
} // end if
} // end while
NSEnumerator *filenum;
filenum = [files objectEnumerator];
while ( filename = [filenum nextObject] )
{
NSLog(@”%@”, filename);
}
[pool drain];
return 0;
} // main
what stood out for me was while ( filename = [filenum nextObject] ) and I thought, ‘how this is actually a conditional statement?’ I suppose a call to [filenum nextObject] is an NSString with a filename, or nil, but how do we get to execute filename = while we are evaluating that? can you point me to the correct documentation that would explain more clearly just what is possible here?
This is a conditional statement since every assignation has a value, which is the value on the right hand side assigned to the var on the left.
In this case, the final value turns out to be, either a valid
NSStringobject, or nil.As long as
nilrepresents “nothing”, the condition is interpreted as false, otherwise it’s true.