繁体   English   中英

如何从NSArray删除括号和?

[英]How to remove brackets and , from NSArray?

我有一个数组,其中每个索引都包含数组。

array is :(
        (
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-19g1.jpg",
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-20g2.jpg",
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-20g3.jpg"
    ),
        (
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-49y1.jpg",
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-50y2.jpg"
    ),
        (
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-50y3.jpg",
        "http://localhost/ColorPicker/upload/2014-01-14-04-01-51y6.jpg"

    )
)

我想像这样制作一个数组

  (  
"http://localhost/ColorPicker/upload/2014-01-14-04-01-50y3.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-51y6.jpg", 
"http://localhost/ColorPicker/upload/2014-01-14-04-01-50y3.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-51y6.jpg",                    
"http://localhost/ColorPicker/upload/2014-01-14-04-01-50y3.jpg",
"http://localhost/ColorPicker/upload/2014-01-14-04-01-51y6.jpg"
    )

如何消除数组中的(),()并制作一个包含网址的单个数组。

您需要创建一个新数组:

NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (NSArray *a in array)
    [newArray addObjectsFromArray:a];

您可以使用键值编码运算符“ @unionOfArrays”来展平数组:

NSArray *nested = @[@[@"A1", @"A2", @"A3"], @[@"B1", @"B2", @"B3"], @[@"C1", @"C2", @"C3"]];
NSArray *flattened = [nested valueForKeyPath:@"@unionOfArrays.self"];

NSLog(@"nested = %@", nested);
NSLog(@"flattened = %@", flattened);

输出:

nested = (
        (
        A1,
        A2,
        A3
    ),
        (
        B1,
        B2,
        B3
    ),
        (
        C1,
        C2,
        C3
    )
)
flattened = (
    A1,
    A2,
    A3,
    B1,
    B2,
    B3,
    C1,
    C2,
    C3
)

您需要编写代码来遍历外部数组,将第二级数组的内容复制到“平面”数组中。 像这样:

(根据Carl Norum的帖子进行编辑,以使用addObjectsFromArray)

-(NSArray )flattenArray: (NSArray *) sourceArray;
{
  NSMutableArray *result = [[NSMutableArray alloc] init];
  for (NSArray *array sourceArray)
  {
  //Make sure this object is an array of some kind. 
  //(use isKindOFClass to handle different types of array class cluster)
  if ([array isKindOfClass: [NSArray class])
  {
    [result addObjectsFromArray: array];
  }
  else
  {
    NSLog(@"Non-array object %@ found. Adding directly.", array);
    [result addObject: array];
  }
  return [result copy]; //return an immutable copy of the result array
}

你必须通过数组正常化您的数组循环,那么它的所有子阵列,并将它们添加到另一个阵列像这样的东西应该足以让你开始: 在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM