iphone - What are best practices that you use when writing Objective-C and Cocoa? -


i know hig (which quite handy!), programming practices use when writing objective-c, , more when using cocoa (or cocoatouch).

there few things have started do not think standard:

1) advent of properties, no longer use "_" prefix "private" class variables. after all, if variable can accessed other classes shouldn't there property it? disliked "_" prefix making code uglier, , can leave out.

2) speaking of private things, prefer place private method definitions within .m file in class extension so:

#import "myclass.h"  @interface myclass () - (void) somemethod; - (void) someothermethod; @end  @implementation myclass 

why clutter .h file things outsiders should not care about? empty () works private categories in .m file, , issues compile warnings if not implement methods declared.

3) have taken putting dealloc @ top of .m file, below @synthesize directives. shouldn't dealloc @ top of list of things want think in class? true in environment iphone.

3.5) in table cells, make every element (including cell itself) opaque performance. means setting appropriate background color in everything.

3.6) when using nsurlconnection, rule may want implement delegate method:

- (nscachedurlresponse *)connection:(nsurlconnection *)connection                   willcacheresponse:(nscachedurlresponse *)cachedresponse {       return nil; } 

i find web calls singular , it's more exception rule you'll wanting responses cached, web service calls. implementing method shown disables caching of responses.

also of interest, iphone specific tips joseph mattiello (received in iphone mailing list). there more, these useful thought (note few bits have been edited original include details offered in responses):

4) use double precision if have to, such when working corelocation. make sure end constants in 'f' make gcc store them floats.

float val = somefloat * 2.2f; 

this important when somefloat may double, don't need mixed-mode math, since you're losing precision in 'val' on storage. while floating-point numbers supported in hardware on iphones, may still take more time double-precision arithmetic opposed single precision. references:

on older phones supposedly calculations operate @ same speed can have more single precision components in registers doubles, many calculations single precision end being faster.

5) set properties nonatomic. they're atomic default , upon synthesis, semaphore code created prevent multi-threading problems. 99% of don't need worry , code less bloated , more memory-efficient when set nonatomic.

6) sqlite can very, fast way cache large data sets. map application instance can cache tiles sqlite files. expensive part disk i/o. avoid many small writes sending begin; , commit; between large blocks. use 2 second timer instance resets on each new submit. when expires, send commit; , causes writes go in 1 large chunk. sqlite stores transaction data disk , doing begin/end wrapping avoids creation of many transaction files, grouping of transactions 1 file.

also, sql block gui if it's on main thread. if have long query, it's idea store queries static objects, , run sql on separate thread. make sure wrap modifies database query strings in @synchronize() {} blocks. short queries leave things on main thread easier convenience.

more sqlite optimization tips here, though document appears out of date many of points still good;

http://web.utk.edu/~jplyon/sqlite/sqlite_optimization_faq.html


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -