Skip to content

Framework developing

Amr Aboelela edited this page Mar 17, 2015 · 1 revision

Parent pages: Developing

##Coding general rules

  • Do not copy and paste any Copyrighted code even from .h files.
  • In git put your username descriptive as aaboelela, alinour, and not vague as developer, so when we do git blame we know who did what.

##Pragma mark

##Memory management

  • For every Create there should be Release, example:

    CGColorSpace aColorSpace = CGColorSpaceCreateDeviceRGB(); .... CGColorSpaceRelease(aColorSpace);

  • Use malloc and free instead of release / retain, for global variables, example: MyDataContext* data = (MyDataContext*) malloc(sizeof(MyDataContext)); // do some work with data free(data);

  • For every malloc there should be free

  • For every alloc or copy, or retain, there should be release or autorelease.

##Code optimization

  • Use CoreFoundation and C functions whenever you can as sending objective-C message is 7 times slower than calling a C function.

  • Use private headers, check [https://github.com/amraboelela/myOS/blob/master/frameworks/UIKit/UIView-private.h UIView-private.h] as example, this will enable you to share those c functions within the framework, while remaining private to outside world.

    • (void)_removeFromSuperview:(BOOL)notifyViewController; Got converted to: void _UIViewRemoveFromSuperview(UIView *view, BOOL notifyViewController);
  • Explaining each part of the name:

    • _: means it is private function.
    • UI: is the framework abbreviation.
    • UIView: is the class name
    • RemoveFromSuperview: is the function name
    • (UIView *view: is the object instance as the original function is instance function.
  • Any private function, implement it in c and not in objective-c, and if you want to share within the framework, then put its definition in the *-private.h file.

  • In framework developing we should direct access class parameters and not use @property as following: UIResponder *_UIApplicationFirstResponderForScreen(UIApplication *application, UIScreen *screen) { if (application->_keyWindow.screen == screen) { return application->_keyWindow->_firstResponder; } }

  • Use [http://developer.apple.com/library/ios/documentation/cocoa/conceptual/objectivec/Chapters/ocDefiningClasses.html#//apple_ref/doc/uid/TP30001163-CH12-TPXREF127 @package] in the class definition, so you can access the class parameters directly within the framework, while remaining private for the outside world.

##Private .h files

  • It is only private from the user (app developer). But not private from us So we can still use it in other frameworks, but we have to copy the -private.h files into /usr/local/include/ manually. The -private.h file is not included in GNUmakefile which means it won't be shipped to the app developers.

##Debugging errors

  • Two ways to debug errors:
    • Using backtrace of gdb
    • Run an example and use DLog to print different things out while running to close down on the root of the problem. Also u can do DLog inside any framework including CoreFoundation and reinstall it then run the example again and again till u figure out the problem.

##Curly brackets {} rules

  • Functions should be:
    • (UIResponder *)firstResponderForScreen:(UIScreen *)screen { if (application->_keyWindow.screen == screen) { return application->_keyWindow->_firstResponder; } }
  • if, for, while, switch or any loop, should be: if (application->_keyWindow.screen == screen) { return application->_keyWindow->_firstResponder; } else { // else code } for (...) { }

##Naming convention

  • Any variable not tied with property we use _ in its name as in CALayer.h :

    @interface CALayer : NSObject { @package id delegate; CALayer *modelLayer; CALayer *presentationLayer; ... CFMutableDictionaryRef _animations; // any variable not tied with property we use _ in its name CGFloat contentsScale; CATransform3D transform; BOOL _needsDisplay; NSDictionary *actions; NSDictionary *style; }

##See also

##External links

Clone this wiki locally