NSString具有两次相同的值

时间:2014-06-09 15:26:37

标签: ios xcode nsstring

我有一个初始化背包对象数组的函数,背包对象的属性是正,负和中性属性,它们都是NSStrings。问题是,当我初始化属性的值时,相同的值重复两次(当我记录它的值时)

代码:

#pragma mark - Creating Backpack Icons
-(NSArray *)createBackpackIcons:(NSArray *)groups usingItemGroups:(NSArray *)items
{
    NSMutableArray *backpackItems = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < _groups.count; i++) {
        Group *group = _groups[i];
        NSString *defindex1 = [NSString stringWithFormat:@"%@", group.defindex];
        for (NSInteger j = 0; j < _itemGroups.count; j++)
        {
            Item *item = _itemGroups[j];
            NSString *defindex2 = [NSString stringWithFormat:@"%@", item.defindex];
            if([defindex1 isEqualToString:defindex2])
            {
                //NSLog(@"%@", item.item_name);
                backpackIcons *backpack = [[backpackIcons alloc] init];
                backpack.pos_att = @"";
                backpack.neg_att = @"";
                backpack.neutral_att = @"";
                //name of item
                if([item.proper_name boolValue])
                {
                    backpack.name = @"The ";
                    backpack.name = [backpack.name stringByAppendingString:item.item_name];
                }
                else
                {
                    backpack.name = item.item_name;    
                }
                backpack.original_id = group.original_id; //id of the item
                backpack.defindex = item.defindex; //defindex number of the item
                backpack.level = group.level; //level of the item
                backpack.quality = group.quality; //quality of the item
                backpack.image_url = item.image_url; //image of the item
                backpack.item_description = item.item_description; //description of the item
                backpack.image_url_large = item.image_url_large; //large image of the item
                backpack.item_type = item.item_type_name; //type of item
                //find origin of item
                if(group.origin != NULL) {
                    backpack.origin = [item.originNames objectAtIndex:[group.origin integerValue]];
                }
                else {
                    backpack.origin = @"";
                }
                if([group.flag_cannot_craft boolValue])
                {
                    //item isn't craftable
                    backpack.not_craftable = @"(Not Craftable)";
                }
                if([group.flag_cannot_trade boolValue])
                {
                    //item isn't tradable
                    backpack.not_tradable = @"(Not Tradable)";
                }
                //custom name
                if(group.custom_name != NULL) {
                    backpack.custom_name = group.custom_name;
                }
                //custom description
                if(group.custom_desc != NULL) {
                    backpack.custom_desc = group.custom_desc;
                }

                //find positive and negative attributes of the item
                if(group.attributes.count != 0) {
                    for(NSInteger num = 0; num < group.attributes.count; num++) {
                        NSString *att_def = [NSString stringWithFormat:@"%@",[[group.attributes objectAtIndex:num] valueForKey:@"defindex"]];
                        //NSLog(att_def);
                        for(NSInteger num2 = 0; num2 < item.attributes.count; num2++) {
                        NSString *att_def2 = [NSString stringWithFormat:@"%@", [[item.attributes objectAtIndex:num2] valueForKey:@"defindex"]];
                        //defindex of 214 is for counting kills on strange weapons
                        if(att_def2 != NULL) {
                             if ([att_def isEqualToString:att_def2]) {
                             //we have found the attribute!
                             if(![[[item.attributes objectAtIndex:num2] valueForKey:@"hidden"] boolValue]) {
                                    //item isn't hidden from view
                                        if(![[[item.attributes objectAtIndex:num2] valueForKey:@"stored_as_integer"] boolValue]) {
                                            //we use the float value
                                            if([[NSString stringWithFormat:@"%@", [[group.attributes objectAtIndex:num] valueForKey:@"float_value"]] isEqualToString:@"1"] || [[NSString stringWithFormat:@"%@", [[group.attributes objectAtIndex:num] valueForKey:@"float_value"]] isEqualToString:@"0"]) {
                                                //the attribute is string only, it has no values
                                                NSString *att_type = [[item.attributes objectAtIndex:num2] valueForKey:@"effect_type"];
                                                NSString *attribute = [[item.attributes objectAtIndex:num2] valueForKey:@"description_string"];
                                                if([att_type isEqualToString:@"positive"]) {
                                                    backpack.pos_att = [backpack.pos_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                } else if ([att_type isEqualToString:@"negative"]) {
                                                    backpack.neg_att = [backpack.neg_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                } else {
                                                    backpack.neutral_att = [backpack.neutral_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                }
                                            }
                                            else {
                                                //the attribute uses values given my the float_value key
                                                NSString *att_type = [[item.attributes objectAtIndex:num2] valueForKey:@"effect_type"];
                                                double y = [[NSString stringWithFormat:@"%@", [[group.attributes objectAtIndex:num] valueForKey:@"float_value"]] doubleValue];
                                                NSInteger z;
                                                if([[[item.attributes objectAtIndex:num2] valueForKey:@"description_string"] rangeOfString:@"%s1%"].location != NSNotFound) {
                                                    //for values with percentages
                                                    z = lroundf(y*100);
                                                    if(z >= 100) {
                                                        z = z - 100;
                                                    }
                                                }
                                                else {
                                                    //for values that are just integers
                                                    z = y;
                                                }
                                                NSString *a = [NSString stringWithFormat:@"%d", z];
                                                NSString *attribute = [[[item.attributes objectAtIndex:num2] valueForKey:@"description_string"] stringByReplacingOccurrencesOfString:@"%s1" withString:a];
                                                //NSLog(attribute);
                                                if([att_type isEqualToString:@"positive"]) {
                                                    backpack.pos_att = [backpack.pos_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                } else if ([att_type isEqualToString:@"negative"]) {
                                                    backpack.neg_att = [backpack.neg_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                } else {
                                                    backpack.neutral_att = [backpack.neutral_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                }

                                            }
                                        }
                                        else {
                                            //we use the integer value
                                            NSString *att_type = [[item.attributes objectAtIndex:num2] valueForKey:@"effect_type"];
                                            NSInteger y = [[NSString stringWithFormat:@"%@", [[group.attributes objectAtIndex:num] valueForKey:@"value"]] integerValue];
                                            NSString *a = [NSString stringWithFormat:@"%d", y];
                                            NSString *attribute = [[[item.attributes objectAtIndex:num2] valueForKey:@"description_string"] stringByReplacingOccurrencesOfString:@"%s1" withString:a];
                                            if([att_type isEqualToString:@"positive"]) {
                                                backpack.pos_att = [backpack.pos_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                            } else if ([att_type isEqualToString:@"negative"]) {
                                                backpack.neg_att = [backpack.neg_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                            } else {
                                                backpack.neutral_att = [backpack.neutral_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                            }

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                [backpackItems addObject:backpack];
            }
        }
    }
    return backpackItems;
}

设置属性值的实际代码:

if(![[[item.attributes objectAtIndex:num2] valueForKey:@"stored_as_integer"] boolValue]) {
                                                //we use the float value
                                                if([[NSString stringWithFormat:@"%@", [[group.attributes objectAtIndex:num] valueForKey:@"float_value"]] isEqualToString:@"1"] || [[NSString stringWithFormat:@"%@", [[group.attributes objectAtIndex:num] valueForKey:@"float_value"]] isEqualToString:@"0"]) {
                                                    //the attribute is string only, it has no values
                                                    NSString *att_type = [[item.attributes objectAtIndex:num2] valueForKey:@"effect_type"];
                                                    NSString *attribute = [[item.attributes objectAtIndex:num2] valueForKey:@"description_string"];
                                                    if([att_type isEqualToString:@"positive"]) {
                                                        backpack.pos_att = [backpack.pos_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                    } else if ([att_type isEqualToString:@"negative"]) {
                                                        backpack.neg_att = [backpack.neg_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                    } else {
                                                        backpack.neutral_att = [backpack.neutral_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                    }
                                                }
                                                else {
                                                    //the attribute uses values given my the float_value key
                                                    NSString *att_type = [[item.attributes objectAtIndex:num2] valueForKey:@"effect_type"];
                                                    double y = [[NSString stringWithFormat:@"%@", [[group.attributes objectAtIndex:num] valueForKey:@"float_value"]] doubleValue];
                                                    NSInteger z;
                                                    if([[[item.attributes objectAtIndex:num2] valueForKey:@"description_string"] rangeOfString:@"%s1%"].location != NSNotFound) {
                                                        //for values with percentages
                                                        z = lroundf(y*100);
                                                        if(z >= 100) {
                                                            z = z - 100;
                                                        }
                                                    }
                                                    else {
                                                        //for values that are just integers
                                                        z = y;
                                                    }
                                                    NSString *a = [NSString stringWithFormat:@"%d", z];
                                                    NSString *attribute = [[[item.attributes objectAtIndex:num2] valueForKey:@"description_string"] stringByReplacingOccurrencesOfString:@"%s1" withString:a];
                                                    //NSLog(attribute);
                                                    if([att_type isEqualToString:@"positive"]) {
                                                        backpack.pos_att = [backpack.pos_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                    } else if ([att_type isEqualToString:@"negative"]) {
                                                        backpack.neg_att = [backpack.neg_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                    } else {
                                                        backpack.neutral_att = [backpack.neutral_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                    }

                                                }
                                            }
                                            else {
                                                //we use the integer value
                                                NSString *att_type = [[item.attributes objectAtIndex:num2] valueForKey:@"effect_type"];
                                                NSInteger y = [[NSString stringWithFormat:@"%@", [[group.attributes objectAtIndex:num] valueForKey:@"value"]] integerValue];
                                                NSString *a = [NSString stringWithFormat:@"%d", y];
                                                NSString *attribute = [[[item.attributes objectAtIndex:num2] valueForKey:@"description_string"] stringByReplacingOccurrencesOfString:@"%s1" withString:a];
                                                if([att_type isEqualToString:@"positive"]) {
                                                    backpack.pos_att = [backpack.pos_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                } else if ([att_type isEqualToString:@"negative"]) {
                                                    backpack.neg_att = [backpack.neg_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                } else {
                                                    backpack.neutral_att = [backpack.neutral_att stringByAppendingString:[NSString stringWithFormat:@"%@\n", attribute]];
                                                }

                                            }
                                        }

1 个答案:

答案 0 :(得分:0)

似乎问题是我的包含每个项目属性的数组groups.attributes每个属性都有两次,所以我初始化错了。感谢所有帮助过的人。