A
A
Aleksandr Govorukhin2017-06-03 18:12:05
iOS
Aleksandr Govorukhin, 2017-06-03 18:12:05

How to use dispatch_async in Swift 3?

Hello!
I'm trying to parse text using api in VK, but there are some problems with returning text, I read on the Internet that I need to use something asynchronously there and they explain it in a language that is not entirely clear to me for implementation)) Can anyone tell me how to return text?

func getJSON(rs: String, tx: String) -> String {
        
        var text = ""
        
        let inputURL = self.inputURL.text
        
        let link = inputURL?.components(separatedBy: "wall")
        
        let baseURL = "https://api.vk.com/method/wall.getById?posts=\(link![1])&access_token=87c166c356c35f6d835048af60d84014fa57d4eae&v=5.64"
        
        let url = NSURL(string: baseURL)
        let request = NSURLRequest(url: url! as URL)
        let session = URLSession(configuration: .default)
        let task = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
            
            if error == nil {
                
                let swiftyJSON = JSON(data: data!)
                let response = swiftyJSON[rs].arrayValue
                
                for tobject in response {
                    
                    let text = tobject[tx].stringValue
                }

            }
        }
        task.resume()
        
        return text
    }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Aleksandr Govorukhin, 2017-06-07
@SnapSh0t

func parse(callback: @escaping (_ text: String, _ images: [String]) -> Void) {
        
        let baseURL = "https://api.vk.com/method/wall.getById?posts=-34451036_490279)&access_token=5502c502caef0d55e1758dd&v=5.64"
        
        let url = NSURL(string: baseURL)
        let request = NSURLRequest(url: url! as URL)
        let session = URLSession(configuration: .default)
        let task = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
            
            if error == nil {
                
                let swiftyJSON = JSON(data: data!)
                let response = swiftyJSON["response"].arrayValue
                var links = [String]()
                
                for tobject in response {
                    
                    let text = tobject["text"].stringValue
                    let attachments = tobject["attachments"].arrayValue
                    
                    for photo in attachments {
                        
                        let searchPhoto = photo["photo"].dictionary
                        
                        if let val = searchPhoto?["photo_130"]?.stringValue {
                            
                            links.append(val)
                        }
                    }
                    
                    callback(text, links)
                }
            }
        }
        task.resume()
    }


 parse(callback: { (text, links) in
            recipe.descriptionRecipe = text
            (UIApplication.shared.delegate as! AppDelegate).saveContext()
            self.goToHome()

        })

N
nanosem, 2017-06-03
@nanosem

func getJSON(rs: String, tx: String, completion: ((String) -> Void)) {
        DispatchQueue.global().async { [weak self]
        guard let strongSelf = self else { return }

        let inputURL = strongSelf.inputURL.text
        let link = inputURL?.components(separatedBy: "wall")
        let baseURL = "https://api.vk.com/method/wall.getById?posts=\(link![1])&access_token=87c166c356c35f6d835048af60d84014fa57d4eae&v=5.64"
        
        let url = URL(string: baseURL)
        let request = NSURLRequest(url: url! as URL)
        let session = URLSession(configuration: .default)

        session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in    
            if error == nil {        
                let swiftyJSON = JSON(data: data!)
                let response = swiftyJSON[rs].arrayValue
                for tobject in response {
                    completion(tobject[tx].stringValue)
                }
            }
        }
    }

Accordingly, in the place of the function call:
getJSON(rs: "", tx: "") { result in
// result - нужная строка
}

A
Anton Gorb, 2017-06-06
@AntonGorb

func getJSON(rs: String, tx: String, completion: @escaping ((String) -> Void)) {
        DispatchQueue.main.async { [weak self] in
            guard let `self` = self else { return }
            
            let inputURL = self.inputURL.text
            let link = inputURL?.components(separatedBy: "wall")
            let baseURL = "https://api.vk.com/method/wall.getById?posts=\(link![1])&access_token=87c166c356c35f6d835048af60d84014fa57d4eae&v=5.64"
            
            let url = URL(string: baseURL)
            let request = NSURLRequest(url: url! as URL)
            let session = URLSession(configuration: .default)
            
            session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
                if error == nil {
                    let swiftyJSON = JSON(data: data!)
                    let response = swiftyJSON[rs].arrayValue
                    for tobject in response {
                        completion(tobject[tx].stringValue)
                    }
                }
            }
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question