簡體   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