Felix F. Felix F. - 3 months ago 10
Swift Question

Use Elements of Array as Input for Class which needs a String

I have a class from URLEmbeddedView which performs an operation on a given String. However I'm trying to use this

class
for a bunch of Strings after each other, and not just one. I prepared an
Array
of
String
, and now I want to perform this class on each element of an
Array
.

The code:

var linkArray = [String]() //Contains bunch of Elements

OGDataProvider.sharedInstance.fetchOGData(urlString: TheUrlString) { [weak self] ogData, error in
if let _ = error {
return
}

let pageTitle = ogData.pageTitle
Cell.titleLabel.text = pageTitle
}


So instead of
TheUrlString
I want to have my array, as I need the pageTitle for each element in that array. How can I accomplish this? Is it possible to use an if which loops through every element?

Answer

You can try like this

func fetchTitleOGData(index: Int) {
    if (index < linkArray.count) {
         OGDataProvider.sharedInstance.fetchOGData(urlString: linkArray[index]) { [weak self]     ogData, error in
             if let _ = error {
                  //Try again to fetch title
                  fetchTitleOGData(index)
             }
             else {
                 titleArr.append(ogData.pageTitle)
                 fetchTitleOGData(index + 1)
             }
         }
    }
    else {
         //All the title set do what you want.
    }
}

Now call this function like

self.fetchTitleOGData(0)
Comments