objective-c tricks
DESCRIPTION
These slides talk about tricks in Objective-C language while working in iOS apps. It contains info and tricks about Literals, NSString, CGRect and Method SwizzlingTRANSCRIPT
![Page 1: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/1.jpg)
1
Objective-c Tricks2014 – Jan
By: Hossam Ghareeb
![Page 2: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/2.jpg)
2
Use Literals
The following is the old way of writing code:
![Page 3: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/3.jpg)
3
The same code can be written as follows:
![Page 4: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/4.jpg)
4
Subscripting
You can use subscripting to access the values inside dictionaries and arrays. This is how the syntax looks:
![Page 5: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/5.jpg)
5
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
![Page 6: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/6.jpg)
* Required 6
We can also decide in run time which selector to be called based on input:
![Page 7: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/7.jpg)
* Required 7
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
![Page 8: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/8.jpg)
8
Example make a UINavigationBar green
T 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
![Page 9: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/9.jpg)
* Required 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
![Page 10: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/10.jpg)
10
Here is the swizzling:
![Page 11: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/11.jpg)
* Required 11
Another problem solved by Swizzling
measure how long the synchronize takes in NSUserDefaults
![Page 12: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/12.jpg)
12
Cont.
![Page 13: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/13.jpg)
13
Example 2
Borders around all UIViews initWithFrame: get a red border,
views that were unarchived (for example as part of XIBs) get a blue border.
![Page 14: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/14.jpg)
14
Surprise the status bar at the top ofan iOS app is drawn by the app itself
![Page 15: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/15.jpg)
* Required 15
How to do that?
![Page 16: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/16.jpg)
* Required 16
Cont.
![Page 17: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/17.jpg)
17
Other tricks objc_msgSend()
This function takes a target, a selector and a list of arguments
![Page 18: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/18.jpg)
18
CGRect Tricks Shrink CGRect:
Different insets for each edge:using UIEdgeInsets(top, left, bottom, right)
![Page 19: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/19.jpg)
19
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:
![Page 20: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/20.jpg)
20
Cont.
Dictionary Representation (to store in in plist on disk):
Print CGRect values :
![Page 21: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/21.jpg)
21
Cont. Storing it in Obj-C Storage Classes
![Page 22: Objective-C Tricks](https://reader033.vdocuments.site/reader033/viewer/2022061206/5481faa4b47959d80c8b465b/html5/thumbnails/22.jpg)
22
Thank you
Any Questions?