如何使用目标c重新排列数组?

时间:2015-08-12 00:42:50

标签: ios objective-c

我是目标c的新手。我有阵列请看样品。我需要     从1,2,3重新排列到2,3,1。谢谢你们。

        "M 90.5, 88", <- please pay attention to this
        " C",
        "84.083333373069763, 96.083333253860474", <- 1;
        "87.5, 90.333333253860474", <- 2;
        "84.5, 92.666666507720947", <- 3;
        "M 171, 204", <- please pay attention to this
        " C",
        "161, 199.50000002980232", <- 1;
        "168, 202.33333337306976", <- 2;
        "165, 200.66666674613953", <- 3;
        " C",
        "153.16666674613953, 215.25", <- 1;
        "148.5, 202.66666650772095", <- 2;
        "150, 208.33333301544189", <- 3;
        " C",
        "136.5, 249", <- 1;
        "146.83333349227905, 246.66666662693024", <- 2;
        "141.66666698455811, 247.83333325386047" <- 3;

这是我之前的代码,它仅适用于单个起始点(moveTO)。如果我绘制很多线条会产生多个起点

"M 82, 130.5  ",
" 76.333333253860474, 137.16666674613953 ",
" 78.833333253860474, 132.66666674613953 ",
" 75.666666507720947, 134.83333349227905  ",
" 86, 144.5 ",
" 77, 139.5 ",
" 81.5, 142",
"M 146, 137.5  ",
" 147.25, 152.66666674613953 ",
" 146.83333331346512, 142.5 ",
" 147.66666662693024, 147.5  ",
" 141.83333337306976, 166.08333349227905 ",
" 146.83333337306976, 157.83333349227905 ",
" 145.16666674613953, 163.16666698455811  ",
" 128.5, 170 ",
" 138.5, 169 ",
" 133.5, 169.5"

    NSArray *myArray = @[@"M 198.16666666418314, 199.75",@"203.5, 198.5", and so on... ];

    NSMutableArray *newArray = [NSMutableArray arrayWithArray:myArray];

    for(int i=1;i<[newArray count];i=i+3) {
        [newArray exchangeObjectAtIndex:i withObjectAtIndex:i+2];
        [newArray exchangeObjectAtIndex:i withObjectAtIndex:i+1];
    }

1 个答案:

答案 0 :(得分:0)

我明白了。多谢你们。

   for(int i=1;i<[newArray count];i++) {
      NSString *strMovTo2 = [newArray objectAtIndex:i];
        NSArray *elements2 = [strMovTo2 componentsSeparatedByString:@"-"];
        newArray2 = [NSMutableArray arrayWithArray:elements2];
      for(int j=1;j<[newArray2 count];j=j+3) {
          [newArray2 exchangeObjectAtIndex:j withObjectAtIndex:j+2];
          [newArray2 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
      }


      newArray3 = [[NSMutableArray alloc] init];
              for (int y = 2; y<[newArray2 count]; y=y+3){
                  [newArray3 addObject: [NSString stringWithFormat:@"%@ %@ %@", newArray2[y-1], newArray2[y], newArray2[y+1] ]];
              }
      curves = [newArray3 componentsJoinedByString:@" C "];

      MovTOstart = [NSString stringWithFormat:@"-M %@ C", newArray2[0]];
      [svgFileToPast appendFormat:@"%@ %@", MovTOstart, curves];
  }