问题
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.
here is 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"]
}
}
NOTE : OrderDetailSecond is an Array
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";
}
);
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?
this is what i tried.
for mytest in orderdata
{
if mytest.isRxMedicine == 1
{
self.myarray?.addObject(mytest)
}
}
NOTE: in this mytest is type OrderDetailSecond like let mytest: OrderDetailSecond
and when i print myarray its show orderpilz.OrderDetailSecond which is my class name.
and when i tried to print like this
print(myarray?.objectAtIndex(0).valueForKey("quentity"))
its give me this error
*** NSForwarding: warning: object 0x1461b800 of class 'orderpilz.OrderDetailSecond' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[orderpilz.OrderDetailSecond valueForKey:]
possible alternative.
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
回答1:
You can simply filter the orderdata
array instead of looping and checking the condition
let filteredArray = orderdata.filter({
$0.isRxMedicine == 1
})
This will return an array of objects that have isRxMedicine = 1
Note: The type of objects in the filteredArray
will be same as objects in orderdata
来源:https://stackoverflow.com/questions/38138622/how-to-append-array-or-how-can-i-convert-model-to-nsmuttable-array