0

In My Application I want to Merge NSArray to NSMutable Array based on some scenario(i.e) If the accountNumber and Fips are same in Both arrays I want to Merge like the below format..

Here I paste My NSArray Response :

{
    AccountNumber = G110080003900;
    City = JACKSON;
    FIPS = 39079;
    InspectionId = 425;
    InspectionLogId = 366;
    InspectionType = "OCA Inspector";
    InspectionTypeId = 2;
    LandUseCode = 1001;
    ScheduledDate = "2018-05-21T00:00:00";
    State = OH;
    Status = Done;
    StatusId = 2;
    StreetName = "JISCO WEST";
    StreetNumber = 4564;
    TemplateId = 1075;
    TemplateName = "AVM Inspection";
    UserId = 51;
    UserPropertyId = 1546;
    XCoordinate = "39.03665";
    YCoordinate = "-82.6797";
    Zip = 45640;
},
    {
    AccountNumber = 000053809;
    City = 0;
    FIPS = 5051;
    InspectionId = 427;
    InspectionLogId = 368;
    InspectionType = "OCA Inspector";
    InspectionTypeId = 2;
    LandUseCode = 8001;
    ScheduledDate = "2018-05-22T00:00:00";
    State = AR;
    Status = Done;
    StatusId = 2;
    StreetName = CYPRESSVALE;
    StreetNumber = 0;
    TemplateId = 1075;
    TemplateName = "AVM Inspection";
    UserId = 51;
    UserPropertyId = 1547;
    XCoordinate = 0;
    YCoordinate = 0;
    Zip = 0;
},
    {
    AccountNumber = 000053809;
    City = 0;
    FIPS = 5051;
    InspectionId = 428;
    InspectionLogId = 369;
    InspectionType = "OCA Inspector";
    InspectionTypeId = 2;
    LandUseCode = 8001;
    ScheduledDate = "2018-05-23T00:00:00";
    State = AR;
    Status = "In Progress";
    StatusId = 4;
    StreetName = CYPRESSVALE;
    StreetNumber = 0;
    TemplateId = 1075;
    TemplateName = "AVM Inspection";
    UserId = 51;
    UserPropertyId = 1547;
    XCoordinate = 0;
    YCoordinate = 0;
    Zip = 0;
},{
    AccountNumber = "633-342-004";
    City = "INDIAN WELLS";
    FIPS = 6065;
    InspectionId = 349;
    InspectionLogId = 290;
    InspectionType = "OCA Inspector";
    InspectionTypeId = 2;
    LandUseCode = 1001;
    ScheduledDate = "2018-03-19T00:00:00";
    State = CA;
    Status = Completed;
    StatusId = 3;
    StreetName = CHEYENNE;
    StreetNumber = 77431;
    TemplateId = 1090;
    TemplateName = "Casualty Loss Inspection";
    UserId = 51;
    UserPropertyId = 1482;
    XCoordinate = "33.72453";
    YCoordinate = "-116.313739";
    Zip = 92210;
},

)

Here I paste My NSMutableArray Response:

{
    AccountNumber = 000053809;
    AssessmentRollId = "<null>";
    AssessmentYear = 0;
    BuildingArea = 0;
    City = 0;
    CreatedBy = 51;
    DateCreated = "2018-05-21T00:01:47.93";
    DateModified = "2018-05-21T00:01:47.93";
    FIPS = 5051;
    Id = 1547;
    LandUseCode = 8001;
    LotSizeSquareFeet = 0;
    PropertyId = 78781545;
    State = AR;
    StreetName = CYPRESSVALE;
    StreetNumber = 0;
    TotalAssessedValue = 600;
    UpdatedBy = 51;
    UserId = 51;
    XCoordinate = 0;
    YCoordinate = 0;
    YearBuilt = 0;
    Zip = 0;
},
    {
    AccountNumber = "<null>";
    AssessmentRollId = "<null>";
    AssessmentYear = "<null>";
    BuildingArea = "<null>";
    City = Houston;
    CreatedBy = 51;
    DateCreated = "2018-05-22T01:14:43.29";
    DateModified = "2018-05-22T01:14:43.29";
    FIPS = 48201;
    Id = 1548;
    LandUseCode = 338;
    LotSizeSquareFeet = 2525;
    PropertyId = 1;
    State = TX;
    StreetName = "san Felipe suite 2600";
    StreetNumber = 5847;
    TotalAssessedValue = "<null>";
    UpdatedBy = 51;
    UserId = 51;
    XCoordinate = "29.749522";
    YCoordinate = "-95.481959";
    YearBuilt = 2013;
    Zip = 77057;
}

If AccountNumber and Fips are Same in both Array means I want add the Particular Index to the Child array for NSMutableArray Value. Here I Show the Format what i want to Merge both Arrays.Suppose The AccountNumber and Fips are Not Same means Just I want to Add the Particular Index to NSMutableArray as a new Dictionary Index.:

{
    AccountNumber = 000053809;
    AssessmentRollId = "<null>";
    AssessmentYear = 0;
    BuildingArea = 0;
    City = 0;
    CreatedBy = 51;
    DateCreated = "2018-05-21T00:01:47.93";
    DateModified = "2018-05-21T00:01:47.93";
    FIPS = 5051;
    Id = 1547;
    Inspectionlist =         (
        {
            AccountNumber = 000053809;
            City = 0;
            FIPS = 5051;
            InspectionId = 428;
            InspectionLogId = 369;
            InspectionType = "OCA Inspector";
            InspectionTypeId = 2;
            LandUseCode = 8001;
            ScheduledDate = "2018-05-23T00:00:00";
            State = AR;
            Status = "In Progress";
            StatusId = 4;
            StreetName = CYPRESSVALE;
            StreetNumber = 0;
            TemplateId = 1075;
            TemplateName = "AVM Inspection";
            UserId = 51;
            UserPropertyId = 1547;
            XCoordinate = 0;
            YCoordinate = 0;
            Zip = 0;
        },
        {
            AccountNumber = 000053809;
            City = 0;
            FIPS = 5051;
            InspectionId = 428;
            InspectionLogId = 369;
            InspectionType = "OCA Inspector";
            InspectionTypeId = 2;
            LandUseCode = 8001;
            ScheduledDate = "2018-05-23T00:00:00";
            State = AR;
            Status = "In Progress";
            StatusId = 4;
            StreetName = CYPRESSVALE;
            StreetNumber = 0;
            TemplateId = 1075;
            TemplateName = "AVM Inspection";
            UserId = 51;
            UserPropertyId = 1547;
            XCoordinate = 0;
            YCoordinate = 0;
            Zip = 0;
        }
    );
    LandUseCode = 8001;
    LotSizeSquareFeet = 0;
    PropertyId = 78781545;
    State = AR;
    StreetName = CYPRESSVALE;
    StreetNumber = 0;
    TotalAssessedValue = 600;
    UpdatedBy = 51;
    UserId = 51;
    XCoordinate = 0;
    YCoordinate = 0;
    YearBuilt = 0;
    Zip = 0;
},
{
    AccountNumber = "<null>";
    AssessmentRollId = "<null>";
    AssessmentYear = "<null>";
    BuildingArea = "<null>";
    City = Houston;
    CreatedBy = 51;
    DateCreated = "2018-05-22T01:14:43.29";
    DateModified = "2018-05-22T01:14:43.29";
    FIPS = 48201;
    Id = 1548;
    LandUseCode = 338;
    LotSizeSquareFeet = 2525;
    PropertyId = 1;
    State = TX;
    StreetName = "san Felipe suite 2600";
    StreetNumber = 5847;
    TotalAssessedValue = "<null>";
    UpdatedBy = 51;
    UserId = 51;
    XCoordinate = "29.749522";
    YCoordinate = "-95.481959";
    YearBuilt = 2013;
    Zip = 77057;
},{
    AccountNumber = "633-342-004";
    City = "INDIAN WELLS";
    FIPS = 6065;
    InspectionId = 349;
    InspectionLogId = 290;
    InspectionType = "OCA Inspector";
    InspectionTypeId = 2;
    LandUseCode = 1001;
    ScheduledDate = "2018-03-19T00:00:00";
    State = CA;
    Status = Completed;
    StatusId = 3;
    StreetName = CHEYENNE;
    StreetNumber = 77431;
    TemplateId = 1090;
    TemplateName = "Casualty Loss Inspection";
    UserId = 51;
    UserPropertyId = 1482;
    XCoordinate = "33.72453";
    YCoordinate = "-116.313739";
    Zip = 92210;
}

Here I show my Code What I am tried, I made some Mistake but Unable to Find that. Please help me to fix the issue.

func removeDuplicatesFromOrdersArray3() {
    print(propertyMutableArray.count)
    let storedArray = NSMutableArray()
    for a in 0..<propertyMutableArray.count {
        let orderFipsValue:NSNumber = (propertyMutableArray[a] as AnyObject).value(forKey: "FIPS") as! NSNumber
        let orderAccountNumberValue:String = (propertyMutableArray[a] as AnyObject).value(forKey: "AccountNumber") as? String ?? ""
        for b in 0..<mergeOrdersMutArray.count {
            let FipsValue:NSNumber = (mergeOrdersMutArray[b] as AnyObject).value(forKey: "FIPS") as! NSNumber
            let AccountNumberValue:String = (mergeOrdersMutArray[b] as AnyObject).value(forKey: "AccountNumber") as? String ?? ""
            if orderFipsValue == FipsValue && orderAccountNumberValue == AccountNumberValue {
                storedArray.add(mergeOrdersMutArray[b])
                let dictMutableCopy = propertyMutableArray[a] as! NSDictionary
                let editDict = NSMutableDictionary(dictionary: dictMutableCopy)
                editDict.setValue(storedArray, forKey: "Inspectionlist")
                propertyMutableArray.add(editDict)
                mergeOrdersMutArray.remove(b)
                isDuplicate = true
            }
            else {
                if (!isDuplicate) {
                    propertyMutableArray.add(mergeOrdersMutArray[b])
                }
            }
        }
    }
    print(mergeOrdersMutArray.count)
    print(propertyMutableArray.count)
    print(openOrderArray.count)
    print(logOrderArray.count)
    print(storedArray.count)
}

2 Answers 2

0

I assume you want to use these arrays for displaying data in some screens. It will be easier if you created model classes for the array objects.

Struct NonMutableModel {
    var accntNum: String?
    var flips: Int?
    ......
}

Struct MutableModel {    
    var accntNum: String?    
    var flips: Int?    
    var inspectionList: [Model]    
    ....    
}

Initialize the models with the data from your arrays and store it in some variables.

var nonMutableModels: [NonMutableModel]?
var mutableModels: [MutableModel]?

now you loop through the model arrays to check the values and append to mutableModels.

guard let mutables = mutableModels, let nonMutables = nonMutableModels else { return }


for mutableModel in mutables {
    for model in nonMutables {
        if mutableModel.accntNum == model.accntNum, mutableModel.flips == model.flips {
            mutableModel.inspectionList.append(model)
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Swift 5 Easy way https://stackoverflow.com/a/57389179/6881070

https://stackoverflow.com/a/57389179/6881070 
//MARK:- Follow this link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.