0

I've stumbled upon some code where the variable declarations use return to assign the variable value like below:

var method: HTTPMethod { return .Post}

Is the line above the same as below?

var method: HTTPMethod = .Post

3 Answers 3

1

The two lines are not so similar.

The first line declares a computed property without the setter. This means that the value is constant.

The second declaration is a simple property declaration with the value initialised to .Post. This means that the property can be changed later on.

The first line is more similar to the following line than to your second line:

let method: HTTPMethod = .Post

Since the above also declares a constant property.

Then what's the difference between a let constant property and a computed property without a setter and returns a constant value? The main difference is that a computed property can be put into extensions while let constants cannot.

Sign up to request clarification or add additional context in comments.

2 Comments

What do you mean when you say "can be put into extensions"
@Brosef extensions can only contain computed properties and methods, not stored properties. If you try to put a let constant into an extension, it does not work.
1

No both are different

This one is get-only property

var method: HTTPMethod { return .Post}

and second one is mutable object where you can assign different value.

var method: HTTPMethod = .Post

1 Comment

I understood in few Seconds. !
0
var method: HTTPMethod = .Post

The latter can be mutated if you change var to let the behavior would be the same as the first option.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.