如何添加字符串和数组

时间:2014-02-06 07:17:37

标签: ios objective-c

NSString *butterfly = [NSString stringWithFormat:@"brush_%d.png", i]

我希望这个字符串像这样

  1. 刷_
  2. %d.png
  3. 因为我的项目中有很多动画功能(0到39)。

    if(counter == 0) {
        NSMutableArray *dashBoy = [NSMutableArray array];
        for (i = 1; i<= 13; i++) {
            butterfly = [NSString stringWithFormat:@"brush_%d.png", i];
            if ((image = [UIImage imageNamed:butterfly]))
                [dashBoy addObject:image];
        }
    
        [stgImageView setAnimationImages:dashBoy];
        [stgImageView setAnimationDuration:2.0f];
        [stgImageView startAnimating];
    }
    
    .
    .
    
    if(counter == 39) {
        NSMutableArray *dashBoy1 = [NSMutableArray array];
        for (i = 1; i <= 30; i++) {
            butterfly = [NSString stringWithFormat:@"catch_%d.png", i];
            if ((image = [UIImage imageNamed:butterfly]))
                [dashBoy1 addObject:image];
        }
    
        [stgImageView setAnimationImages:dashBoy1];
        [stgImageView setAnimationDuration:7.20f];
        [stgImageView startAnimating];
    }
    

    我尝试了这个,但这不正确

    NSArray  *c = [NSArray arrayWithObjects:@"brush", @"catch", @"clap",@"dog", nil];
    
    //NSString *str = @"brush_";
    NSString *str = [c componentsJoinedByString:@""];
    for (int i=0; i<=3;i++)
        str = [str stringByAppendingFormat:@"_%i.png ",i];
        NSLog(@"%@",str); //Output is brushcatchclapdog_0.png _1.png _2.png _3.png 
    

    我想要

    brush_0.png, catch_1.png, clap_2.png in array
    

    这里我想在一个函数中使用for循环上面的类别。可以使用一个函数来组合上面的39个动画函数。

2 个答案:

答案 0 :(得分:0)

    [self animateWithCategory:@"brush_" WithCount:13 duration:0.2f]

-(void)animateWithCategory:(NSString *)strCategory WithCount:(CGFloat)count duration:(NSInteger)duration{
        NSMutableArray *dashBoy = [NSMutableArray array];
        for (int i = 1; i<= count; i++) {
           NSString* butterfly = [NSString stringWithFormat:@"%@%d.png",strCategory ,i];
            UIImage *image=[UIImage imageNamed:butterfly];
            [dashBoy addObject:image];
        }

        [stgImageView setAnimationImages:dashBoy];
        [stgImageView setAnimationDuration:duration];
        [stgImageView setAnimationRepeatCount:1];
        [stgImageView startAnimating];
    }

答案 1 :(得分:0)

根据您的期望输出:

  

我想要

     数组

中的

brush_0.png,catch_1.png,clap_2.png

你可以这样试试:

NSArray  *c = [NSArray arrayWithObjects:@"brush", @"catch", @"clap",@"dog", nil];
NSMutableArray  *o =  [[NSMutableArray alloc] initWithCapacity:[c count]];
//NSString *str = @"brush_";
//NSString *str = [c componentsJoinedByString:@""];
for (int i=0; i<=3;i++)
{
    NSString *str = [[c objectAtIndex:i] stringByAppendingFormat:@"_%i.png ",i];
    NSLog(@"%@",str); //Output is brushcatchclapdog_0.png _1.png _2.png _3.png

    [o addObject:str];
}//brush_0.png, catch_1.png, clap_2.png in array
NSLog(@"Output array : %@", o);

代码的记录输出:

2014-02-07 00:31:29.020 DesignMantic[807:70b] brush_0.png 
2014-02-07 00:31:29.023 DesignMantic[807:70b] catch_1.png 
2014-02-07 00:31:29.024 DesignMantic[807:70b] clap_2.png 
2014-02-07 00:31:29.025 DesignMantic[807:70b] dog_3.png 
2014-02-07 00:31:29.026 DesignMantic[807:70b] Output array : (
    "brush_0.png ",
    "catch_1.png ",
    "clap_2.png ",
    "dog_3.png "
)