Objective-c Tricks
1
2 0 1 4 – Jan
B y : H o s s a m G h a r e e b
2
Use Literals
 The following is the old way of writing
code:
3
The same code can be written
as follows:
Subscripting
 You can use subscripting to access the
values inside dictionaries and arrays. This
is how the syntax looks:
4
Classes And Selectors From
Strings
 We can dynamically generate classes and
selectors from strings, We do this by using
the NSClassFromString and
NSSelectorFromString functions:
 NSClassFromString will return nil if there isn't a class
in the runtime that matches the string
5
 We can also decide in run time which selector to be called
based on input:
* Required 6
Method Swizzling
 Methods are made up of two components. The selector, which is an
identifier for a method, and the IMP, which is the actual implementation
that is run. One of the key things about this separation is that a selector
and IMP link can be changed. One IMP can have multiple selectors
pointing to it.
 Subclassing -> This allows you to override a method and call the original
implementation, but it means that you have to use instances of this
subclass.
 Category -> you cannot call the original implementation if you override
a method.
 Swizzling -> You can override a method without subclassing AND call
the original implementation
* Required 7
Example
 make a UINavigationBar green
 this will change all navigation bars in the app to
green, but if I used UIImagePickerController it will
be changed too!!!. We have to find new way for
that >>>>> Method Swizzling
8
Method Swizzling Cont.
 Method swizzling, in short, is switching methods at runtime. So
you can say for UINavigationBar don’t use the
standard drawRect:, but instead swap it with a different one
* Required 9
 Here is the swizzling:
10
Another problem solved by Swizzling
 measure how long the synchronize takes in
NSUserDefaults
* Required 11
Cont.
12
Example
 Borders around all UIViews
 initWithFrame: get a red border, views
that were unarchived (for example as
part of XIBs) get a blue border.
13
Surprise 
the status bar at the top of
an iOS app is drawn by
the app itself
14
How to do that ?
* Required 15
Cont.
* Required 16
Other tricks
 objc_msgSend()
This function takes a target, a selector and
a list of arguments
17
CGRect Tricks
 Shrink CGRect:
 Different insets for each edge:
using UIEdgeInsets(top, left, bottom, right)
18
Cont.
 CGRectUnion is UIScrollView’s best friend:
if you want use UIScrollView with bunch of
subviews, so you have to know only the top-left
and bottom-right views:
19
Cont.
 Dictionary Representation (to store in in plist on disk):
20
Print CGRect values :
Cont.
 Storing it in Obj-C Storage Classes
21
Thank you
Any Questions?
22

Objective C Tricks

  • 1.
    Objective-c Tricks 1 2 01 4 – Jan B y : H o s s a m G h a r e e b
  • 2.
    2 Use Literals  Thefollowing is the old way of writing code:
  • 3.
    3 The same codecan be written as follows:
  • 4.
    Subscripting  You canuse subscripting to access the values inside dictionaries and arrays. This is how the syntax looks: 4
  • 5.
    Classes And SelectorsFrom Strings  We can dynamically generate classes and selectors from strings, We do this by using the NSClassFromString and NSSelectorFromString functions:  NSClassFromString will return nil if there isn't a class in the runtime that matches the string 5
  • 6.
     We canalso decide in run time which selector to be called based on input: * Required 6
  • 7.
    Method Swizzling  Methodsare made up of two components. The selector, which is an identifier for a method, and the IMP, which is the actual implementation that is run. One of the key things about this separation is that a selector and IMP link can be changed. One IMP can have multiple selectors pointing to it.  Subclassing -> This allows you to override a method and call the original implementation, but it means that you have to use instances of this subclass.  Category -> you cannot call the original implementation if you override a method.  Swizzling -> You can override a method without subclassing AND call the original implementation * Required 7
  • 8.
    Example  make aUINavigationBar green  this will change all navigation bars in the app to green, but if I used UIImagePickerController it will be changed too!!!. We have to find new way for that >>>>> Method Swizzling 8
  • 9.
    Method Swizzling Cont. Method swizzling, in short, is switching methods at runtime. So you can say for UINavigationBar don’t use the standard drawRect:, but instead swap it with a different one * Required 9
  • 10.
     Here isthe swizzling: 10
  • 11.
    Another problem solvedby Swizzling  measure how long the synchronize takes in NSUserDefaults * Required 11
  • 12.
  • 13.
    Example  Borders aroundall UIViews  initWithFrame: get a red border, views that were unarchived (for example as part of XIBs) get a blue border. 13
  • 14.
    Surprise  the statusbar at the top of an iOS app is drawn by the app itself 14
  • 15.
    How to dothat ? * Required 15
  • 16.
  • 17.
    Other tricks  objc_msgSend() Thisfunction takes a target, a selector and a list of arguments 17
  • 18.
    CGRect Tricks  ShrinkCGRect:  Different insets for each edge: using UIEdgeInsets(top, left, bottom, right) 18
  • 19.
    Cont.  CGRectUnion isUIScrollView’s best friend: if you want use UIScrollView with bunch of subviews, so you have to know only the top-left and bottom-right views: 19
  • 20.
    Cont.  Dictionary Representation(to store in in plist on disk): 20 Print CGRect values :
  • 21.
    Cont.  Storing itin Obj-C Storage Classes 21
  • 22.