简体   繁体   English

在 Objective-C 或 Swift 中将大量 NSArray 组合成单个 NSArray

[英]Combine large number of NSArrays into single NSArray in Objective-C or Swift

I can combine two NSArrays with the following code:我可以将两个 NSArray 与以下代码结合使用:

NSArray *combinedArray =firstArray?[firstArray arrayByAddingObjectsFromArray:secondArray]:[[NSArray alloc] initWithArray:secondArray];

If you have a large number of arrays such as ten or more, is there a simpler way to combine them than one by one?如果您有大量数组,例如十个或更多,有没有比一个一个更简单的方法来组合它们?

If we are talking about Swift and all you need it's a short way, then you can do it like this:如果我们正在谈论 Swift 并且您只需要一条捷径,那么您可以这样做:

let a = [0, 1]
let b = [2, 3]
let c = [4, 5]

let d = [a, b, c].flatMap { $0 }

LinqToObjectiveC may be your help. LinqToObjectiveC可能会对您有所帮助。

This project contains a collection of NSArray and NSDictionary methods that allow you to execute queries using a fluent syntax, inspired by Linq.该项目包含 NSArray 和 NSDictionary 方法的集合,它们允许您使用受 Linq 启发的流畅语法执行查询。

I think you can merge ten or more arrays simply like below.我认为您可以简单地合并十个或更多数组,如下所示。

id mergedArray = [tenOrMoreArrays linq_aggregate:^id(id item, id aggregate) {
    return [aggregate arrayByAddingObjectsFromArray:item];
}];

*I have never tried this project, but I think it's worth to try. *我从未尝试过这个项目,但我认为值得一试。

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

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