- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在多个 NSArray 中搜索和合并对象。
基本上 NSArray 具有多个 NSArray 和 1 个对象,然后是 NSDictionary。我想检查该对象是否已经存在并将其与现有对象合并,这样它就会是 NSSArray,它有一个带有 1 个对象的 NSArray,然后是多个 NSDictionary。在 Objective-c 中。
(
(
"The Essential: Céline Dion",
{
"album": "The Essential: Céline Dion",
"artwork": "<UIImage: 0x608000087210>, {512, 506}",
"title": "It's All Coming Back to Me Now"
}
),
(
"Ah Via Musicom",
{
"album": "Ah Via Musicom",
"artwork": "<UIImage: 0x60000008a6e0>, {600, 600}",
"title": "Cliffs of Dover"
}
),
(
"Tears Roll Down (Greatest Hits 82-92)",
{
"album": "Tears Roll Down (Greatest Hits 82-92)",
"artwork": "<UIImage: 0x6080000887a0>, {512, 512}",
"title": "Everybody Wants to Rule the World"
}
),
(
"No Money - Single",
{
"album": "No Money - Single",
"artwork": "<UIImage: 0x6080000845b0>, {600, 600}",
"title": "No Money"
}
),
(
"Tears Roll Down (Greatest Hits 82-92)",
{
"album": "Tears Roll Down (Greatest Hits 82-92)",
"artwork": "<UIImage: 0x60800008d5c0>, {512, 512}",
"title": "Shout"
}
),
(
"STAB!",
{
"album": "STAB!",
"artwork": "<UIImage: 0x60800008da20>, {512, 512}",
"title": "Will You Remember Me?"
}
),
(
"Life After Sundown",
{
"album": "Life After Sundown",
"artwork": "<UIImage: 0x60000008d840>, {600, 600}",
"title": "Werewolves On Wheels"
}
)
)
对此
(
(
"The Essential: Céline Dion",
{
"album": "The Essential: Céline Dion",
"artwork": "<UIImage: 0x608000087210>, {512, 506}",
"title": "It's All Coming Back to Me Now"
}
),
(
"Ah Via Musicom",
{
"album": "Ah Via Musicom",
"artwork": "<UIImage: 0x60000008a6e0>, {600, 600}",
"title": "Cliffs of Dover"
}
),
(
"Tears Roll Down (Greatest Hits 82-92)",
{
"album": "Tears Roll Down (Greatest Hits 82-92)",
"artwork": "<UIImage: 0x6080000887a0>, {512, 512}",
"title": "Everybody Wants to Rule the World"
},
{
"album": "Tears Roll Down (Greatest Hits 82-92)",
"artwork": "<UIImage: 0x60800008d5c0>, {512, 512}",
"title": "Shout"
}
),
(
"No Money - Single",
{
"album": "No Money - Single",
"artwork": "<UIImage: 0x6080000845b0>, {600, 600}",
"title": "No Money"
}
),
(
"STAB!",
{
"album": "STAB!",
"artwork": "<UIImage: 0x60800008da20>, {512, 512}",
"title": "Will You Remember Me?"
}
),
(
"Life After Sundown",
{
"album": "Life After Sundown",
"artwork": "<UIImage: 0x60000008d840>, {600, 600}",
"title": "Werewolves On Wheels"
}
)
)
最佳答案
让我们将原始数组称为mainArray
。
首先,您从该 mainArray
中收集唯一的名称。
NSMutableArray *uniqueNames = [NSMutableArray array];
for (NSArray *sub in mainArray) {
NSString *name = [sub firstObject];
BOOL isDuplicate = [[uniqueNames filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF contains %@",name]] count] > 0;
if (!isDuplicate) {
[uniqueNames addObject:name];
}
}
之后,循环遍历 uniqueNames
数组并在 mainArray
中搜索重复项并重新构造数组。
NSMutableArray *final = [NSMutableArray array];
for (NSString *name in uniqueNames) {
NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF contains %@",name];
NSArray *duplicateArrays = [mainArray filteredArrayUsingPredicate:namePredicate];
if ([duplicateArrays count] > 1) {
NSMutableArray *newArray = [NSMutableArray arrayWithObject:name];
for (NSArray *duplicateArray in duplicateArrays) {
[newArray addObject:[duplicateArray lastObject]];
}
[final addObject:newArray];
}
else{
[final addObject:duplicateArrays];
}
}
这里的final
数组就是你的结果。
关于iOS sdk NSArray 内的 NSArray 查找和合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42358860/
在我的应用程序中,我必须创建这样的结构: $arrProducts = array( array( “product_id” => “1”, “qty” =>
我相信我的问题涉及指针,这是一个我经常纠结的概念,但这就是我正在尝试做的事情。 我有六个 NSArray。我想要一个由这六个数组组成的额外 NSArray,所以: self.arr1 = [NSArr
我正在使用 VKSdk 制作 iOS 应用程序.我想将数据从 response.json[@"items"](它是 nsarray,response.json 是它的 nsdictionary)复制到
我创建 NSArray *test = @[@[@1,@2,@3,@4], @[@3,@5,@6,@7]]; 这将被视为 nsarray 的 nsarray。 但是当我想用打印出来的时候 NSLog
首先引用下图: 我有一个包含 9 个元素的 NSArray。这 9 个元素中的每一个都包含另外 5 个元素。 我想做的是将 [*][1] 从每个地方带到另一个地方,只包含这些日期。 如何最好地实现这一
我正在尝试将 NSDictionaries 数组排序为基于 NSDictionary valueForKey: 第一个字母之一的字母化数组数组 实际上我想要看起来有点像这样的东西 Array A //
我有一个 NSArray 输出如下: ( { category = 1; categoryname = Random; excuse = "Enter your exc
我是 Objective-C 和 iPhone 的新手 我必须使用值对 NSDictionary 进行排序,并且根据我必须排列该值的键,我已经对数组进行了排序。 //getting values NS
我觉得这很简单,但我就是无法理解它。我有一组星期几供用户选择: NSArray * daysOfTheWeek = [NSArray arrayWithObjects:@"sunday", @"mon
我有一个 NSArray 对象: class Object { var name: String? = nil var id: String? = nil } 我想创建一个具有唯一“名
我只是想将 NSNumbers 的 NSArray 排序为数字顺序,但我不确定最好的方法。以我的思维方式,001 和 002 具有相当的可比性,所以我怀疑两者都可以。对于 003,我不确定当方法需要
我面临一个非常常见的场景。 我有一个 NSArray,它有一个自定义类型的对象,比如 Person。 Person 类具有以下属性:firstName、lastName 和 age。 如何从具有 Pe
我有一个 NSArray,其中填充了大约 30 个 NSDate 格式的日期, 我需要做的是从中创建另一个数组,其中包含从第一个日期到最后一个日期的所有日期的 bool 值。 前数组 111/1/12
NSarray 的简单问题。我将对象存储在 NSMuteableArray 中。[obj1, obj2, obj3] 如果选择了一个对象,我想把它放在数组的前面。 IE。如果选择了 obj3,则: [
我已按字母顺序存储联系人,因此我有联系人列表的数组数组,该数组已显示在屏幕上。现在我想通过姓名搜索联系人,但谓词在这里无法正常工作。我已经完成了下面的代码。 filterArray.filter
对于使用 ARC 的 iOS 5.0 应用程序,我有一个对象的 NSArray,其中包含其他对象的 NSArray。是否可以在不遍历数组的情况下从内部数组中提取对象列表,例如比如,使用 NSPredi
这个问题在这里已经有了答案: Sort NSArray of custom objects based on sorting of another NSArray of strings (5 个答案
我有一个 NSArray 对象,每个对象都包含一个“键”字段 我有另一个带有嵌套NSArrays的NSArray,通过不同的字段“按字段分组”对这些对象进行分组,例如显示名称(字母分组) 例如 @in
我正在尝试在多个 NSArray 中搜索和合并对象。 基本上 NSArray 具有多个 NSArray 和 1 个对象,然后是 NSDictionary。我想检查该对象是否已经存在并将其与现有对象合并
这个问题在这里已经有了答案: Check that the contents of one NSArray are all in another array (9 个回答) 关闭 8 年前。
我是一名优秀的程序员,十分优秀!