如何在swift中将NSArray合并到NSMutableArray?

时间:2018-05-23 09:00:31

标签: ios swift nsmutablearray nsarray

在我的应用程序中我想基于某种情况将NSArray合并到NSMutable数组(即)如果两个数组中的accountNumber和Fips相同,我想合并,如下面的格式..

这里我粘贴了我的NSArray响应:

{
    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;
},

这里我粘贴了我的NSMutableArray响应:

{
    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;
}

如果两个数组中的AccountNumber和Fips相同,则表示我想将特定索引添加到子数组中以获取NSMutableArray值。 在这里,我展示格式我想要合并两个Arrays.Suppose AccountNumber和Fips不相同意味着我想将特定索引添加到NSMutableArray作为新的词典索引。:

{
    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;
}

在这里,我展示了我的代码我尝试了什么,我犯了一些错误,但无法找到它。请帮我解决这个问题。

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 个答案:

答案 0 :(得分:0)

我假设你想使用这些数组在某些屏幕上显示数据。如果为数组对象创建模型类,将会更容易。

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

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

使用数组中的数据初始化模型并将其存储在某些变量中。

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

现在循环遍历模型数组以检查值并附加到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)
        }
    }
}

答案 1 :(得分:0)

Swift 5简单方法 https://stackoverflow.com/a/57389179/6881070

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