Search code examples
iosswift3

iOS: How to add NSMutableDictionary to particular index of NSMutableArray in swift?


In my existing project was developed by someone in swift 2, I have two NSMutableArrays with a collection of NSDictionarys, NSArray names are array1 and array2. Here is My question, how to add a particular index dictionary from array2 to particular index in array1 as a dictionary.

Here I post the Main Dictionary.

{
    AccountNumber = "2004-001-014";
    City = "CANOGA PARK";
    FIPS = 6037;
    InspectionId = 228;
    InspectionLogId = 169;
    InspectionType = "Property Owner";
    InspectionTypeId = 1;
    LandUseCode = 1001;
    ScheduledDate = "2018-02-15T00:00:00";
    State = CA;
    Status = Completed;
    StatusId = 3;
    StreetName = MAYNARD;
    StreetNumber = 8312;
    TemplateId = 1090;
    TemplateName = "Casualty Loss Inspection";
    UserId = 72;
    UserPropertyId = 1380;
    XCoordinate = "34.2197";
    YCoordinate = "-118.6214";
    Zip = 91304;
}

Here I post the New Dictionary.

LandUseCode = 101;
ScheduledDate = "2018-01-21T00:00:00";
State = CA;
Status = Done;
StatusId = 2;
StreetName = MAYNARD;
StreetNumber = 8312;
TemplateId = 1090;

This is the format want to make now.

{
    AccountNumber = "2004-001-014";
    City = "CANOGA PARK";
    FIPS = 6037;
    InspectionId = 228;
    InspectionLogId = 169;
    InspectionType = "Property Owner";
    InspectionTypeId = 1;
    LandUseCode = 1001;
    ScheduledDate = "2018-02-15T00:00:00";
    State = CA;
    Status = Completed;
    StatusId = 3;
    StreetName = MAYNARD;
    StreetNumber = 8312;
    TemplateId = 1090;
    TemplateName = "Casualty Loss Inspection";
    UserId = 72;
    UserPropertyId = 1380;
    XCoordinate = "34.2197";
    YCoordinate = "-118.6214";
    Zip = 91304;
    newlyadded =         {
        LandUseCode = 101;
        ScheduledDate = "2018-01-21T00:00:00";
        State = CA;
        Status = Done;
        StatusId = 2;
        StreetName = MAYNARD;
        StreetNumber = 8312;
        TemplateId = 1090;

    };
}

But I don't know that.

Here I post the code what I am trying.

func mergeOrderAndOpenOrderArrayData() {
        for k in 0..<(logOrderMutArray.count) {
            for j in 0..<self.openOrderArray.count {
                if let logOrderfips:NSNumber = (logOrderMutArray[k] as AnyObject).value(forKey: "FIPS") as? NSNumber {
                    if let openOrderfips:NSNumber = (openOrderArray[j] as AnyObject).value(forKey: "FIPS") as? NSNumber {
                        if let logOrderAccNum:String = (logOrderMutArray[k] as AnyObject).value(forKey: "AccountNumber") as? String {
                            if let openOrderAccNum:String = (openOrderArray[j] as AnyObject).value(forKey: "AccountNumber") as? String {
                                if logOrderfips == openOrderfips && logOrderAccNum == openOrderAccNum {
                                    print("Same")
                                    let PropertyMutableDict = NSMutableDictionary()
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "AccountNumber"), forKey: "AccountNumber")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "FIPS"), forKey: "FIPS")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "City"), forKey: "City")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "InspectionId"), forKey: "InspectionId")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "InspectionLogId"), forKey: "InspectionLogId")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "InspectionType"), forKey: "InspectionType")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "InspectionTypeId"), forKey: "InspectionTypeId")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "LandUseCode"), forKey: "LandUseCode")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "ScheduledDate"), forKey: "ScheduledDate")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "State"), forKey: "State")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "Status"), forKey: "Status")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "StatusId"), forKey: "StatusId")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "StreetName"), forKey: "StreetName")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "StreetNumber"), forKey: "StreetNumber")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "TemplateId"), forKey: "TemplateId")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "TemplateName"), forKey: "TemplateName")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "UserId"), forKey: "UserId")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "UserPropertyId"), forKey: "UserPropertyId")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "XCoordinate"), forKey: "XCoordinate")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "YCoordinate"), forKey: "YCoordinate")
                                    PropertyMutableDict.setValue((openOrderArray[j] as AnyObject).value(forKey: "Zip"), forKey: "Zip")
                                    validatePropertyMutableDict.setValue(PropertyMutableDict, forKey: "validateproperty")
                                    print("validatePropertyMutableDict is", validatePropertyMutableDict)
                                    validatePropertyMutArray.insert(validatePropertyMutableDict, at: k)
                                    print(logOrderMutArray)
                                }
                            }
                        }
                    }
                }
            }
        }
}

Solution

  • Get the value from existing dictionary append new dictionary into this and put it back to its position.

    var dictMutableCopy = (array1[index] as! NSDictionary).mutableCopy as! NSMutableDictionary
    let newDict = array2[index] as! NSDictionary
    dictMutableCopy["newlyAdded"] = newDict
    /// Or use below line if you want to merge both dictionaries
    /// dictMutableCopy.addEntries(from: <#T##[AnyHashable : Any]#>)
    
    array1[index] = dictMutableCopy
    

    I don't have Swift 2.0 environment to test it as it is very old but it will work or help you. You don't need to add one by one key into another dictionary.