Ktmock13
Ktmock13

Reputation: 143

Need help with this "if" statement in Objective-C, how to display?

I'm trying to display all the objects in this array (prefixes) with the variable sufField in a list.

    if (newsPre.on = YES)
    {
        NSArray *newsArray = [NSArray AarrayWithObjects = @"News", @"Latest", @"Trending", nil;]
        for(int i=0, i<3, ++i;)
        {
            NSString *newText = [NSString stringWithFormat:@"%@", sufField, newsArray objectAtIndex: i];
            display.text=newText;
        }

    }

For example if sufField = "channel" the desired output would be...

Newschannel
Latestchannel
Trendingchannel

I'm getting a variety of build errors such as "Expected ']' before '=' token" in the NSArray line..... and others dealing with the "for" statement.

Please help! thanks!

Upvotes: 0

Views: 316

Answers (3)

Dave DeLong
Dave DeLong

Reputation: 243146

Oh man...

if (newsPre.on = YES) {

You need two equal signs there. Otherwise you're invoking a setter method. (See? Dot syntax is bad!) As it currently stands, this is saying: if ([newsPre setOn:YES]) {. What you want is either newsPre.on == YES or [newsPre on] == YES.

    NSArray *newsArray = [NSArray AarrayWithObjects = @"News", @"Latest", @"Trending", nil;]

First, the semicolon goes after the bracket. It's supposed to be the last thing on the line. Also, "AarrayWithObjects" should be "arrayWithObjects", and that extra "=" in the middle of the line should be ":".

    for(int i=0, i<3, ++i;) {

Those commas are supposed to be semi colons

        NSString *newText = [NSString stringWithFormat:@"%@", sufField, newsArray objectAtIndex: i];

You only have one substitution modifier (%@), but you're trying to substitute in 2 values. Additionally, you need brackets around "newsArray objectAtIndex:i"

        display.text=newText;

Hooray! A syntactically correct line! However, this is happening on every iteration of the loop. So every time you loop, you're changing the text of display. Are you sure that's what you want?

    }

This is OK

}

This is OK too.

In summary: learn the syntax.

Upvotes: 2

Dair
Dair

Reputation: 16240

if (newsPre.on == YES)
{
    NSArray *newsArray = [NSArray arrayWithObjects: @"News", @"Latest", @"Trending", nil];
    for(int i=0; i<3; ++i)
    {
        NSString *newText = [NSString stringWithFormat:@"%@%@", sufField, [newsArray objectAtIndex: i]];
        display.text=newText;
    }

}

This should work.

Mr. Mu forgot that newsArray objectAtIndex: i should have brackets, your string w/ format is similar to a printf statement: therefore you need tags for both objects.

Upvotes: 0

mu is too short
mu is too short

Reputation: 434645

Your for loop should look like this:

for(int i = 0; i < 3; ++i)

You should be using semicolons — not commas — to separate the three components of a for loop.

Your NSArray problem is probably because you have the semicolon statement terminator on the wrong side of the closing bracket and your message sending syntax is wrong; this:

[NSArray AarrayWithObjects = @"News", @"Latest", @"Trending", nil;]

should probably be:

[NSArray arrayWithObjects: @"News", @"Latest", @"Trending", nil];

as you are sending the arrayWithObjects message to the NSArray class.

Upvotes: 0

Related Questions