//: Playground - noun: a place where people can play
import Foundation
import XCPlayground
struct Object1 {
func sendRequest(toObject: Object2) {
let theUrl = URL(string: "http://google.com")!
let dataTask = URLSession.shared.dataTask(with: theUrl) {
data, response, error in
toObject.receiveResult(data: data, response: response, error: error)
}
dataTask.resume()
}
}
struct Object2 {
func receiveResult(data: Data?, response: URLResponse?, error: Error?) {
guard let validData = data else {
print("Failed to get Result")
return
}
print(validData)
if let resultHtml = String(data: validData, encoding: String.Encoding.ascii) {
print(resultHtml)
} else {
print("Couldn't get the string of the response data")
}
// Let the playground know that we have our result and exit....
XCPlaygroundPage.currentPage.finishExecution()
}
}
var sender = Object1()
var receiver = Object2()
sender.sendRequest(toObject: receiver)
// Hack for making sure we are allowed to continue execution while we wait for a response during the playground.
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true