Joel Derfner
Joel Derfner

Reputation: 2207

unused expression

Newbie here. In the following code:

+ (NSString *)descriptionOfProgram:(id)program
{
    NSMutableArray *mutableCopyOfProgram = [program mutableCopy];
    NSString *descr = @"";
    for (int i=0; i<mutableCopyOfProgram.count; i++)
    {
        descr = [descr stringByAppendingString:(@"%@",[mutableCopyOfProgram objectAtIndex:i])];
    }
    return descr;
}

I keep getting an "expression result unused" warning on the code in the loop. But how can that be, when in the next line I return the expression result?

Upvotes: 0

Views: 455

Answers (3)

Adam
Adam

Reputation: 26917

The warning you get is because you should use stringByAppendingFormat: method instead of stringByAppendingString:. Anyway, I would recommend using NSMutableString for building a string. Also, it's better to use [mutableCopyOfProgram count] instead of mutableCopyOfProgram.count. The following code should work for you:

+ (NSString *)descriptionOfProgram:(id)program
{
    NSMutableArray *mutableCopyOfProgram = [program mutableCopy];
    NSMutableString *descr = [[NSMutableString alloc] init];
    for (int i=0; i < [mutableCopyOfProgram count]; i++)
    {
        [descr appendFormat:@"%@", [mutableCopyOfProgram objectAtIndex:i]];
    }
    return descr;
}

Upvotes: 1

InsertWittyName
InsertWittyName

Reputation: 3940

You have a few stray parenthesis () and also should be using stringByAppendingFormat:

+ (NSString *)descriptionOfProgram:(id)program
{
    NSMutableArray *mutableCopyOfProgram = [program mutableCopy];
    NSString *descr = @"";
    for (int i=0; i<mutableCopyOfProgram.count; i++)
    {
        descr = [descr stringByAppendingFormat:@"%@", [mutableCopyOfProgram objectAtIndex:i]];
    }
    return descr;
}

Upvotes: 0

John Corbett
John Corbett

Reputation: 1615

use stringByAppendingFormat: instead of stringByAppendingString:

I don't think [mutableCopyOfProgram objectAtIndex:i] is used when you use stringByAppendingString: so that would be what's unused.

a format is something like @"%@", @"a string" while a string is just @"a string", so if you're going to use a format, make sure you use the proper method.

Upvotes: 0

Related Questions