问题描述
在我的应用程序中,我使用 alamofire 对象映射器来使用 MVC 结构.现在我得到了一个数组,我把它放在模型类中.
in my app i am using alamofire object mapper for use MVC structure. now i am getting one array and i fit it in model class.
这里是模型类
class OrderDetailSecond: Mappable {
var id : Int?
var isRxMedicine : Int?
var medicineTypeId : String?
var name : String?
var orderId : String?
var price : String?
var quentity : Int?
var strength : String?
required init?(_ map: Map){
}
func mapping(map: Map) {
id <- map["id"]
isRxMedicine <- map["is_rx_medicine"]
medicineTypeId <- map["medicine_type_id"]
name <- map["name"]
orderId <- map["order_id"]
price <- map["price"]
quentity <- map["qty"]
strength <- map["strengh"]
}
}
注意:OrderDetailSecond 是一个数组
现在在 [OrderDetailSecond] 的 orderData 中,我得到了那个数组
and now in orderData which is [OrderDetailSecond] i got that array
那个数组有很多这样的对象
that array have many object like this
(
{
id = 50158;
"is_rx_medicine" = 1;
"medicine_type_id" = 2;
name = "1-11~qwe";
"order_id" = 50128;
price = "<null>";
qty = 12;
strengh = "12 mcg";
},
{
id = 50159;
"is_rx_medicine" = 1;
"medicine_type_id" = 3;
name = "1-12~qwe";
"order_id" = 50128;
price = "<null>";
qty = 12;
strengh = "12 ng/dL";
}
);
现在我只想要"is_rx_medicine" = 1 的对象;
Now i want only that object whom "is_rx_medicine" = 1;
并希望将该对象添加到任何特定数组中.那么我该怎么做呢?
and want to add that object in any perticular array. So how can i do this?
这是我试过的.
for mytest in orderdata
{
if mytest.isRxMedicine == 1
{
self.myarray?.addObject(mytest)
}
}
注意:在这个 mytest 中输入 OrderDetailSecond 就像 let mytest: OrderDetailSecond
NOTE: in this mytest is type OrderDetailSecond like let mytest: OrderDetailSecond
当我打印 myarray 时,它显示 orderpilz.OrderDetailSecond,这是我的类名.
and when i print myarray its show orderpilz.OrderDetailSecond which is my class name.
当我尝试像这样打印时
print(myarray?.objectAtIndex(0).valueForKey("quentity"))
它给了我这个错误
*** NSForwarding: warning: object 0x1461b800 of class 'orderpilz.OrderDetailSecond' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[orderpilz.OrderDetailSecond valueForKey:]
可能的选择.
1-> 我可以用这样的对象创建数组,然后手动向该对象添加值,然后将该对象添加到数组中.所以让我知道我该怎么做
1-> i can make array with object like this and then add value to that object manually and then add that object to array. So let me know how can i do this
推荐答案
你可以简单地过滤 orderdata
数组而不是循环和检查条件
You can simply filter the orderdata
array instead of looping and checking the condition
let filteredArray = orderdata.filter({
$0.isRxMedicine == 1
})
这将返回具有 isRxMedicine = 1
注意: filteredArray
中的对象类型将与 orderdata
Note: The type of objects in the filteredArray
will be same as objects in orderdata
这篇关于如何追加数组.或者我如何将模型转换为 nsmuttable 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!