Amazon.com Widgets 16 exercise 1 attempt
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 28, 2014, 08:29:40 PM
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)
| | | |-+ 16 exercise 1 attempt
Pages: [1] Go Down
Print
Author Topic: 16 exercise 1 attempt (Read 883 times)
getawaybird
Newbie
*
Posts: 4


Email




on: August 06, 2011, 04:14:04 PM

Hello,
when i build my attempt of 16.1 in Xcode it succeeds, but once i try to call it in Terminal i get this error:

-MacBook-Pro:newproject Marc$ ./newproject File copyFile progs
Segmentation fault: 11


Heres the code:


#import <Foundation/Foundation.h>


int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSFileManager *fm;
    NSString *source, *dest;
    NSProcessInfo *proc = [NSProcessInfo processInfo];
    NSArray *args = [proc arguments];
    NSUInteger i;
    BOOL isDir;
    
    fm = [NSFileManager defaultManager];
    
    if ([args count] < 3) {
        NSLog(@"Usage: %@ source1 source2 source3 ...etc directory/destination",[proc processName]);
        return 1;
    }
    
    
    
    
    NSLog(@"%@ args", [args count]);
    
    dest = [args objectAtIndex: [args count] - 1];
    
    [fm fileExistsAtPath: dest isDirectory: &isDir];
    if (isDir == NO){
        return 4;
    }
    i = 1;
    
    for (i = 1; i < [args count]; i++) {
        
        dest = [args objectAtIndex: [args count] - 1];
        source = [args objectAtIndex: i];


        if (source == dest) {
            [pool drain];
            return 5;
        }
        
        if ([fm isReadableFileAtPath: source] == NO) {
            NSLog(@"Cant read %@", source);
            return 2;
        }
        
        dest = [dest stringByAppendingPathComponent: [source lastPathComponent]];
        [fm removeItemAtPath: dest error: NULL];
        
        if ([fm copyItemAtPath:source toPath:dest error:NULL] == NO) {
            NSLog(@"Copy of %@ failed!", source);
            return 3;
          
        }
      
    }
    
    
    
    NSLog (@"Copy  succeeded!");
        
              
    [pool drain];
    return 0;
}



Any idea of whats wrong?
Thank you!
Marc

Edit: updated code but still get the same error
Edit: changed code again still same error
Last Edit: August 07, 2011, 01:46:24 PM by getawaybird Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: August 06, 2011, 10:00:23 PM

The count method returns an NSUInteger.  That's not an object, so you can't display its value with %@.  Use %li (or %lu) instead.

Cheers,

Steve
Logged
getawaybird
Newbie
*
Posts: 4


Email




Reply #2 on: August 07, 2011, 01:45:42 PM

Ahh ok thank you now it works  Cheesy
For some reason i thought that an NSInteger turns a number into an object. So whats the difference between an NSInteger and just declaring an int?

Also the first time i ran this program in got caught in some kind of loop and ended up recreating the progs directory over and over again so i ended up making somewhere around 486 files haha.
So i put this into the for loop:


        if (source == dest) {
            [pool drain];
            return 5;
        }

I'll add that to my first post as well. Thanks for the help Steve I will definitely be giving your book a good review on amazon once I'm done with it.
Thank you
Marc
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.