how to append array. or how can i convert model to nsmuttable array

ε祈祈猫儿з 提交于 2019-12-12 02:42:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!