Reputation: 273540
I recently started learning iOS development and just loved using the interface builder of Xcode. It's so easy to add segues and outlets and "event handler" thingys (sorry I don't know what they are called).
However, I got confused when I add an outlet of a label or something and sees this gets added in the code
@IBOutlet var weak myLabel: UILabel!
I don't see any assignments to the variable! And when I use the variable, it is not nil
!
Back then when I was doing Windows Forms with C#, whenever I added something in the interface designer, some code is generated in a separate file to actually "add" the thing.
I know that storyboard files are just xml files. Whenever I do something to it, the xml changes. But I don't believe xml can do reflection to assign a value to my variable, can it?
So I deduced that there must be some code generated behind the scenes that reflected my code and assigns a value to the outlet variables. Where can I find this file? What exactly is added? Or is my guess wrong?
Upvotes: 1
Views: 274
Reputation: 1023
Inside the .storyboard files, the outlets you create are persisted like so:
<connections>
<outlet property="myLabel" destination="PM2-8V-67p" id="yvq-yW-6rD"/>
</connections>
The property
key value pair references the name of the variable it's attached to. The destination
KVP points to the element in your storyboard that it's attached to.
Yes, storyboards are simply XML, but when you build your project, they are compiled like any other source code, particularly into compiled NIB files. You'll find a compiled NIB file for each view in your storyboard. Then, when you load your view, it loads the appropriate compiled NIB into memory, creates the actual objects you've created representations for in your storyboard, and hooks up your outlet properties to those objects using the setValue:forKey:
accessor method (in iOS). You can find lots more information about the whole NIB lifecycle here:
Mac Developer Library: The Nib Object Life Cycle
As far as where the compiled NIB files exist, you can see them in your build folder (found mine in DerivedData/<YourBuildFolder>/Build/Products/Debug-iphoneos/<YourProjectName>/
). Ones generated from storyboards can be found in the Base.lproj folder.
Upvotes: 2