簡體   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