Reputation: 787
Recently I've started IOS development using Swift. I saw there are two ways of setting some attributes of UI elements:
(e.g self.actionLabel.textAlignment = .Center)
So the questions are:
May be for simple apps it doesn't make sense which way you use but I think in huge apps there could be a slight or considerable difference. Thanks in advance!
Upvotes: 1
Views: 67
Reputation: 25632
Even huge apps won't load tons of elements at a time, because screen space is rather limited. Bottom line: do not base your decision here on performance, aka "micro optimizations". If you don't do it completely wrong, you won't see any difference at runtime. But it might make a huge difference for your codebase and development.
As this came up some time before: if there are thousands of elements in the storyboard or nib file, you're doing it wrong anyways - they are intended as blue prints. For example, you won't create a dedicated element for each question in a quiz game, but just a template that gets filled with the concrete data at runtime.
Upvotes: 1
Reputation: 19954
Which way is preferred to set attribute value in terms of performance?
I'm not sure which is faster per se. Though I am fairly confident that your interface builder settings are loaded prior to your initial View Controller. This causes your Interface Builder settings to load first and your programmatic settings to load after (which will override your interface builder settings).
See this post: How does XCode load the main storyboard?
If attribute set via "Utilities > Attributes inspector" in which file does it stored?
Open your Main.storyboard in a text editor and you'll see that all of your settings are stored directly in XML:
<!-- started from random node -->
<rect key="frame" x="82" y="479" width="211" height="51"/>
<color key="backgroundColor" red="0.17207773850489261" green="0.46615564923228975" blue="0.77629813762626265" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="width" constant="211" id="S1M-Vg-xbE"/>
<constraint firstAttribute="height" constant="51" id="nr7-Cq-G9C"/>
</constraints>
<fontDescription key="fontDescription" name="AvenirNext-Medium" family="Avenir Next" pointSize="21"/>
<state key="normal" title="Login">
<color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</state>
<connections>
<action selector="handleLoginTapped:" destination="dtF-AM-7P4" eventType="touchUpInside" id="iVh-kl-1Hl"/>
</connections>
</button>
<imageView userInteractionEnabled="NO" alpha="0.10000000000000001" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="question" translatesAutoresizingMaskIntoConstraints="NO" id="a1j-ZC-tn9">
<rect key="frame" x="162" y="567" width="50" height="50"/>
<constraints>
<constraint firstAttribute="width" constant="50" id="PLN-f0-vSm"/>
<constraint firstAttribute="height" constant="50" id="Uga-aV-5p4"/>
</constraints>
</imageView>
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="wra_retina-2" translatesAutoresizingMaskIntoConstraints="NO" id="qkc-Mg-pmQ">
<rect key="frame" x="46" y="20" width="282" height="144"/>
<constraints>
<constraint firstAttribute="height" constant="144" id="OjZ-Nw-qwL"/>
</constraints>
</imageView>
<!-- And on, and on, and on it goes -->
Looking forward to other answers on this one.
Upvotes: 1
Reputation: 2407
Either ways have the same opportunities. Their performance are same. Sometimes you have to set your attributes in your code. The simple way is to set the attributes in your storyboard. However if you want to change your attributes in runtime you should use the attributes in your code.
Upvotes: 0