Swift:通过比较类型过滤协议数组(Swift: filter protocol array by comparing types)
(第一篇文章)
通常我能在这里或其他地方找到答案,但这次没有运气=(
问题:在Swift中,如何通过作为函数参数提供的实现类型过滤协议类型的数组?
protocol Aprotocol { var number:Int { get set } } class Aclass: Aprotocol { var number = 1 } class AnotherClass: Aprotocol { var number = 1 } var array:[Aprotocol] = [ Aclass(), AnotherClass(), Aclass() ] func foo (parameter:Aprotocol) -> Int { return array.filter({ /* p in p.self == parameter.self */ }).count } var bar:Aprotocol = // Aclass() or AnotherClass() var result:Int = foo(bar) // should return 2 or 1, depending on bar type
也许这根本不是正确的方法?
谢谢!
(first post)
usually im able to find answers here or elsewhere but no luck this time =(
Question: in Swift, how do you filter an array that is of a protocol type by an implementing type supplied as a function parameter?
protocol Aprotocol { var number:Int { get set } } class Aclass: Aprotocol { var number = 1 } class AnotherClass: Aprotocol { var number = 1 } var array:[Aprotocol] = [ Aclass(), AnotherClass(), Aclass() ] func foo (parameter:Aprotocol) -> Int { return array.filter({ /* p in p.self == parameter.self */ }).count } var bar:Aprotocol = // Aclass() or AnotherClass() var result:Int = foo(bar) // should return 2 or 1, depending on bar type
maybe this is not the right approach at all?
thanks!
原文:https://stackoverflow.com/questions/30823554
满意答案
我可以看到这个命令的一些问题:
db.location.aggregate([ {$match : { FirstName: { $nin : [" "] } } }, {$group: {_id : "$DeviceId" } }, {$project: { "DeviceId" : 1 } } ])
$match
阶段与您问题中提供的文档不匹配,因为您提供的文档具有"FirstName" : " "
因此该文档将匹配$in
而不是$nin
如果
$match
阶段更改为$in
那么它将发出一个文档,分组阶段将有一些工作,但分组阶段只配置为返回_id
属性,因此它返回以下中间文档:{ "_id" : "89984320001499681816" }
$project
阶段尝试投影"DeviceId"
但分组阶段的结果仅包含_id
属性,因此您无法从该文档投影DeviceId
属性。当您的聚合管道到达
forEach
函数时,文档中没有DeviceId,FirstName或LastName属性。要进一步了解聚合管道内发生的情况,您可以运行此查询:
db.location.aggregate([ {$match : { FirstName: { $in : [" "] } } }, {$project: { "DeviceId" : 1, FirstName: 1, LastName: 1 } } ]).forEach( function(obj) { print(obj); } );
或者,只需自己运行管道中的每个阶段并查看每个状态的输出,因为这将成为下一阶段的输入。 所以,例如:
这个命令......
db.location.aggregate([ {$match : { FirstName: { $in : [" "] } } } ])
...返回:
{ "_id" : ObjectId("59d13b64c26584cd8b7a15c8"), "CreationDateTime" : ISODate("2017-09-26T06:39:29.105Z"), "DeviceId" : "89984320001499681816", "UserId" : UUID("bca0db12-2246-49a5-8703-b03fee45e50f"), "FirstName" : " ", "LastName" : "" }
这个命令......
db.location.aggregate([ {$match : { FirstName: { $in : [" "] } } }, {$group: {_id : "$DeviceId" } } ])
...返回:
{ "_id" : "89984320001499681816" }
这个命令......
db.location.aggregate([ {$match : { FirstName: { $in : [" "] } } }, {$group: {_id : "$DeviceId" } }, {$project: { "DeviceId" : 1 } } ])
...返回:
{ "_id" : "89984320001499681816" }
这应该清楚地表明,一旦添加
$group
阶段,您将丢失DeviceId,FirstName和LastName属性,因此它们无法在forEach
函数中打印。I can see a few issues with this command:
db.location.aggregate([ {$match : { FirstName: { $nin : [" "] } } }, {$group: {_id : "$DeviceId" } }, {$project: { "DeviceId" : 1 } } ])
- The
$match
stage will not match the document supplied in your question because the document you supplied has"FirstName" : " "
so that document would be matched by$in
but not by$nin
If the
$match
stage was changed to$in
then it would emit a document and the grouping stage would have something to work on but the grouping stage is only configured to return an_id
attribute so it returns the following intermediate document:{ "_id" : "89984320001499681816" }
The
$project
stage attempts to project"DeviceId"
but the result of the grouping stage contains only an_id
attribute so you cannot project aDeviceId
attribute from that document.By the time your aggregation pipeline reaches the
forEach
funciton there is no DeviceId, FirstName or LastName attribute in the documents.To further understand what's happening inside the aggregation pipeline you could run this query:
db.location.aggregate([ {$match : { FirstName: { $in : [" "] } } }, {$project: { "DeviceId" : 1, FirstName: 1, LastName: 1 } } ]).forEach( function(obj) { print(obj); } );
Or, just run each stage in the pipeline on its own and review the output of each state since that becomes the input to the next stage. So, for example:
This command ...
db.location.aggregate([ {$match : { FirstName: { $in : [" "] } } } ])
... returns:
{ "_id" : ObjectId("59d13b64c26584cd8b7a15c8"), "CreationDateTime" : ISODate("2017-09-26T06:39:29.105Z"), "DeviceId" : "89984320001499681816", "UserId" : UUID("bca0db12-2246-49a5-8703-b03fee45e50f"), "FirstName" : " ", "LastName" : "" }
This command ...
db.location.aggregate([ {$match : { FirstName: { $in : [" "] } } }, {$group: {_id : "$DeviceId" } } ])
... returns:
{ "_id" : "89984320001499681816" }
This command ...
db.location.aggregate([ {$match : { FirstName: { $in : [" "] } } }, {$group: {_id : "$DeviceId" } }, {$project: { "DeviceId" : 1 } } ])
... returns:
{ "_id" : "89984320001499681816" }
This should make clear that once you add in the
$group
stage you lose the DeviceId, FirstName and LastName attributes hence they are not available for you to print in theforEach
function.
相关问答
更多mongodb聚合(co-monk)未定义的函数(mongodb aggregation (co-monk) undefined function)
如何在MongoDB中累积结果(使用forEach?)?(How to accumulate results (with forEach?) in MongoDB?)
仅查询在MongoDB中具有值的文档(Query only documents that have values in it in MongoDB)
如何从MongoDB聚合中读取未定义对象的属性?(How to read property of undefined object from MongoDB aggregation?)
MongoDB查询中forEach函数中的未定义值(Undefined value in forEach function in MongoDB query)
MongoDB:forEach vs fetch + each(MongoDB: forEach vs fetch + each)
如何在forEach函数中检查mongodb文件中是否存在属性?(How to check if attribute exists in mongodb document in forEach function?)
查询中的Mongodb $(Mongodb $within query)
我可以在MongoDB中的聚合查询中申请forEach吗?(Can I apply forEach in aggregate query in MongoDB?)
Mongodb查询中的未定义值(Undefined value in Mongodb query)
相关文章
更多lucene4.7 过滤Filter(六)
Swift入门视频教程-尚学堂视频教程
RFC2848--The PINT Service Protocol: Extensions to SIP and SDP for IP Access to Telephone Call Services
java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter的解决方案
Solr4:利用Filter实现两组关键词组合查询
Storm【实践系列-如何写一个爬虫- 对于Protocol进行的封装】
Filter 简单应用实例求详解
线性渐变-linear-gradient和滤镜opacity/filter的透明效果兼容性解决方案及其RGB/RGBA与16进制转换方法
Hadoop源码分析之RPC(Remote Procedure Call Protocol)
报错说找不到abbrev这个方法,但Array有这个方法的吧?
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!