Swift ? Must Be Followed by a Call, Member Lookup, or Subscript

swift ? must be followed by a call, member lookup, or subscript

Actually the as? are all fine. The problem is this line:

if let id = cell.productId?

Just remove the question mark at the end of that. It makes no sense.

?'must be followed by a call, member lookup or subscript

What about this? Looks more elegant to me:

var name : String? {
let condition = true // your own condition here of course
return condition ? "I have a name" : nil
}

The problem in your code:

var name : String? {
var theName : String?

let condition = true // your own condition here of course
if condition {
theName = "I have a name"
}

return theName // get rid of the ? here
}

The field theName is already optional, no need to add another ? there.

Why is my proposed solution not an alternate solution:

The construct I used is called ternary operator:

The ternary conditional operator is a special operator with three parts, which takes the form question ? answer1 : answer2. It is a shortcut for evaluating one of two expressions based on whether question is true or false. If question is true, it evaluates answer1 and returns its value; otherwise, it evaluates answer2 and returns its value.

It behaves like the if statement but is suitable here as it is shorter and thus clearer to read: Depending on the condition, the value is either theName or nil. You really don't need to assign the value to any other variable, because, afterall, you are computing it, so might as well simply return it as the condition decides, what the value is.

Swift 1.2 Results to '?' must be followed by a call, member lookup, or subscript

In Swift 1.2, it is illegal to follow a variable with ? to unwrap it. The ? is used in an optional chain and must then be followed by a method call, a member lookup (i.e. a property), or a subscript as the error message said.

In the comments you added:

If I remove the "?" the code complies ok but what happens when a node
doesn't have a name?

It is perfectly valid to compare a String? to a literal String value without unwrapping the variable first. If the optional is nil, then nil is not equal to any literal String so the if will simply fail.

Try the following in a Swift Playground:

var str: String? = nil

if str == "hello" {
println("it is hello")
} else {
println("not hello") // prints "not hello"
}

// Here we reassign str, but it is still a String?
str = "hello"

if str == "hello" {
println("it is hello") // prints "it is hello"
} else {
println("not hello")
}

So it is completely safe just just compare paintedNode.name to "paintedArea" and if the node doesn't have a name then paintedNode.name will be nil and the if will fail just as if it had a different name.

convert ambiguous reference to swift 3

The easiest and most natural way to test type is the is keyword:

var superview = containerView.superview

if superview is UIWindow {
superview = containerView
}

What is wrong with this Swift syntax?

Sometimes swift cannot understand the type of an inline statement.
Have you tried:

Creating an external Boolean:

    [usernameTextField, passwordTextField, birthYearTextField, genderTextField].forEach {
let isHighlighted: Bool = ($0 === textField)
updateHighlightOnTextField($0!, highlight: isHighlighted)
}

Dynamically check if an object supports dynamic member lookup

I do not think this is possible at the moment.

However, after SR-8077
Compile error when using @dynamicMemberLookup as a protocol requirement bug gets fixed in a production version of swift (hopefully, in swift 5), we can have a workaround for that in the form of protocols, constrained by @dynamicMemberLookup.

You can also read here on why @dynamicMemberLookup was not implemented as a protocol:

We started with the approach of making this be a protocol that types conform to to get this behavior. It turns out that this behavior is very non-protocol like: it is not useful to define generic algorithms over, and existential values are only useful if they define a specific subscript that implements the requirements implicit in this attribute.

For these and other reasons, defining this as a protocol doesn't really fit into the design of Swift.

Any way to avoid ? mark to unwrap again and again in Swift?

You need to use conditional binding to avoid it.

For example:

var name : String?
name = "Hello world"

if let unwrappedName = name as? String
{
// Now unwrappedName isn't optional
}

Basically, unwrappedName will be String AKA not optional.

But it really all depends what you are trying to achieve, if you know for sure that name will never be nil I'd suggest using implicitly unwrapped:

var name : String!

But using implicitly unwrapped suggest that name could be nil thus the conditional binding need to be checked again - it all depends on how sure are you that the variable will never be nil.



Related Topics



Leave a reply



Submit