Calculate the sum of length of arrayWithObjects

Discussion in 'Mac Programming' started by rbert, Jan 28, 2010.

  1. rbert macrumors newbie

    Joined:
    Nov 15, 2009
    #1
    Hi,

    I have an NSArray and i'd like to calculate the sum of the lenght of all previous objects with the length of current object.
    Can you please help me? Thanks!

    PHP:
    #import <Cocoa/Cocoa.h>

    int main(int argcchar *argv[])
    {
        
    NSAutoreleasePool *pool = [[NSAutoreleasePool allocinit];

        
    NSArray *myArray = [NSArray arrayWithObjects:@"One", @"Two", @"Three"nil];
        
    unsigned count;
        
    unsigned arrayCount = [myArray count];
        for (
    count 0count arrayCountcount++) {
            
    NSInteger len = [[myArray objectAtIndex:countlength];
            
    NSLog (@"thisObject: %i"len);
            
            
    // How can i get the sumlen like this:
            // sumlen #1 = 3
            // sumlen #2 = 6
            // sumlen #3 = 11
        
    }
        
        [
    pool autorelease];
        return 
    0;
    }
     
  2. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #2
    Code:
    #import <Cocoa/Cocoa.h>
    
    int main(int argc, char *argv[])
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        NSArray *myArray = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
        int total=0;
        unsigned count;
        unsigned arrayCount = [myArray count];
        for (count = 0; count < arrayCount; count++) {
            NSInteger len = [[myArray objectAtIndex:count] length];
            total+=len;
            NSLog (@"// sumlen #%d = %d",count, total);
        }
    
        [pool drain];
        return 0;
    }
    
    output:
    Code:
    2010-01-28 11:07:06.413 mrex[22071:901] // sumlen #0 = 3
    2010-01-28 11:07:06.416 mrex[22071:901] // sumlen #1 = 6
    2010-01-28 11:07:06.417 mrex[22071:901] // sumlen #2 = 11
    
    Btw, you can not autorelease an autoreleasepool. Use drain.
     
  3. rbert thread starter macrumors newbie

    Joined:
    Nov 15, 2009
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    I'm not at a mac, so this isn't tested, but:
    Code:
    #import <Cocoa/Cocoa.h>
    
    int main(int argc, char *argv[])
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        NSArray *myArray = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
        int total = 0;
        unsigned count = 0;
        for (NSString single in myArray) {
            total+=[single length];
            NSLog (@"// sumlen #%d = %d",count++, total);
        }
    
        [pool release];
        return 0;
    }
    
    If you're on 10.5 or above, you have access to Objective-C 2.0. If this is the case, fast enumeration is your friend.

    -Lee
     
  5. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    Actually, you should use release because otherwise you are left with an empty pool; you actually want to get rid of it completely.

    Further explanation: auto release pools are the one object that you can't autorelease (and when you think about it, it doesn't make sense, because when would the autorelease pool itself go away? ) I think it will crash; not that you notice it when it is the last thing before the return from main () :D
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Or you could use key-value coding :)
    Code:
    NSArray *myArray = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
    int total = [[myArray valueForKeyPath:@"@sum.length"] intValue];
    NSLog(@"total: %d", total);
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    I didn't know specifically what the OP was going for. This was my first thought, but I wasn't sure if printing a running total was important, so I kept the code doing that.

    -Lee
     
  8. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    yea, my bad. I only read the title, not the actual post :) /fail

    New Command Line Tool projects use drain by default in main(), but I guess there it doesn't make a big difference since the process is ending immediately after that. Edit: according to the docs drain is the same as release in non-GC.
     
  9. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #9
    -drain and -release are equivalent in R/R mode. Use -drain.
     
  10. rbert thread starter macrumors newbie

    Joined:
    Nov 15, 2009
    #10
    I got it with key-value, fast enumeration and autorelease.
    You help me a lots to avoid mistakes with autorelease.
    Thank you very much to all!
     

Share This Page