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 ~

The recent transfer from OC to Swift2, because Swift has not been used in the formal classics, so the understanding of the language is basically a ball ... I have to feel the speed of the Apple Movement. Swift hasn't learned it yet. It's 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 self-active layout, or the old saying, abbreviation is SB (StoryBoard) things can be used to where to go ~ ~ but also because this has encountered some problems ~ when writing the VFL in multiple views how to get the dictionary? 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.

。 I can't even write the code for this kind of soil hammer. Suddenly there is a feeling to be prostitution. decided to switch to storyboard to do the Interface.


finally, The result is.

。。 My storyboard with too unskilled or Xcode7 storyboard still have bugs, each time you open the Xcode part of the control will change the size of the design interface and the execution of the interface to display different warnings, At the same time with the addition of SB content the computer will change the card (although the contribution of the lossless player is even 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 ... overall, There is no positive impact on Efficiency.

。。 well, at least for Me.


well, handwritten self-active layout is true Love.

。。


So the question goes back to the origin-what should be handled in Swift in the Hand-written self-layout of the Hemp rib place.


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 infer the object in the objects of the variable name of the view, 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 easy, most of the handwritten self-active layout is written for Uiviewcontroller. But there will still be in the UIView to write their own active layout of the 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, I wrote this method and wrote something else with it. It's a little thing.-simply put, the normal VFL statement is written in the general Way.


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


I personally do not like to write pure string, 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 again 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, or even slightly affect the readability, but suppose enough familiarity is not a problem ~


The method is very easy:


<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 clearly a more reasonable approach, but the problem is that this approach is equivalent to an inline method. Use the return value directly, assuming that ' String ' is required to add a '! ' to each use, it's too Painful.

。 So I made a shameless compromise--using this method, the VFL became


|-[\ (namefor (view1)) (==\ (namefor (view2)))][\ (namefor (view2))]-|


Assuming that there is any merit in this writing, 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, write not look so troublesome ~ In addition to become longer than others are very good ~ How to write a matter of Opinion.

Alternatives to nsdictionaryofvariablebindings in Swift

Related Article

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.