Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter String array in Swift

By using objective C I have filter year array by using NSPredicate,

Below is code.

  yearArray = [yearArray filteredArrayUsingPredicate:[NSPredicate
  predicateWithFormat:@"SELF != ''"]];

As per above code it's working fine in objective c , I have to filter array in Swift 3,

What is Input Year Array :-

( Year,"","","",JAN,"","","",FEB,"","","",MAR,"","","",APR,"","","",
  MAY,"","","",JUN,"","","",JUL,"","","",AUG,"","","",SEP,"","","",OCT
  ,"","","", NOV,"","","",DEC,"","","","",WIN,"","","",SPR,"","","",SUM
  ,"","","",AUT,"","","","",ANN)

Need filter Output Array

(Year,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC,WIN,SPR,SUM,AUT,ANN)

Please give solution how to filter array in swift.

like image 381
Jaywant Khedkar Avatar asked Dec 05 '25 09:12

Jaywant Khedkar


1 Answers

Use this code:

let yearArray: [String] = ... // your array of type [String]

let filteredArray = yearArray.filter {
    !$0.isEmpty
}

Look at the picture for output:

Example

like image 175
Aleksandr Honcharov Avatar answered Dec 07 '25 21:12

Aleksandr Honcharov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!