[ios]总结iOS开发当中一些特别注意的问题 【转】

1. mutable的数据类型,不能声明为copy的属性,如@property(nonatomic, copy) NSMutableArray *array;  @property(nonatomic, copy)  NSMutableDictionary *dict;这样的声明,然后再初始化的时候会有问题,self.array = [[NSMutableArray alloc] init];  其实它在内存中是NSArray的实例。


2.如果用下面代码出现一个模态ui,这个模态ui中有UITextField或UITextView的成员,那么会出现keyboard, 如果发送resignFirstrRsponder键盘是不会消失的。

  1. UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:searchVC];  
  2. nv.modalPresentationStyle = UIModalPresentationFormSheet;  
  3. [self presentModalViewController:nv animated:YES];  


UINavigationController必须用Category的方法实现如下方法,才可以让键盘消失

  1. @interface UINavigationController (DismissKeyboard)  
  2. - (BOOL)disablesAutomaticKeyboardDismissal;  
  3. @end  
  4.   
  5. @implementation UINavigationController (DismissKeyboard)  
  6.   
  7. - (BOOL)disablesAutomaticKeyboardDismissal  
  8. {  
  9.     return NO;  
  10. }  
  11. @end  


3.关于NSURLConnection中的delegate,下面是官网的说明:

  1. Note: During a download the connection maintains a strong reference to the delegate. It releases that strong reference when the connection finishes loading, fails, or is canceled.  


也就是说,delegate会被NSURLConnection拥有,在非arc情况下,下面的代码也可以

  1. //creates the handler object  
  2. MyHandlerClass *handler = [[MyHandlerClass alloc] init];  
  3.    
  4. //creates the connection with handler as an autorelease object  
  5. [[NSURLConnection alloc] initWithRequest:request delegate:[handler autorelease]];  
  6.   
  7. 或  
  8.   
  9. //creates the handler object  
  10. MyHandlerClass *handler = [[MyHandlerClass alloc] init];  
  11.    
  12. //creates the connection with handler  
  13. [[NSURLConnection alloc] initWithRequest:request delegate:handler];  
  14.    
  15. //releases handler object  
  16. [handler release];  


MyHandlerClass中NSURLConnection的回调方法也会解发。

在arc情况下代码如下:

  1. //creates the handler object  
  2. MyHandlerClass *handler = [[MyHandlerClass alloc] init];  
  3.    
  4. //creates the connection with handler  
  5. [[NSURLConnection alloc] initWithRequest:request delegate:handler];  


4. static library中如果用了category, 那么在引用库的时候在other Link中加入-all_load 或 -force_load参考

5. 如果项目工程中有c/c++的源码,那么在编写项目Prefix.pch的时候一定得注意,如果下面这样写,编译会出错:

  1. #ifdef __OBJC__  
  2.     #import <UIKit/UIKit.h>  
  3.     #import <Foundation/Foundation.h>  
  4. #endif  
  5. #import "AppDelegate.h"  


修改方法为如下就正确了:

  1. #ifdef __OBJC__  
  2.     #import <UIKit/UIKit.h>  
  3.     #import <Foundation/Foundation.h>  
  4. #import "AppDelegate.h"  
  5. #endif  

因为这是一个预编译头文件,是全局的,所有源文件对其都是可见的,所以在c/c++源码中也会引入,在c/c++源码中引用objective_c的源码就会出错。

6. UIView 的addSubview:b方法,如果参数b是同一个指针,那么无论调用多少次,它的subview中只有一个b对象。 测试环境是iOS6, 记得iOS3好像没有这个限制。

7. NSNumber能表示的数字整数部份精度只有15位,其实是double的精度。如果需要更高的精度,请用NSDecimalNumber。

8. UITableView相关:

a.不要在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath方法中调用

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell,会引起循环调用。

9. 发送crash log 到邮箱

  1. void uncaughtExceptionHandler(NSException *exception) {  
  2.     NSLog(@"CRASH: %@", exception);  
  3.     NSLog(@"Stack Trace: %@", [exception callStackSymbols]);  
  4.     // Internal or email error reporting  
  5. }  
  6.   
  7. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  8. {  
  9.     NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);  
  10.     // Normal launch stuff  
  11. }  
原文地址:https://www.cnblogs.com/jinjiantong/p/2986308.html