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键盘是不会消失的。
- UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:searchVC];
- nv.modalPresentationStyle = UIModalPresentationFormSheet;
- [self presentModalViewController:nv animated:YES];
UINavigationController必须用Category的方法实现如下方法,才可以让键盘消失
- @interface UINavigationController (DismissKeyboard)
- - (BOOL)disablesAutomaticKeyboardDismissal;
- @end
- @implementation UINavigationController (DismissKeyboard)
- - (BOOL)disablesAutomaticKeyboardDismissal
- {
- return NO;
- }
- @end
3.关于NSURLConnection中的delegate,下面是官网的说明:
- 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情况下,下面的代码也可以
- //creates the handler object
- MyHandlerClass *handler = [[MyHandlerClass alloc] init];
- //creates the connection with handler as an autorelease object
- [[NSURLConnection alloc] initWithRequest:request delegate:[handler autorelease]];
- 或
- //creates the handler object
- MyHandlerClass *handler = [[MyHandlerClass alloc] init];
- //creates the connection with handler
- [[NSURLConnection alloc] initWithRequest:request delegate:handler];
- //releases handler object
- [handler release];
MyHandlerClass中NSURLConnection的回调方法也会解发。
在arc情况下代码如下:
- //creates the handler object
- MyHandlerClass *handler = [[MyHandlerClass alloc] init];
- //creates the connection with handler
- [[NSURLConnection alloc] initWithRequest:request delegate:handler];
4. static library中如果用了category, 那么在引用库的时候在other Link中加入-all_load 或 -force_load参考
5. 如果项目工程中有c/c++的源码,那么在编写项目Prefix.pch的时候一定得注意,如果下面这样写,编译会出错:
- #ifdef __OBJC__
- #import <UIKit/UIKit.h>
- #import <Foundation/Foundation.h>
- #endif
- #import "AppDelegate.h"
修改方法为如下就正确了:
- #ifdef __OBJC__
- #import <UIKit/UIKit.h>
- #import <Foundation/Foundation.h>
- #import "AppDelegate.h"
- #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 到邮箱
- void uncaughtExceptionHandler(NSException *exception) {
- NSLog(@"CRASH: %@", exception);
- NSLog(@"Stack Trace: %@", [exception callStackSymbols]);
- // Internal or email error reporting
- }
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
- // Normal launch stuff
- }