0

I have an Array of JSON Data that I want to delete nil objects and I have one object with the word Connex in a long string that I want to delete. This value is in the $0.financialInstitution field.

I've tried using filter to find the items I want to delete. My JSON data is rendering correctly.

I've tried working with the solution on this post: Check if array contains part of a string in Swift?

  • I get an error: Cannot convert the value of type '(String) -> Bool' to expected argument type '(RateDetail) -> Bool' when I change the array to use my JSON data.

     // to remove / surpress nil values 
        let nonNilElements = rateDetails.compactMap {$0}
    
     // this does not work
    
    
     //code form post noted above, updated with my search parameters 
    and array
    
        let itemsArray = rateDetails
        let searchToSearch = "Connex"
    
        let filteredStrings = itemsArray.filter({(item: String) -> Bool in
    
            let stringMatch = item.lowercased().range(of: searchToSearch.lowercased())
            return stringMatch != nil ? true : false
        })
        print(filteredStrings)
    
    
        if (filteredStrings as NSArray).count > 0
        {
            //Record found
        }
        else
        {
            //Record Not found
        }
    

Value in RateDetails

.RateDetail(financialInstitution: "Your Neighbourhood C.U.", variableRate: "0", sixMonths: "0", oneYear: "3.59", twoYear: "3.69", threeYear: "3.79", fourYear: "3.89", fiveYear: "3.99", date: "2019-07-01")

value to be deleted:

RatesJSON.RateDetail(financialInstitution: CANNEX on June 30, 2019 at 00:30:20 ET", variableRate: "0", sixMonths: "0", oneYear: "0", twoYear: "0", threeYear: "0", fourYear: "0", fiveYear: "0", date: "2019-07-01"),

Blank value to be deleted:

RatesJSON.RateDetail(financialInstitution: "", variableRate: "0", sixMonths: "0", oneYear: "0", twoYear: "0", threeYear: "0", fourYear: "0", fiveYear: "0", date: "2019-07-01")

  • I want to delete array values that are blank
  • delete the value connex that is part of a longer string stored in the financialInstitution field.
k.thomas
  • 51
  • 7

2 Answers2

0

Is itemsArray an array of RateDetails?

If so, your filter closure states that each item in the array is a String, which isn't true. It's a RateDetail.

So the correct closure looks like this:

itemsArray.filter({ (item: RateDetail) -> Bool in
...
})
rodskagg
  • 3,827
  • 4
  • 27
  • 46
  • 2
    just as an adding on this answer can be that in the filter function item.financialInstitution should be checked for an empty value and for "CANNEX" – m1sh0 Jul 01 '19 at 14:11
0

I do not know if I managed to understand you, but try something like this...

let resultArray = rateDetailArray.filter { rateDetail in
 return  !rateDetail.financialInstitution.isEmpty && !rateDetail.financialInstitution.lowercased().contains("Connex")
}
  • Thank you. I have this a try and the empty values have been removed, but CANNEX is still showing. I copied and pasted part of the string from ratesDetails to remove the risk of typing errors. – k.thomas Jul 01 '19 at 15:10