我有一个不是 NSObject 子类的类和该类的实例数组。
enum ObjectType{
case type1
case type2
case type3
}
class MyObject {
var type = ObjectType!
//some other properties...
}
let array = [obj1(type:type1),
obj2(type:type2),
obj3(type:type3),
obj4(type:type2),
obj5(type:type3)]
let type2Array = array.filter(){ $0.type == .type2}
// type2Array is supposed to be [obj2, obj4]
这会导致 fatal error :无法从 Objective-C 桥接数组
如何适本地过滤数组?
我是否必须从 NSObject 继承或使我的类符合任何协议(protocol)?
最佳答案
从你的问题中我可以看出,以上都与 Objective-C 没有任何关系。但是,您的示例包含一些其他问题,使其无法按预期工作。
MyObject 没有初始化器(从 Swift 2.2 开始,您应该至少包含一个初始化器)。obj1, obj2, ... 是什么?当我假设您打算将它们作为 MyObject 类型的实例时,您将它们视为方法或类/结构类型。如果修复上述问题,代码的实际过滤部分将按预期工作(请注意,您可以从 filter() {... } 中省略 () ), 例如:
enum ObjectType{
case type1
case type2
case type3
}
class MyObject {
var type : ObjectType
let id: Int
init(type: ObjectType, id: Int) {
self.type = type
self.id = id
}
}
let array = [MyObject(type: .type1, id: 1),
MyObject(type: .type2, id: 2),
MyObject(type: .type3, id: 3),
MyObject(type: .type2, id: 4),
MyObject(type: .type3, id: 5)]
let type2Array = array.filter { $0.type == .type2}
type2Array.forEach { print($0.id) } // 2, 4
作为直接过滤到枚举案例的替代方法,您可以指定枚举的 rawValue 类型并与之匹配。例如。使用 Int rawValue 允许您(除了过滤 w.r.t. rawValue)执行模式匹配,例如枚举中的案例范围。
enum ObjectType : Int {
case type1 = 1 // rawValue = 1
case type2 // rawValue = 2, implicitly
case type3 // ...
}
class MyObject {
var type : ObjectType
let id: Int
init(type: ObjectType, id: Int) {
self.type = type
self.id = id
}
}
let array = [MyObject(type: .type1, id: 1),
MyObject(type: .type2, id: 2),
MyObject(type: .type3, id: 3),
MyObject(type: .type2, id: 4),
MyObject(type: .type3, id: 5)]
/* filter w.r.t. to rawValue */
let type2Array = array.filter { $0.type.rawValue == 2}
type2Array.forEach { print($0.id) } // 2, 4
/* filter using pattern matching, for rawValues in range [1,2],
<=> filter true for cases .type1 and .type2 */
let type1or2Array = array.filter { 1...2 ~= $0.type.rawValue }
type1or2Array.forEach { print($0.id) } // 1, 2, 4
关于arrays - 过滤自定义对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36260439/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss