Search code examples
iosarraysswifttrim

How to trim Array items in swift?


my array looks like this:

myArray = [a (1), b (22), c (34), d (6)]

Is there any way i can get only the alphabets of the array like:

myTrimmedArray = [a, b, c, d]

If I can get each elemnts of arrray trim it and then append it in a new array also works fine for me. But is that possible?


Solution

  • var arrayCount = myArray.count
    var i: Int
        for(i = 0; i < arrayCount; i++){
            var name = myArray[i] as String
            var myStringArr = name.componentsSeparatedByString(" ")
            var arrayValue = myStringArr[0]
            self.myTirimmedArray.append(arrayValue)   //Output: myTrimmedArray = [a, b, c, d]
        }   
    

    In this instaed of componentsSeparatedByString I could use NSCharacterSet But this thing worked for my requirements