<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">It can be (more-or-less) solved in library code today:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class="">extension NSObjectProtocol {</div></div><div class=""><div class=""> public func with(@noescape fn: Self -> Void) -> Self {</div></div><div class=""><div class=""> fn(self)</div></div><div class=""><div class=""> return self</div></div><div class=""><div class=""> }</div></div><div class=""><div class="">}</div></div></blockquote><div class=""><br class=""></div>This way, you can do, on NSObjects:<div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">textLabel.with {</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>$0.textAlignment = .Left</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>$0.textColor = .darkTextColor()</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">}</blockquote><div class=""><br class=""></div><div class="">I love this pattern.</div><div class=""><br class=""></div>You can also make it a function to make it work with any value of any kind (it will then take form of `with(foo) { …}`).<div class=""><br class=""></div><div class="">Ideally, if you could write a universal extension (something like `extension Any`), you could just add this behavior, with method syntax, to everything.</div><div class=""><div class=""><br class=""><div class="">
<div class="">— Radek</div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><p class="p1">I recently learned some VBA and I found a very conveniently <span class="s1">`with`</span> statement.</p><p class="p1"><span class="s1">`with`</span> statement can be helpful to set property for UIKit instance.</p><p class="p1">for instance a UILabel instance `textLabel` ,with `with` statement we can set UILabel property like this</p><p class="p2"><br class=""></p><p class="p3">```swift</p><p class="p3">with textLabel{</p><p class="p4"><span class="s1">        </span>.textAlignment<span class="s1"> = </span>.Left</p><p class="p4"><span class="s1">        </span>.textColor<span class="s1"> = UIColor</span>.darkTextColor<span class="s1">()</span></p><p class="p4"><span class="s1">        </span>.font<span class="s1"> = UIFont</span>.systemFontOfSize<span class="s1">(</span><span class="s2">15</span><span class="s1">)</span></p><p class="p3">}</p><p class="p3">```</p></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></div></body></html>