Asi Givati
Asi Givati

Reputation: 1475

Swift - Anonymous closure argument not contained in a closure

I'm learning the Swift lang with a tutorial.

I'm using this code:

let hasPrefixAndSuffix: (String,String,String) -> Bool
{
  var hasPrefix = $0.hasPrefix($1)
  var hasSuffix = $0.hasSuffix($2)
  return hasPrefix && hasSuffix
}

and I've this error:

Anonymous closure argument not contained in a closure.

Upvotes: 0

Views: 3869

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 187024

You forgot the = which actually assigns that closure to the variable.

let hasPrefixAndSuffix: (String,String,String) -> Bool = {
  var hasPrefix = $0.hasPrefix($1)
  var hasSuffix = $0.hasSuffix($2)
  return hasPrefix && hasSuffix
}

Upvotes: 1

Related Questions