IOS Coding Tips for Beginners

1. Create view objects in Interface builder not in source code.

2. Flower brackets always be there in if conditions or while loops, even it has single statement.

3. Give one line space between @implementation and @synthesize

4. Open flower bracket “{” should start in next line.

5. Give one line space above conditional or loop statements like if, while, for.

6. The method name itself should resembles its behavior.

7. Remove unnecessary methods which are created by the file templates.

8. Use Edit->Format->Re-Indent to re-indent the selection of statements.

9. Don’t use string literals as keys, make the string literal as constant and use that. Place all constants in a common file.

10. Try to use property directly than calling the accessory method. for example Object.memberVariable than [Object memberVariable];

11. Try to define all literals in common file.

12. Instead of using literals, create meaningful constants for them in common file and use them.

13. Always group the things like constants, statements, etc.

14. The format for method signature should be like
– (void) methodName:(id) sender;

15. One line space between logical group of statements and two lines between physical groups.

16. Try to eliminate autorelease objects, instead use retain and release.

17. If you are declaring a UI object, please suffix the type to the variable. For example, if you are declaring a UILabel object with name ‘userName’, then name it ‘userNameLabel’.

18. Maintain all image names in lowercase with underscore dividing the inline words.

19. Directly access the variable instead of using property in the same class. For example, don’t access

UITableView mTableView;
@Property () UITableView *tableView;
//To reload the table in the same class use
[mTableView reloadData]; // correct
[self.tableView reloadData]; //wrong

Advertisements

How to get selected cell frame from UITableView

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGRect selectedCellFrame = [cell.superview convertRect:cell.frame toView:self.view];

IOS Coding Tips

1. Create view objects in Interface builder not in source code.

2. Flower brackets always be there in if conditions or while loops, even it has single statement.

3. Give one line space between @implementation and @synthesize

4. Open flower bracket “{” should start in next line.

5. Give one line space above conditional or loop statements like if, while, for.

6. The method name itself should resembles its behavior.

7. Remove unnecessary methods which are created by the file templates.

8. Use Edit->Format->Re-Indent to re-indent the selection of statements.

9. Don’t use string literals as keys, make the string literal as constant and use that. Place all constants in a common file.

10. Try to use property directly than calling the accessory method. for example Object.memberVariable than [Object memberVariable];

11. Try to define all literals in common file.

12. Instead of using literals, create meaningful constants for them in common file and use them.

13. Always group the things like constants, statements, etc.

14. The format for method signature should be like
        – (void) methodName:(id) sender;

15. One line space between logical group of statements and two lines between physical groups.

16. Try to eliminate autorelease objects, instead use retain and release.

17. If you are declaring a UI object, please suffix the type to the variable. For example, if you are declaring a UILabel object with name ‘userName’, then name it ‘userNameLabel’.

18. Maintain all image names in lowercase with underscore dividing the inline words.

19. Directly access the variable instead of using property in the same class. For example, don’t access

UITableView mTableView;
@Property () UITableView *tableView;
//To reload the table in the same class use
[mTableView reloadData]; // correct
[self.tableView reloadData]; //wrong

How to disable UIWebView from user scrolling?

#import <objc/runtime.h>

    id scroller = [[Webview subviews] lastObject];
    int count;
    Method *method = class_copyMethodList([scroller class], &count);
    int i;
    for (i=0; i<count; i++) {
        if (strcmp(method_getName(method[i]), "setScrollingEnabled:") == 0)
            break;
    }
    IMP test = method_getImplementation(method[i]);
    test(scroller, @selector(setScrollingEnabled:), NO);

Blogged with the Flock Browser

How to get country name from iPhone locale settings

    NSLocale *locale = [NSLocale currentLocale];
    NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
   
    NSString *countryName = [locale displayNameForKey: NSLocaleCountryCode
                                                                             value: countryCode]];

Blogged with the Flock Browser

Encryption for cocoa, cocoa touch

how to retrieve the user phone number programmatically?

NSString *phoneNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@”SBFormattedPhoneNumber”];

NSLog(@”Your phone number is %@”, phoneNumber);

Check these two links:
        http://www.alexcurylo.com/blog/2008/11/15/snippet-phone-number/
        http://stackoverflow.com/questions/193182/programmatically-get-own-phone-number-in-iphone-os

Blogged with the Flock Browser