Successfully reported this slideshow.

推荐 iOS 开发的几个最佳实践

481 views

Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

推荐 iOS 开发的几个最佳实践

  1. 1. 最佳实践的开始 寻找更优的实现
  2. 2. 最佳实践包含哪些 解决什么问题 适用什么场景 解决的⽅方式 这样实践的好处 实例
  3. 3. 分享三个实践 Objective C ARC Light View Controller Ways to avoid/find bug
  4. 4. ARC Best Practices http:/ /amattn.com/2011/12/07/arc_best_practices.html General : property definitions Bridging : CFxxx -> NSxxx Out parameters : NSError Block : weak or strong Accessing CGThings from NSThings or UIThings
  5. 5. Light View Controller http:/ /www.objc.io/issue-1/lighter-view-controllers.html Move Domain Logic into the Model Move View Code into the View Layer Separate Out Data Source and Other Protocols Creating the Store Class Move Web Service Logic to the Model Layer Communication
  6. 6. Ways to avoid/find bug Use assert to check unexpected situation Unit test Logging Cost App Testing 100 75 50 25 0 Assert UT Test Log Crash Crash report Misc Instrument Lint tools Hour 180 135 90 45 0 Assert UT Test Log Crash
  7. 7. Use Assert to check Parameter - (BOOL)appendPartWithFileURL:(NSURL *)fileURL name:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType error:(NSError * __autoreleasing *)error { NSParameterAssert(fileURL); NSParameterAssert(name); NSParameterAssert(fileName); NSParameterAssert(mimeType); Check required resource #if DEBUG NSString *mainBundlePath = [[NSBundle mainBundle] resourcePath]; NSString *path = [mainBundlePath stringByAppendingPathComponent:kDOUADStandardResourceBundle]; NSAssert([NSBundle bundleWithPath:path] != nil, [@"Need" stringByAppendingPathComponent:kDOUADStandardResourceBundle]); path = [mainBundlePath stringByAppendingPathComponent:kDOUADCustomResourceBundle]; NSAssert([NSBundle bundleWithPath:path] != nil, [@"Need" stringByAppendingPathComponent:kDOUADCustomResourceBundle]); #endif User assert to check expected value BOOL existed = [[NSFileManager defaultManager] fileExistsAtPath:dir isDirectory:&isDir]; NSAssert(existed == NO || isDir == YES, @"Should be directory");
  8. 8. Assert vs try-catch Try... catch - for exceptional conditions Assertions for catching invalid code
  9. 9. Unit Test & Code quality Unit test Examples: DOUUserManager AmonSulIOS What we can do? More unit test in common library Test invisible logic
  10. 10. Logging logging error, warning, etc properly Check logging message after QA test Crash report Check crash report daily Misc Run analysis

×