Alternatives to nsdictionaryofvariablebindings in Swift

Source: Internet
Author: User

There are days did not write things, take some time to practice lianbi, diligence desolate in the hippie ~

Recently transferred from OC to SWIFT2, because Swift has not been used in the formal classics, so the understanding of the language is basically a ball ... Have to feel the speed of the Apple action, Swift has not learned it, on the 2 ... Then realize that it may be true that you don't learn 2 ~ ~ took some time to read throughout the "the Swift programming Language" Finally can write something with him ~ ~ followed by the question came out


Accustomed to handwritten automatic layout, or the old saying that the abbreviation is SB (StoryBoard) things can use where to go ~ ~ but also because this has encountered some problems ~ How to write the VFL dictionary when writing to multiple views? Because Swift does not support the macro definition, that people feel the kind of ' nsdictionaryofvariablebindings ' directly so waste, online search, there is no serious solution ~ ~ own handwriting Dictionary ... This kind of soil hammer code I also really can't write out, suddenly have to be prostitution feeling, decided to switch to storyboard to do interface.


The end result is ... My storyboard with too unskilled or Xcode7 storyboard still have bugs, each time you turn on Xcode added constraint control will change the size of the design interface and run the interface display different effects of the warning, Meanwhile with the addition of SB content the computer will change the card (although the contribution of the lossless player is greater ...) ), it is a disaster to find a specific content ... As for the ScrollView design in IB is the inability to spit groove ... After all, the abbreviation is SB's thing ... In general, there is no positive effect on efficiency ... Well, at least for me.


Well, handwriting auto-layout is true love ...


So the question goes back to the origin-what to do with the handwritten auto-layout in Swift where the rib ...


Think, since can not be handled by the macro, then the method you always stop me ~ The idea is to pass the view array, and then through the runtime to determine the object of these several view variable name, manually create an array to simulate the function of the macro is good ~ So wrote a extension for NSObject. Why the NSObject? The reason is very simple, most of the handwriting automatic layout is written for Uiviewcontroller, but there will still be in UIView internal writing automatic layout situation appears ~ So nsobject more Reasonable


The idea has, the content is relatively simple ~


<span style= "FONT-SIZE:14PX;" >    func dictforviews (Views:[uiview]), [String:uiview] {        var count:uint32 = 0        var dicts:[string:uivi EW] = [:] let                ivars = Class_copyivarlist (Self.classforcoder, &count) for        var i = 0; i < Int (count); ++i{
   let obj = Object_getivar (self, ivars[i])            if let temp = obj as? uiview{                views.contains (temp) let                name = String.fromcstring (Ivar_getname (ivars[i]))!                Dicts[name] = Temp                if Dicts.count = = views.count{Break}}                (ivars)                return dicts    } </span>



The code that generates the dictionary can be simply written


<span style= "FONT-SIZE:14PX;" >let views = Dictforviews ([view1,view2]) </span>



Print it out:


[VIEW2: <UIView:0x7f8820fcb610; frame = (0 0; 0 0); layer = <calayer:0x7f8820fc1960>>

View1: <UIView:0x7f8820fe17b0; frame = (0 0; 0 0); layer = <CALAYER:0X7F8820FBE560>>]


OK, basic needs to be done.


By the way, because wrote this method and wrote another thing with the use, is also a small thing ~ to put it simply, the normal VFL statement is written in general this way


|-[view1 (==VIEW2)][view2]-|


I personally do not like to write pure strings, because sometimes for the readability of the program, the variable name will be defined for a very long ~ and in order to write the VFL will be written over and over the name is really painful, even if the copy and paste is a troublesome thing, so write a way to get the property name by Object ~ The string insertion mechanism with Swift is still good to use, although the VFL itself will be long, even slightly affect the readability, but if enough familiarity is no problem ~


The method is simple:


<span style= "FONT-SIZE:14PX;" >     func namefor (view:uiview), string{        var count:uint32 = 0 Let        ivars = Class_copyivarlist ( Self.classforcoder, &count) for        var i = 0; i < Int (count); ++i{let            obj = Object_getivar (self, ivars[i]) 
   if Let temp = obj as? uiview{                if temp = = = View {                    return string.fromcstring (Ivar_getname (ivars[i]))!                }} Free (ivars)                return ""    }</span>


This returns ' string ' instead of ' string '.

For the normal swift method ' string ' is obviously a more reasonable way, but the problem is that this method is equivalent to an inline method, directly with the return value, if the use of ' string ' will need to add a '! ' in each use, it is too painful ... So I made a shameless compromise-using this method, the VFL became


|-[\ (Namefor (View1)) (==\ (Namefor (VIEW2)))][\ (Namefor (view2))]-|


If this is a good thing to say, I think there should be at least two points, one is to avoid the pure handwritten string caused by the input error, the second is to change the variable name by refactoring to eliminate the trouble of string substitution ~ at the time of writing because the methods and variables are smart hints, it does not seem so cumbersome to write ~ In addition to become longer than others are very good ~ How to write a matter of opinion.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Alternatives to nsdictionaryofvariablebindings in Swift

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.