Amazon.com Widgets Attempted answer to exercise 16-3-basename
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 01, 2014, 06:39:38 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 16 (Moderator: sgg_admin)
| | | |-+ Attempted answer to exercise 16-3-basename
Pages: [1] Go Down
Print
Author Topic: Attempted answer to exercise 16-3-basename (Read 943 times)
mdeh
Full Member
***
Posts: 166






on: February 18, 2009, 02:27:47 PM

Code: (Objective-C)
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSProcessInfo.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSAutoreleasePool.h>

/* basename */

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSFileManager *fm = [ NSFileManager defaultManager];
NSProcessInfo *proc = [NSProcessInfo processInfo];
NSArray *args = [proc arguments];
NSString *name, *basename, *basenameFinal;

/* check usage */

if ( args.count != 2)
{
NSLog(@"Usage:%@ basename NAME[SUFFIX]", [proc processName]);
return 1;
}


name = [args objectAtIndex: 1];
basename = [name lastPathComponent];
basenameFinal= [basename stringByDeletingPathExtension];
NSLog(@"%@", basenameFinal);
[pool drain];
    return 0;
}
Logged
jpurnell
Newbie
*
Posts: 3






Reply #1 on: February 18, 2009, 03:34:08 PM

Works for me...I suppose you could eliminate the second variable by stringing the commands, but I'm not sure if that will introduce readability problems with larger programs.

Code: (Objective-C)
	basenameInput = [args objectAtIndex:1];
basenameFinal = [[basenameInput lastPathComponent] stringByDeletingPathExtension];
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.