繁体   English   中英

如何快速将NSArray合并到NSMutableArray?

[英]How to Merge NSArray to NSMutableArray in swift?

在我的应用程序中,我想根据某些情况将NSArray合并到NSMutable数组(即,如果两个数组中的accountNumber和Fips相同,我想像以下格式一样进行合并。

在这里,我粘贴了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;
},

在这里,我粘贴了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值的子数组中。 在这里我展示了我想要合并两个数组的格式。假设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)
}

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

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)
        }
    }
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM