0

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?

2
  • if you edit fetchOGData to get table of strings and all other logic you can do that Commented Aug 12, 2016 at 11:09
  • You cannot pass array of urlString if argument accept a String, not unless you have source code which you can alter. You can use for-in loop to initialise multiple async operation to download data. If you want dependency or ability to cancel, than NSOperationQueue can help. Commented Aug 12, 2016 at 11:21

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that seems to me like the approach I wanted. Editing the git source isn't that handy.
But this function will takes time to execute all the url. so better to call this in background with dispatch.

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.