0

I am storing values from Json response like

self.NameArray = self.attachmentsArray.valueForKey("filename") as! NSArray 

Output:

NameArray(("Din.pdf","img.jpeg"),(),(),("41_58"))

I got this output. I need to get the array only having ("Din.pdf","img.jpeg","41_58").

How to get it using swift code?

10
  • what is the type of your NameArray? Commented Sep 14, 2016 at 11:06
  • Its not duplicate. Is it?? Commented Sep 14, 2016 at 11:16
  • NameArray is in NSArray Commented Sep 14, 2016 at 11:16
  • 1
    Apologies, it does! Commented Sep 14, 2016 at 11:19
  • 1
    @Mr.UB Actually now that OP has told us they have an NSArray, the linked answers would not be sufficient since OP would have to cast their NSArray to a Swift array first (screenshot). Commented Sep 14, 2016 at 11:31

1 Answer 1

1
  • Convert NSArray to Swift Type [[String]]:

    let NameArray:NSArray = [["Din.pdf","img.jpeg"], [], [], [ "41_58" ]]
    
    let swiftArray = NameArray as! [[String]]
    
    let flattenedArray = swiftArray.flatMap{ $0 }
    

Credits: Eric Aya and Flatten a Array of Arrays in Swift

  • If you do not want to convert it into Swift Type:

    let NameArray:NSArray = [["Din.pdf","img.jpeg"], [], [], [ "41_58" ]]
    
    let arrFiltered:NSMutableArray! = []
    
    for arr in NameArray {
        for a in arr as! NSArray {
            arrFiltered.addObject(a)
        }
    }
    
    print(arrFiltered)
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.