Kirill Titov Blog https://kirilltitov.com/en Personal blog of Kirill Titov. en Copyright 1989-2k17 Kirill Titov 2017-05-03T11:19:54+00:00 kirill@kirilltitov.com 60 Secret of Self in Swift https://kirilltitov.com/en/blog/2017/capitalized-self-in-swift <p>Anyone who study Swift programming language surely knows what keyword <code>self</code> means — it is reference to current object inside class instance context. Completely normal stuff in almost any objective language. The tricky part begins when one deals with static methods and properties as well as with the famous Swift's protocol-oriented paradigm. We won't emphasize attention on protocol philosophy, instead let's proceed to the most interesting part, and I'm quite sure anyone had business with it.</p> <p>I'm talking about calling static method from protocol extension or defining a protocol method which must return eventual type. All of that is a particular case of late static binding, and everybody may google it anytime.</p> <p>It's quite straight and simple in case of commonplace static method inside class or structure — method is easily called with type name, or you just call <code>type(of: self)</code> which returns current type and from that you may call static methods. However when there is no concrete type yet, but only protocol and/or its extensions (i.e. metatype), language simply won't let us do it because protocol cannot contain implementation and protocol extension is not considered as concrete code, hence we get compiler error <code>Static member 'staticMethodName' cannot be used on protocol metatype 'ProtocolName.Protocol'</code>.</p> <p>The solution is using keyword <code>Self</code> (exactly that, capitalized) which is a reference to eventual type conforming to protocol. Official documentation has <a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-ID374">only one mentioning</a> of this keyword:</p> <blockquote>In that [protocol] context, Self refers to the eventual type that conforms to the protocol.</blockquote> <p>Respectively, this keyword works only in metatype context (protocols and protocol extensions), in concrete classes or structures you get error <code>Use of unresolved identifier 'Self'</code>.</p> <p><script src="https://pastebin.com/embed_js/77qnj3Q8/noheader"></script></p> <p>P.S. All of it relates to Swift versions 3 and 4, and may be changed in future. I'll try to not forget to update this article if anything changes (it might, as we all know).</p> 2017-07-05T17:04:24+00:00 https://kirilltitov.com/en/blog/2017/capitalized-self-in-swift Ultra short stories https://kirilltitov.com/en/blog/2017/ultra-short-stories <p>Once I had hard time trying to sleep and decided to shoot the sunrise at our ponds.</p> <img src="https://c1.staticflickr.com/5/4288/35554828405_85f56b8b39_b.jpg" /> <img src="https://c1.staticflickr.com/5/4264/35554827925_dbc2b4322f_b.jpg" /> <img src="https://c1.staticflickr.com/5/4217/35168131640_b64d8df89a_b.jpg" /> https://kirilltitov.com/en/blog/2017/ultra-short-stories Railway dilogy https://kirilltitov.com/en/blog/2017/railway-dilogy <a class="img" href="https://c1.staticflickr.com/5/4204/34183493584_bd92dd8cb2_o.jpg"><img src="https://c1.staticflickr.com/5/4204/34183493584_6c0459c22b_b.jpg" /></a> <a class="img" href="https://c1.staticflickr.com/5/4272/35027197215_1eecba4e2e_o.jpg"><img src="https://c1.staticflickr.com/5/4272/35027197215_1d3866db44_b.jpg" /></a> https://kirilltitov.com/en/blog/2017/railway-dilogy Sergeant McPussy https://kirilltitov.com/en/blog/2017/cat <a class="img" href="https://c1.staticflickr.com/5/4270/34216685943_d6566a0aaa_o.jpg"><img src="https://c1.staticflickr.com/5/4270/34216685943_13b1c906e5_b.jpg" /></a> https://kirilltitov.com/en/blog/2017/cat Website update, astonishing! (no) (yes) (no) https://kirilltitov.com/en/blog/2017/rss <p>Now the website has an RSS-feed, one can add it wherever you want (damn you, Google!) and read me whenever you wish (as if you couldn't before, lol).</p> <p>In case your client didn't catch it by passing link to website itself (it should work, though), here is <a href="https://kirilltitov.com/en/rss">direct link</a> to feed.</p> 2017-05-03T11:22:51+00:00 https://kirilltitov.com/en/blog/2017/rss Trees and sea https://kirilltitov.com/en/blog/2017/trees-and-sea <img src="https://c1.staticflickr.com/3/2850/33594941562_546d8d795f_o.jpg" /> https://kirilltitov.com/en/blog/2017/trees-and-sea Short stories https://kirilltitov.com/en/blog/2017/short-stories-1 <p>Took a stroll.</p> <img src="https://static.kirilltitov.com/img/blog/stories/kiev-1/IMG_2408.jpg" /> 2017-01-16T10:49:02+00:00 https://kirilltitov.com/en/blog/2017/short-stories-1 Cliché https://kirilltitov.com/en/blog/2017/postrock <img src="https://static.kirilltitov.com/img/blog/stories/kiev-1/IMG_2425.jpg" /> https://kirilltitov.com/en/blog/2017/postrock Talking about lenses https://kirilltitov.com/en/blog/2016/new-lens-canon-24-70 <p>Some time ago I bought a successor for kit <a href="https://www.usa.canon.com/internet/portal/us/home/products/details/lenses/ef/standard-zoom/ef-s-18-135mm-f-3-5-5-6-is-stm">Canon EF-S 18-135mm 1:3.5-5.6 IS STM</a> lens (see <a href="/en/blog/tag/spb">St-Petersburg</a> photographs) — <a href="https://www.usa.canon.com/internet/portal/us/home/products/details/lenses/ef/standard-zoom/ef-24-70mm-f-4l-is-usm">Canon EF 24-70mm f/4L IS USM</a> lens (as everyday all-purpose lens) (often seen in 5D Mark III kit). As heaven from earth, really.</p> <img src="https://c7.staticflickr.com/1/597/31531521022_741cbfec3d_h.jpg" /> <p>Next stop — <a href="https://www.usa.canon.com/internet/portal/us/home/products/details/lenses/ef/telephoto-zoom/ef-70-200mm-f-2-8l-is-ii-usm">Canon EF 70-200mm f/2.8L IS II USM</a> for concerts and neighbors peeping.</p> 2016-12-17T10:55:34+00:00 https://kirilltitov.com/en/blog/2016/new-lens-canon-24-70 Leningrad in fall of 2016. Part 2. Details. https://kirilltitov.com/en/blog/2016/leningrad-2 <p>All crosswalks in SPb (as well as in the rest of Russia) are marked with zebras.</p> <img src="//static.kirilltitov.com/img/blog/spb-2016/main/IMG_0578.jpg" /> <p>Meanwhile in Ukraine zebra is drawn only on unregulated crossroads, but crossroads with traffic lights are marked with just a dashed corridor. I was the cause of around five nearly-accidents because of it, when I (realizing my legal awareness) crossed the road, not noticing the red light. Drivers (being stopped by my inexorable hand) was too amazed with my impudence to honk.</p> 2016-12-14T21:28:05+00:00 https://kirilltitov.com/en/blog/2016/leningrad-2