Posts

Showing posts from August, 2017

Singleton static object

To create static shared object, that can be used across all view controllers .h + (id) sharedCustomAppController; .m static CustomApplicationController *sharedCustomAppController = nil; + (id) sharedCustomAppController {    @synchronized (self) {        if (sharedCustomAppController == nil) {            sharedCustomAppController = [[CustomApplicationController alloc] init];        }    }    return sharedCustomAppController ; }

NSError - Custom localised messages

NSError can be customised to deliver localized messages to the user. This is meant to show meaningful messages based on situations where and when error happens. 1. Create a strings file. As iOS supports localization, we need to add "strings" file to our project and add the error messages. "99" = "Exception occurred" "100" = "Login Successful."; "101" = "Logout failed"; "102" = "Unable to connect server"; "103" = "Invalid input data"; 2. Add two macro that would help to fetch the localized messages. #define ERROR_KEY(code)                    [NSString stringWithFormat:@"%d", code] #define ERROR_LOCALIZED_DESCRIPTION(code)  NSLocalizedStringFromTable(ERROR_KEY(code), @"Error", nil) // "Error" is the strings file name.

Debug logs

To enable debug logs in iOS application, add the following code in project PCH file. #if defined (DEBUGLOG) #define DebugLog( s, ... ) NSLog( @"%@", [NSString stringWithFormat:(s), ##__VA_ARGS__] ) #else #define DebugLog( s, ... ) #endif

Convert HEX code to RGB

To use hex code in UIColor, use the below macro. #define UIColorFromRGB(rgbValue) [UIColor \        colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \        green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \        blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] //Then use any Hex value self.view.backgroundColor = UIColorFromRGB(0xD2691E);