2014-06-09 129 views
-2

我有一個函數來初始化一個揹包對象的數組,而揹包對象的屬性是正數,負數和中性屬性,它們都是NSString。的問題是,當我初始化屬性的值,相同的值重複兩次(登錄時的它的值)NSString具有相同的值兩次

的代碼:

#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]]; 
               } 

              } 
             } 
+5

代碼太多!我沒有時間或傾向去閱讀它。請構建一個演示此問題的較小示例。 – trojanfoe

+0

@trojanfoe好吧 –

+0

你聲稱*相同的值重複兩次(當我記錄它的值)*,但代碼中沒有未註釋的'NSLog()'語句。我想你可以從斷點記錄數值,但是如果你提供的代碼重現了問題,那將會很好。 @trajanfoe是對的 - 你應該把代碼縮小到一個更小的例子。大部分代碼與字符串無關。 – Caleb

回答

0

看來,問題是,我的包含每個項目的屬性陣列groups.attributes了每個屬性的兩倍,因此我初始化它錯了。感謝所有幫助過我的人。