Can't set the property to hold URLSession, page 315

Hi, So I am on page 315 of the 5th edition of the book and i am getting an error (see where my comment is)
Any help is appreciated, I have uploaded a pictures showing the error. Thanks.

import Foundation

class PhotoStore {

let session: URLSession = {  //ERROR HERE
    let config = URLSessionConfiguration.default
    return URLSession(configuration: config)



I always do this too. It just means we forgot to put () at the end of the closure.

let session: URLSession = {
    let config = URLSessionConfiguration.default
    return URLSession(configuration: config)
}() <----

oh my… thank you for that. Just wasn’t paying attention.

The error is saying that you assigned a function having the signature:

( ) -> _

to a variable of type URLSession, and Swift can’t convert a function to a URLSession. Typically a function signature will look something like this:

(String, Int) -> Int

For instance, examine the following code:

let myMethod = {
    (arg1: String, arg2: Int) -> Int in
    return arg2 + 3

let return_val = myMethod("hello", 0)    //=>3


A closure/block is just an anonymous function, i.e. a function without a name, and if you assign it to a variable, you can call the variable just like a function.

But look at the error message here:

let x: Int = myMethod

cannot convert value of type '(String, Int) -> Int' to specified type 'Int'

Xcode refers to the anonymous function using its type or function signature, i.e a notation that lists the args and the return value.

The function signature specified in your error message:

( ) -> _

refers to a function that takes no args (your block/closure takes no args), and I think Xcode doesn’t really need to continue any further to determine what the return type of your closure is, so it just specifies _. In any case, when you see something of the form:

(possibly empty in here) -> something

you know that Xcode is referring to a function–in your case one that takes no args and returns some unknown type.