While checking a parser of a JSON in swift, I found the following code:
description = "desc" <~~ json
I suppose that it is similar to use the following:
description = json["desc"]
Is it correct? if no, what does this operator mean?
Thanks
Simply use a shortcut to "range": you can construct a range and "~=" means "contains". (
The underscore indicates that there is no external parameter name for the function. Apple's Swift Documentation talks about this concept in terms of when you're writing your own functions.
In swift, we use the let keyword to declare a constant variable, a constant is a variable that once declared, the value can not be changed.
The colon in the declaration means “…of type…,” so the code above can be read as: “Declare a variable called welcomeMessage that's of type String .” The phrase “of type String ” means “can store any String value.” Think of it as meaning “the type of thing” (or “the kind of thing”) that can be stored.
You are right. But it would be wrong to assume that's what it is set out to do in Swift.
I think the parser that was being used was Gloss, and it seems that they have written an operator overload specifically to mean description = json["desc"]
(and or or some other stuff under the hood to make the parsing easier) . The operator does not have a meaning per se in Swift. But it's invented by the framework to do the parsing.
You can read about operator overloading here
EDIT
I always have incorrectly used the terms operator overloading and defining custom operator interchangeably. Operator Overloading is extending the implementation of the existing operators which is different than defining your own custom operators. Thank you SO MUCH for pointing this out, @Giacomo Alzetta!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With