Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rbert

macrumors newbie
Original poster
Nov 15, 2009
12
0
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 argc, char *argv[])
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	NSArray *myArray = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
	unsigned count;
	unsigned arrayCount = [myArray count];
	for (count = 0; count < arrayCount; count++) {
		NSInteger len = [[myArray objectAtIndex:count] length];
		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;
}
 
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.
 
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
 
Btw, you can not autorelease an autoreleasepool. Use drain.

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
 
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);
 
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);

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
 
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

yea, my bad. I only read the title, not the actual post :) /fail

Actually, you should use release because otherwise you are left with an empty pool; you actually want to get rid of it completely.

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.
 
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.