Sunday, July 27, 2014

Understanding IBOutlet and IBAction in InterfaceBuider

In XCode interface builder, when adding a UI element in storyboard, it only creates an UI element, for example UITableView or UILabel, or UIViewController, and it does not give the element reference to be accessed by external module.

However, each of these class can define property, method and IBOutlet & IBAction for external access. Actually, the IBOutlet is just a regular property, the only extra thing IBOutlet does is telling Interface Builder that this property is available to associate with other instance within the current uiViewController scope at design time, so developer can make the association easily in interface builder between IBOutlet and other elements.

So IBOutlet is only meaningful within InterfaceBuilder's UIViewController context so as to allow design time association. Similarly, the Referencing outlet shows the current UI element instance is referenced by all other instances' outlet property.

Note, you can add any object into interface builder, and they will be allocated at runtime by interface builder, and any IBOutlet can associate with those arbitrary object, even if they are nothing to do with UIView. 

Tuesday, July 22, 2014

Property, and its instance variable, accessors with ARC

Before ARC, directly access instance variables will not affect the object's references—it did not extend the lifetime of the object. If the instance variable is associated with a property, then the property should be used to access the object, so that the reference can be properly managed based on property reference attribute (strong, weak, etc).

With ARC, instance variables are strong references by default—assigning an object to an instance variable directly will extend the lifetime of the object, unless the instance variable has __weak attribute. 


By default, a readwrite property will be backed by an instance variable, which will again be synthesized automatically by the compiler. Unless you specify otherwise, the synthesized instance variable has the same name as the property, but with an underscore prefix. For a property called firstName, for example, the synthesized instance variable will be called _firstName.

By default, the property's accessor methods are synthesized automatically by the compiler.
The synthesized methods follow specific naming conventions:
  • The method used to access the value (the getter method) has the same name as the property.
    The getter method for a property called firstName will also be called firstName.
  • The method used to set the value (the setter method) starts with the word “set” and then uses the capitalized property name.
    The setter method for a property called firstName will be called setFirstName:.

Override and overload in objective C

Objective C is message based and it has the full support on override. So if a method with the same signature is implemented in both the super class and derived class, calling the method will invoke the implementation in the derived class.

The object c does not support overload, so you cannot define or implement method with the same method name and parameter name but different return types or parameter types. The reason is objective C uses selector to distinguish different method, and select only use the information of method name and parameter name, and ignore the type information, for example, NSSelectorFromString method will only take method name and parameter name as input without parameter type information.

As a result, if a class has method only methods only different from parameter types, they will be regarded as the same method. This can particularly cause problem if the two methods are defined in super and derived class separately.

Automatically select client certificate by Chrome on Mac

Two steps are required to automatically select client certificate without user interaction.
1. trust the certificate:
open keychain utility and select the client certificate, open the certificate detail by double click it. Open trust section,  in "When using this certificate" dropdown list box, select "Always trust"

2. automatically pick the certificate
create or edit file "/Library/Preferences/com.google.Chrome.plist" and insert code similar to below sample code to match the client certificate information based on the host url pattern information:

<plist version="1.0">
<dict>
  <key>AutoSelectCertificateForUrls</key>
   <array>
     <string>{"pattern":"[*.]sap.corp","filter":{"ISSUER":{"CN":"SSO_CA"}}}</string>
     <string>{"pattern":"[*.]sap.com","filter":{"ISSUER":{"CN":"SSO_CA"}}}</string>
     <string>{"pattern":"[*.]sap-ag.de","filter":{"ISSUER":{"CN":"SSO_CA"}}}</string>
   </array>
</dict>
</plist>