swift - Declared closure result '?' is incompatible with contextual type '' - TagMerge
3Declared closure result '?' is incompatible with contextual type ''Declared closure result '?' is incompatible with contextual type ''

Declared closure result '?' is incompatible with contextual type ''

Asked 1 years ago
0
3 answers

You have products declared as [InstockProduct] (with non-optionals).

Also, in your closure, you have a return type specified as InstockProduct (again, non-optional).

But, in your return statement, you return an optional when you do this:

return try? queryDocumentSnapshot.data(as: InstockProduct.self)

Given that the try could fail, I'd change your code to return an optional and then use compactMap to remove those optionals from the final array:

self.products = documents.compactMap { (queryDocumentSnapshot) -> InstockProduct? in
    return try? queryDocumentSnapshot.data(as: InstockProduct.self)
}

Source: link

0

Consider the following example code:
function init() {
  var name = 'Mozilla'; // name is a local variable created by init
  function displayName() { // displayName() is the inner function, a closure
    alert(name); // use variable declared in the parent function
  }
  displayName();
}
init();
Consider the following code example:
function makeFunc() {
  var name = 'Mozilla';
  function displayName() {
    alert(name);
  }
  return displayName;
}

var myFunc = makeFunc();
myFunc();
Here's a slightly more interesting example—a makeAdder function:
function makeAdder(x) {
  return function(y) {
    return x + y;
  };
}

var add5 = makeAdder(5);
var add10 = makeAdder(10);

console.log(add5(2));  // 7
console.log(add10(2)); // 12
For instance, suppose we want to add buttons to a page to adjust the text size. One way of doing this is to specify the font-size of the body element (in pixels), and then set the size of the other elements on the page (such as headers) using the relative em unit:
body {
  font-family: Helvetica, Arial, sans-serif;
  font-size: 12px;
}

h1 {
  font-size: 1.5em;
}

h2 {
  font-size: 1.2em;
}
Here's the JavaScript:
function makeSizer(size) {
  return function() {
    document.body.style.fontSize = size + 'px';
  };
}

var size12 = makeSizer(12);
var size14 = makeSizer(14);
var size16 = makeSizer(16);

Source: link

0

{ [closureParameters -> ] statements }
{ item++ }                                          (1)

{ -> item++ }                                       (2)

{ println it }                                      (3)

{ it -> println it }                                (4)

{ name -> println name }                            (5)

{ String x, int y ->                                (6)
    println "hey ${x} the value is ${y}"
}

{ reader ->                                         (7)
    def line = reader.readLine()
    line.trim()
}
def listener = { e -> println "Clicked on $e.source" }      (1)
assert listener instanceof Closure
Closure callback = { println 'Done!' }                      (2)
Closure<Boolean> isTextFile = {
    File it -> it.name.endsWith('.txt')                     (3)
}
def code = { 123 }
assert code() == 123

Source: link

Recent Questions on swift

    Programming Languages