如何更有效地编写一些条件代码?

时间:2013-01-25 16:46:49

标签: iphone ios objective-c conditional-statements

什么是更好的编码方式?有一些条件代码检查reviewStarsNumber的int值,然后根据结果显示相应的uiimage。目前似乎很长,我想象它可以简化。

感谢您的帮助

  if ([reviewsStarsNumber intValue] == 0) {
    UIImage *img = [UIImage imageNamed:@"hstars-0.png"];        
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 10) {
    UIImage *img = [UIImage imageNamed:@"hstars-0_5.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 20) {
    UIImage *img = [UIImage imageNamed:@"hstars-1_0.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 30) {
    UIImage *img = [UIImage imageNamed:@"hstars-1_5.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 40) {
    UIImage *img = [UIImage imageNamed:@"hstars-2_0.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 50) {
    UIImage *img = [UIImage imageNamed:@"hstars-2_5.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 60) {
    UIImage *img = [UIImage imageNamed:@"hstars-3_0.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 70) {
    UIImage *img = [UIImage imageNamed:@"hstars-3_5.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 80) {
    UIImage *img = [UIImage imageNamed:@"hstars-4_0.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 90) {
    UIImage *img = [UIImage imageNamed:@"hstars-4_5.png"];
    [self.reviewStars setImage:img];
}
if ([reviewsStarsNumber intValue] == 100) {
    UIImage *img = [UIImage imageNamed:@"hstars-5_0.png"];
    [self.reviewStars setImage:img];
}

8 个答案:

答案 0 :(得分:5)

如果您重命名图像,则可以执行以下操作:

NSUInteger number = [reviewsStarsNumber intValue]
NSString *imageName = [NSString stringWithFormat:@"hstarts-%d.png", number];
UIImage *img = [UIImage imageNamed:imageName];
[self.reviewStars setImage:img];

答案 1 :(得分:4)

 UImage imageNamed;
 switch([reviewsStarsNumber intValue])
 {
    case 0; imageNamed = hstars0; break;
    case 10: imageNamed = hstars0-5; break;
    // 20,30...
    case 100: imageNamed = hstars5; break;

    default: break;
 }
 [self.reviewStars setImage:img];

答案 2 :(得分:2)

1.创建plist文件,例如将其命名为'hstars'。将key = 0,10,20 ......和values = hstars-0_5.png,hstars-1_5.png hstars-2_5.png

放入内部条目
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"hstars" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

2.Or

NSDictionary *dict = @{@10:@"hstars-0_5.png", @20:@"hstars-1_5.png", ....}

然后只是

UIImage *img = [UIImage imageNamed:[dict objectForKey:[NSNumber numberWithInt:reviewsStarsNumber ]]];
[self.reviewStars setImage:img];

答案 3 :(得分:1)

有几种方法,但我会说最好的选择是转换语句。

switch([reviewsStarsNumber intValue])
{
 case 0:
    UIImage *img = [UIImage imageNamed:@"hstars-0.png"];
    [self.reviewStars setImage:img];
    break;
 case 10:
    UIImage *img = [UIImage imageNamed:@"hstars-0_5.png"];
    [self.reviewStars setImage:img];
    break;
.
.
.
.
(etc..)
}

答案 4 :(得分:1)

您可以使用switch语句:

UIImage *img = NULL;
int number = [reviewsStarsNumber intValue];
switch (number)
{
    case 0:
        img = [UIImage imageNamed:@"hstars-0.png"];
        break;

    case 10:
        img = [UIImage imageNamed:@"hstars-0_5.png"];
        break;

    // ...

    default:
        // ...
        break;
}
[self.reviewStars setImage:img];

答案 5 :(得分:1)

您可以使用switch语句。

switch (expression)
{
   case match1:
        statements
        break;
    case match2:
        statements
        break;
    default:
        statements
        break;
}

或者,您可以将图片文件重命名为hstars-<0 - 100>.png并使用以下内容加载它们:

UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"hstars-%@.png", reviewsStarsNumber]];
[self.reviewStars setImage:img];

答案 6 :(得分:1)

创建一个表并在该表中查找评级:

static struct
{
    int rating;
    NSString *image;
} ratings[] =
{
   { 0, @"hstars-0.png" },
   ...
   { 100, @"hstars-5_0.png" }
};

const unsigned numRatings = sizeof(ratings) / sizeof(ratings[0]);
int rating = [reviewsStarsNumber intValue];
BOOL found = NO;
for (unsigned i = 0; i < numRatings && !found; i++)
{
    if (ratings[i].rating == rating)
    {
        UIImage *img = [UIImage imageNamed:ratings[i].image];
        [self.reviewStars setImage:img];
        found = YES;
    }
}

if (!found)
{
    // Perhaps do something if the rating wasn't found?
}

答案 7 :(得分:1)

如果您可以将第一张图片更改为hstars-0-0.png,那么此代码可以为您提供所需内容。

int value = [reviewsStarsNumber intValue] / 10

int highIndex = value / 2
int loxIndex = value % 2 == 1 ? 5 : 0

UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"hstars-%d_%d.png", highIndex, lowIndex]];