Skip to main content

iOS Senior Level [Part 4/4] : Final page

[1] Foundation vs Core Foundation
CoreFoundation is a general-purpose C framework whereas Foundation is a general-purpose Objective-C framework. Both provide collection classes, run loops, etc, and many of the Foundation classes are wrappers around the CF equivalents. CF is mostly open-source , and Foundation is closed-source.
Core Foundation is the C-level API, which provides CFString, CFDictionary and the like. Foundation is Objective-C, which provides NSString, NSDictionary, etc. CoreFoundation is written in C while Foundation is written in Objective-C. Foundation has a lot more classes CoreFoundation is the common base of Foundation and Carbon.

[2] iOS Directories
~/Documents, ~/Documents/Inbox
[both will store user generated files that can be exposed to user. Ex. email attachments]
~/Library [files not to user.]
~/tmp [System clears files if app not running. Not backed up by iTunes]

[3] Concurrency Operations
Operations can be configured to run a block of code synchronously or asynchronously.  Place them in operation queue (NSOperationQueue) to avail concurrency.
Cocoa provides three different types of operations:
Block operations : These facilitate the execution of one or more block objects.
Invocation operations :These allow you to invoke a method in another, currently existing object.
Plain operations :These are plain operation classes that need to be subclassed. The code to be executed will be written inside the main method of the operation object.

[4] GCD
Grand Central Dispatch is not just a new abstraction around what we've already been using, it's an entire new underlying mechanism that makes multithreading easier and makes it easy to be as concurrent as your code can be without worrying about the variables like how much work your CPU cores are doing, how many CPU cores you have and how much threads you should spawn in response. 

[5] Cocoa Manual Memory management rules

To increment the retain count of any object, send it the retain message. This is called retaining the object. The object is now guaranteed to persist at least until its retain count is decremented once more. To make this a little more convenient, a retain call returns as its value the retained object — that is, [myObject retain] returns the object pointed to by myObject, but with its retain count incremented.

When you say alloc (or new) to a class, the resulting instance comes into the world with its retain count already incremented. You do not need to retain an object you’ve just instantiated by saying alloc or new (and you should not). Similarly, when you say copy to an instance, the resulting new object (the copy) comes into the world with its retain count already incremented. You do not need to retain an object you’ve just instantiated by saying copy (and you should not).

To decrement the retain count of any object, send it the release message. This is called releasing the object.

If Object A obtained Object B by saying alloc (or new) or copy, or if Object A has said retain to Object B, then Object A should balance this eventually by saying release to Object B, once. Object A should assume thereafter that Object B no longer exists.


[6] Frame vs bounds
The frame of a view is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.
The bounds of a view is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).

[7] Naming convention for classes, objects and methods
Class names must be unique across an entire app. Use 2 letter prefix related to company name or application name or specific component. If multiple words needed, then Camel case letter is preferred to use.
Method name should be expressive and unique within a class. It do not have prefix, and it should start from the lowercase letter; camel case for multiple words.
Local variable must be unique within the same scope.

[8] NSOperationQueue vs GCD
GCD :Simple, light weight, Low level C based API focused on lock free algorithms and performance.
NSOperationQueue : Internally implemented using GCD. Recommended to use NSOperationQueue instead of GCD unless specific it doesn’t support.

[9] Why Objective – C  is a dynamic programming language ?
Static typed languages are those in which type checking is done at compile-time, whereas dynamic typed languages are those in which type checking is done at run-time.
—- in other words —
Objective-C is a dynamically-typed language, meaning that you don't have to tell the compiler what type of object you're working with at compile time. Declaring a type for a variable is merely a promise which can be broken at runtime if the code leaves room for such a thing. You can declare your variables as type id, which is suitable for any Objective-C object.

[10] Cocoa and Cocoa Touch Frameworks
Uses 2 frameworks:
    “Cocoa” (OS X) = Foundation framework + Appkit
“Cocoa Touch” (iOS) = Foundation framework + UIKit   [In iOS,  Appkit replaced with UIKit]  


[11] iOS App (multi)layered Architecture

Cocoa Touch : Responsible for View Controllers, Story board, Gestures and event ending.
Includes following frameworks - UIKit, Map, Push notification service, Message UI, Game Kit, iAd.

Media: Audio/Video frameworks like Core Video, Core Graphics, Core Image, Image I/O, OpenGL, GL Kit, Asset Library, Quartz (2D), Core Animation, Core Audio, AVFoundation.

Core Services : Object Oriented cover over OS. Taking care of  GCD, iCloud storage, inAppPurchase, Foundation framework., Core Data, SQLite library, Core Foundation, Core Location.

Core OS : Low level Networking, External Accessories, memory allocation, threading, standard I/O. file system, A MAC 4XBSD unix kernel with C API.




[12] App States

 Not running State: The app has not been launched or was running but was 
terminated by the system.

 Inactive state: (Foreground)  The app is running in the foreground but is currently not receiving events. ( It may be executing other code though.) An app usually stays in this state only briefly as it transitions to a different state. The only time it stays inactive for any period of time is when the user locks the screen or the system prompts the user to respond to some event, such as an incoming phone call or SMS message.

 Active state (Foreground): The app is running in the foreground and is receiving events. This is the normal mode for foreground apps.

Background state: The app is in the background and executing code. Most apps enter this state briefly on their way to being suspended. However, an app that requests extra execution time may remain in this state for a period of time. In addition, an app being launched directly into the background enters this state instead of the inactive state.

Suspended state: The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code. When a low­ memory condition occurs, the system may purge suspended apps without notice to make more space for the foreground app.



[13] App Life/Launch Cycle

[a] Main Function :
App entry point “main” function,  Creates application level Autorelease pool.
Hands control off to UIKit framework by calling UIApplicationMain.

[b] UIApplicationMain invokes UIApplication, loads storyboard, reads info.plist for application setups. Sets UIApplicationDelegate, that will create UIWindow object and RootViewController.

[c] Main event loop :  At launch time that later processes user event such as gestures, sensor readings, redraw.

[d] Background Message : The executes code at background but not received any events. However , mostly it will be on the way of suspended state.

[e] Termination of app : System initiates app termination when it misbehaves, not responding events, out of memory. 






[14] Difference between #import in Obj-C and #include in C?
#import contains some logic that will make sure the file included only once. Avoids recursive inclusion. Where it not in the  case of #include.
#import is an improved version of #include.
#import brings the entire header file into the current file.

[15] NSOperationQueue vs GCD
GCD :Simple, light weight, Low level C based API focused on lock free algorithms and performance.
NSOperationQueue : Internally implemented using GCD.
Recommended to use NSOperationQueue instead of GCD unless specific it doesn’t support. because, - It can be pause, resume or cancel operations
KVO capabilities upon NSOperation class properties.
Dependencies to execute operations in specific order.
Specific number of operations can be queued.

[16]  Exceptions vs Error
 Exceptions represent programmer-level bugs like trying to access an array element that doesn’t exist. They are designed to inform the developer that an unexpected condition occurred. Since they usually result in the program crashing, exceptions should rarely occur in your production code.

Popular posts from this blog

Animating label text update - choosing a better way

Recently I published a countdown app .  At one point of development - I have to show a timer on a UILabel which ticks on each seconds. As usual I started  setting text to a label object - self .timerLabel.text = someString Easy piece of cake right !?   But wait ... it won't take much user attention when timer ticks on every seconds. So I decided to make use of a simple animation while label gets text update. I found there are dozens of ways to animate a label. In this short article, I listed 3 best way you can animate text on a label. ( Spoiler Alert 👀- I decided to go with 3rd option)  1. Fade In - Fade out animation : CATransition class has got transition type `fade`. With timing function of CATransition - I was able to see the below result. let animation: CATransition = CATransition () animation.timingFunction = CAMediaTimingFunction (name: CAMediaTimingFunctionName .easeInEaseOut) animation.type = CATransitionType .fade animation.subtype = C...

Prevent Navigationbar or Tabbar overlapping Subview - solved for Card view

Recently, I started with a Card view added as a subview of UIView in a view-controller. When a view controller created along subviews, it tends to use entire screen bounds and also slips behind Tab bar or Navigation bar. In my current situation, it's second case. Casually new iOS developers will write a patch by additional value for coordinate y and subtracting bar height from its size. A lot of them posted in SO threads too : How to prevent UINavigationBar from covering top of view? View got hidden below UINavigationBar iOS 7 Navigation Bar covers some part of view at Top So, how I got solved ? self.edgesForExtendedLayout = [] This  will avoid all subviews in a view controller get behind any bars. Read full apple  documentation on here. Full Source code below :  //Simple view controller where its view layed-out as a card. class WidgetCardViewController : UIViewController { var containerView = UIView () //MARK:- View Controller Life Cyc...

Printing Staircase Pattern : Swift coding challenge

In this post, we can try interesting pattern printing challenge in Swift. The problem statement goes like this " Print a staircase of given size 'n'. Make sure that its base and height are both equal to n, and the image is drawn only using `#` symbols and spaces. The last line is not preceded by any spaces." Expected Output : # ## ### #### ##### ###### Working solution: func makePatternOf ( _ size : Int ) { var str = "" // 1 for index in ( 0 ..< size ) { let stop = size -index-1; // 2 for _ in 0 ..< stop { str. append ( " " ) ; } // 3 for _ in 0 .. . index { str. append ( "#" ) ; } print ( str ) str = "" } } makePatternOf ( 6 ) Loop to visit every row of stair case. Loop for appe...

Implementing autocompletion OTP field in iOS

Long waiting is over. !!  iOS 12 brings Autofill for OTP text field which is close to Android provided a decade back. Previously in iOS we used to toggle between OTP text screen and message inbox.  Which was hard to remember and time consuming resulting a bad user experience. Personally, I have been asked from the client/customer couple of times to implement autocompletion for OTP field and took me a lot of time to convey that it is not possible in iOS. Why Autofill was not possible previously?  We all know that Apple gives at most care for user privacy. When we see iOS architecture, each individual app is like a separate island. There is no inter-app bridge between apps (exception for Keychain and URLSchemes APIs which gives very limited scope). Thus we cannot read message content from inbox. Where to start Autofilling? First of all, the target SMS need to have the OTP Code with prefix string "Code" or "Passcode"on its message content. Beware of OTP c...

UICollectionViewCell shows with wrong size on First time - Solved

We commonly use Collection view where its cell size calculated run time. The flow layout delegate is responsible to return individual cell sizes. BUT in most of the cases, delegate method `collectionView: layout sizeForItem:` expects cell size too early. Before generating actual cell size. extension YourViewController : UICollectionViewDelegateFlowLayout { func collectionView ( _ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize (width: externalWidth, height: externalHeight) } } For instance, if a cell size depends on external view and its frame is not yet ready - results with wrong (or outdated) cell size. Typically happens for the first time view controller laid out all views. You can find similar queries in StackOverflow community : Collection view sizeForItemNotWorking UICollectionViewCell content wrong size on first load How to refresh UICollec...