开发者

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

开发者 https://www.devze.com 2023-02-23 02:01 出处:网络
I\'m trying to display all the objects in this array (prefixes) with the variable sufField in a list.

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 s开发者_开发百科uch as "Expected ']' before '=' token" in the NSArray line..... and others dealing with the "for" statement.

Please help! thanks!


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号