Arrays 按特定映射筛选2个结构/数组
我是一个使用swift 3和xcode 8.3的新用户。当前面临筛选2阵列/结构的问题,其中控制台输出如下:Arrays 按特定映射筛选2个结构/数组,arrays,json,uitableview,swift3,Arrays,Json,Uitableview,Swift3,我是一个使用swift 3和xcode 8.3的新用户。当前面临筛选2阵列/结构的问题,其中控制台输出如下: A_List : Optional([117, 115, 18]) B_List : Optional([{ URL = "169.jpeg"; categories = "A"; description = "description XXX"; height = 128; id = 1; likes = "1.00"; name
A_List : Optional([117, 115, 18])
B_List : Optional([{
URL = "169.jpeg";
categories = "A";
description = "description XXX";
height = 128;
id = 1;
likes = "1.00";
name = "Cake - Baked";
price = "13.78";
width = 128;
}, {
URL = "1622.jpeg";
categories = "A";
description = "Baked till golden";
height = 128;
id = 15;
likes = "1.00";
name = "Croissant";
price = "3.71";
width = 128;
}, {
URL = "11.jpeg";
categories = "A";
description = "description Crispy.";
height = 128;
id = 18;
likes = "1.00";
name = "Plain";
price = "2.65";
width = 128;
}, {
URL = "1622.jpeg";
categories = "A";
description = "A ";
height = 128;
id = 103;
likes = "1.00";
name = "America Pie";
price = "2.12";
width = 128;
}, {
URL = "11.jpeg";
categories = "B";
description = "Puff";
height = 128;
id = 115;
likes = "1.00";
name = "Puff";
price = "2.12";
width = 128;
}, {
URL = "168.jpeg";
categories = "C";
description = "description YYY";
height = 128;
id = 117;
likes = "1.00";
name = "Normal";
price = "3.18";
width = 128;
}])
filtered_List : Optional([{
URL = "11.jpeg";
categories = "A";
description = "description Crispy.";
height = 128;
id = 18;
likes = "1.00";
name = "Plain";
price = "2.65";
width = 128;
}, {
URL = "11.jpeg";
categories = "B";
description = "Puff";
height = 128;
id = 115;
likes = "1.00";
name = "Mini Puff";
price = "2.12";
width = 128;
}, {
URL = "168.jpeg";
categories = "C";
description = "description YYY";
height = 128;
id = 117;
likes = "1.00";
name = "Normal";
price = "3.18";
width = 128;
}])
var filtered_List = [AnyObject]()
let fullrList = B_List?.map{$0["id"] as! String}.map{_ in A_List}
filtered_List.append(fullrList as AnyObject )
print("result :\(filtered_List)")
我想返回B_-List full info作为var-filtered_-List=[AnyObject]()
,其中仅包含一个列表id号117、115和18,如下所示:
A_List : Optional([117, 115, 18])
B_List : Optional([{
URL = "169.jpeg";
categories = "A";
description = "description XXX";
height = 128;
id = 1;
likes = "1.00";
name = "Cake - Baked";
price = "13.78";
width = 128;
}, {
URL = "1622.jpeg";
categories = "A";
description = "Baked till golden";
height = 128;
id = 15;
likes = "1.00";
name = "Croissant";
price = "3.71";
width = 128;
}, {
URL = "11.jpeg";
categories = "A";
description = "description Crispy.";
height = 128;
id = 18;
likes = "1.00";
name = "Plain";
price = "2.65";
width = 128;
}, {
URL = "1622.jpeg";
categories = "A";
description = "A ";
height = 128;
id = 103;
likes = "1.00";
name = "America Pie";
price = "2.12";
width = 128;
}, {
URL = "11.jpeg";
categories = "B";
description = "Puff";
height = 128;
id = 115;
likes = "1.00";
name = "Puff";
price = "2.12";
width = 128;
}, {
URL = "168.jpeg";
categories = "C";
description = "description YYY";
height = 128;
id = 117;
likes = "1.00";
name = "Normal";
price = "3.18";
width = 128;
}])
filtered_List : Optional([{
URL = "11.jpeg";
categories = "A";
description = "description Crispy.";
height = 128;
id = 18;
likes = "1.00";
name = "Plain";
price = "2.65";
width = 128;
}, {
URL = "11.jpeg";
categories = "B";
description = "Puff";
height = 128;
id = 115;
likes = "1.00";
name = "Mini Puff";
price = "2.12";
width = 128;
}, {
URL = "168.jpeg";
categories = "C";
description = "description YYY";
height = 128;
id = 117;
likes = "1.00";
name = "Normal";
price = "3.18";
width = 128;
}])
var filtered_List = [AnyObject]()
let fullrList = B_List?.map{$0["id"] as! String}.map{_ in A_List}
filtered_List.append(fullrList as AnyObject )
print("result :\(filtered_List)")
我在youtube上尝试了一些代码和阅读教程,但不幸的是并没有找到任何解决方案,它仅限于swift2示例
目前,我的代码尝试如下:
A_List : Optional([117, 115, 18])
B_List : Optional([{
URL = "169.jpeg";
categories = "A";
description = "description XXX";
height = 128;
id = 1;
likes = "1.00";
name = "Cake - Baked";
price = "13.78";
width = 128;
}, {
URL = "1622.jpeg";
categories = "A";
description = "Baked till golden";
height = 128;
id = 15;
likes = "1.00";
name = "Croissant";
price = "3.71";
width = 128;
}, {
URL = "11.jpeg";
categories = "A";
description = "description Crispy.";
height = 128;
id = 18;
likes = "1.00";
name = "Plain";
price = "2.65";
width = 128;
}, {
URL = "1622.jpeg";
categories = "A";
description = "A ";
height = 128;
id = 103;
likes = "1.00";
name = "America Pie";
price = "2.12";
width = 128;
}, {
URL = "11.jpeg";
categories = "B";
description = "Puff";
height = 128;
id = 115;
likes = "1.00";
name = "Puff";
price = "2.12";
width = 128;
}, {
URL = "168.jpeg";
categories = "C";
description = "description YYY";
height = 128;
id = 117;
likes = "1.00";
name = "Normal";
price = "3.18";
width = 128;
}])
filtered_List : Optional([{
URL = "11.jpeg";
categories = "A";
description = "description Crispy.";
height = 128;
id = 18;
likes = "1.00";
name = "Plain";
price = "2.65";
width = 128;
}, {
URL = "11.jpeg";
categories = "B";
description = "Puff";
height = 128;
id = 115;
likes = "1.00";
name = "Mini Puff";
price = "2.12";
width = 128;
}, {
URL = "168.jpeg";
categories = "C";
description = "description YYY";
height = 128;
id = 117;
likes = "1.00";
name = "Normal";
price = "3.18";
width = 128;
}])
var filtered_List = [AnyObject]()
let fullrList = B_List?.map{$0["id"] as! String}.map{_ in A_List}
filtered_List.append(fullrList as AnyObject )
print("result :\(filtered_List)")
如果有专家能在这里指导或给出您的解决方案,我们将不胜感激 您应该将所需ID存储在一个集合中,而不是一个数组中。您只需执行一个简单的过滤器操作:
let desiredIds: Set = [117, 115, 18]
B_List.filter{ $0["id"].map{ desiredIds.contains($0) } ?? false } as [AnyObject]
您应该将所需的ID存储在一个集合中,而不是一个数组中。您只需执行一个简单的过滤器操作:
let desiredIds: Set = [117, 115, 18]
B_List.filter{ $0["id"].map{ desiredIds.contains($0) } ?? false } as [AnyObject]
感谢所有专门回复@Alexander这个话题的人。在这里,我的解决方案与其他人分享,可能并不完美
var resultAnyObject = [AnyObject]()
var aListIds = [String]()
for i in A_List! {
aListIds.append(i as! String)
}
//@Alexander : thanks for code below:
let desiredIds = aListIds
let fullList = B_List?.filter{ $0["id"].map{desiredIds.contains($0 as! String) } ?? false };
resultAnyObject.append(fullList as AnyObject)
print("Result of filtered_List :\(resultAnyObject)")
感谢所有专门回复@Alexander这个话题的人。在这里,我的解决方案与其他人分享,可能并不完美
var resultAnyObject = [AnyObject]()
var aListIds = [String]()
for i in A_List! {
aListIds.append(i as! String)
}
//@Alexander : thanks for code below:
let desiredIds = aListIds
let fullList = B_List?.filter{ $0["id"].map{desiredIds.contains($0 as! String) } ?? false };
resultAnyObject.append(fullList as AnyObject)
print("Result of filtered_List :\(resultAnyObject)")
Swift的惯例是对变量名使用lowerCamelCase,而不是snake_大小写,当然也不是Upper_snake_大小写。此外,你的名字“A名单”、“B名单”绝对没有传达任何关于其内容的信息。我强烈建议你付出更多names@Peterhdd不要将代码格式用于与技术版本或名称不同的代码@Alexander感谢您的好意建议。是的,我接受您的评论,并将在我的代码中做得更好。@Peterhdd感谢您的更正。谢谢。@jmattheis,感谢您的commentSwift约定,变量名使用lowerCamelCase,而不是snake_大小写,绝对不是Upper_snake_大小写。此外,你的名字“A名单”、“B名单”绝对没有传达任何关于其内容的信息。我强烈建议你付出更多names@Peterhdd不要将代码格式用于与技术版本或名称不同的代码@Alexander感谢您的好意建议。是的,我接受您的评论,并将在我的代码中做得更好。@Peterhdd感谢您的更正。谢谢。@jmattheis,非常感谢您的评论谢谢您的回复。它起作用了!。无论如何,如何将_列表设置为DesiredId?因为一个列表会改变。错误显示“无法将“[AnyObject]”类型的值转换为指定类型的“Set”。@D_9268通过Set
初始值设定项运行它。(例如Set(someArray)
)。为什么是[AnyObject]
类型的列表,而不是[Int]
?谢谢。其他程序从JSON格式过滤了A\u列表。最后我发现A/B\u列表中的所有信息都是字符串。因此,我必须确保“id”“也使用字符串映射。@D_9268无论何时使用JSON,请确保您远离Any
,AnyObject
,NSArray
,NSDictionary
,[string:Any]
,等等的世界。您想尽快到达类型安全区,请给出建议并指出我的弱点!我将学习更多关于如何在Swift中处理JSON的教程。谢谢你的回复。它起作用了!。无论如何,如何将_列表设置为DesiredId?因为一个列表会改变。错误显示“无法将“[AnyObject]”类型的值转换为指定类型的“Set”。@D_9268通过Set
初始值设定项运行它。(例如Set(someArray)
)。为什么是[AnyObject]
类型的列表,而不是[Int]
?谢谢。其他程序从JSON格式过滤了A\u列表。最后我发现A/B\u列表中的所有信息都是字符串。因此,我必须确保“id”“也使用字符串映射。@D_9268无论何时使用JSON,请确保您远离Any
,AnyObject
,NSArray
,NSDictionary
,[string:Any]
,等等的世界。您想尽快到达类型安全区,请给出建议并指出我的弱点!我将学习更多关于在Swift中处理JSON内容的教程。您只需要一个列表作为[String]
,或者map
。避免使用重复的append
调用循环您只需要A_列表作为[String]
,或者map
。避免重复的append
调用循环