1.取摸除數(shù)為0,存在內存泄露
NSLog(@"%d",6%0); //結果:8409152
2.UIButton 的使用相關
設置UIButton的文字顯示位置、字體的大小、字體的顏色
btn.frame = CGRectMake(x, y, width, height);
[btn setTitle: @"search" forState: UIControlStateNormal];
//設置按鈕上的自體的大小
//[btn setFont: [UIFont systemFontSize: 14.0]]; //這種可以用來設置字體的大小,但是可能會在將來的SDK版本中去除改方法
//應該使用
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[btn seBackgroundColor: [UIColor blueColor]];
按鈕的屬性使用
btn =[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];
//這樣初始化的button,文字默認顏色是白色的,所有如果背景也是白色的話,是看不到文字的,
btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//設置文字位置,現(xiàn)設為居左,默認的是居中
[btn setTitle:@“title”forState:UIControlStateNormal];// 添加文字
有些時候我們想讓UIButton的title居左對齊,我們設置
btn.textLabel.textAlignment = UITextAlignmentLeft
是沒有作用的,我們需要設置
btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;
但是問題又出來,此時文字會緊貼到做邊框,我們可以設置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距離做邊框保持10個像素的距離。
=======================================================
設置UIButton上字體的顏色設置UIButton上字體的顏色,不是用:
[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];
而是用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
3.characterAtIndex:i 這個方法的作用是
返回索引i 所在UniCode 字符 而不是字串
所以應該是
unichar strxmlchar=[strcs characterAtIndex:i];
接收字符
并且format要以%C(大寫字母C)的形式
NSString *strcs=@"猜猜看";
NSString *str=@"";
for(int i=0;i<strcs.length;i++){
unichar strxmlchar=[strcs characterAtIndex:i];
str=[NSStringstringWithFormat:@"%C",strxmlchar];
NSLog(@"%C",strxmlchar);
}
另一種方法
for(int i=0;i<strcs.length;i++){
str=[strcs substringWithRange:NSMakeRange(i, 1)];
NSLog(@"%@",str);
if ([str isEqualToString:@"看"]) {
NSLog(@"%@是指定字符",str);
}
}
4.使用UITableViewCell 的注意點:在使用方法- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
創(chuàng)建cell時,在這個方法中使用 :如 DLog(@"%@",NSStringFromCGRect(self.frame)); 結果為{{0, 0}, {320, 44}} 及在設置cell的子控件時,注意,即使運行在iPhone 6 plus ,為375x667的屏幕時,顯示結果仍為打印結果。如果使用self.frame.size.width,設置需注意,可以使用[UIDevice currentDevice].bounds 得到屏幕的寬度設置。設置子控件的尺寸,可以在 layoutSubviews 方法中;
5.XCode 異常崩潰的處理
調度技巧一(全局斷點):
程序在崩潰的時候,xcode經(jīng)常沒有給出準確的堆棧信息,而是定位在了main方法里, 其實xcode只要裝簡單設置一下,就能準確給出堆棧信息了, 打開xcode左邊的Breakpoint Navigator (view - Navigator -show Breakpoint,可以選擇 Navigator) ,點擊下面的+號添加一個Exception Breakpoint,可以選擇 All, 然后再運行試試,Crash后,就會在在拋出異常的時候停下來了。
調度技巧二(壞內存訪問,查看僵尸對象):
Crash,EXC_BAD_ACCESS (壞內存訪問),這個比較頭疼,因為Crash的時候,可能是比較早之前的某個變量釋放了,現(xiàn)在訪問時出問題。Console里也沒顯示什么日志。開Scheme選項選擇Edit(product) Scheme然后按圖勾上Enable Zombie Objects(僵尸對象) 和Malloc Stack(分配的棧內存)那兩項,注意點:記住一般只有在定位EXC_BAD_ACCESS時候才勾選,別有事沒事都勾上。
重新運行程序,如果是到Exception Breakpoint處停止了,可以在Console中輸入:c(continue)按回車繼續(xù)跑,直到Crash。看下Console是不是有跟SIGABRT類似的錯誤信息日志了。
如 果還沒有日志,在Console中輸入 po $eax $eax標志出錯的地方,適用模擬器,真機用$r0(話說EXC_BAD_ACCESS這種 錯誤模擬器定位就行),還可以輸入比如:po [$eax name] po [$eax reason]等指令查看錯誤其他信息(注意方括號后 沒分號的)。
注意:
開發(fā)時,去掉Enable Zombie Objects,對象不會銷毀
6.屬性注釋,開發(fā)時,不希望注釋占多行,在屬性后面寫注釋,不提示,以下小技巧:
@property(nonatomic,strong)UITableView* tableView; /**< 注釋 */ //在使用的時候,有注釋,只需加個<
7.//這種方法定義的枚舉,在使用時,可以檢查枚舉的類型
typedef NS_ENUM(NSInteger, ZBNetworkStatu)
{
ZBNetworkStatuNoNetwork = 0
};
typedef enum {
FestivalSpring = 0
}Festival;
8.textField 的占位文字設置,使用了kvc
textField.placeholder = @"good";
[textField setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
9.[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; 設置狀態(tài)欄的背景色 首先需要停止使用View controller-based status bar appearance。在project target的Info tab中,插入一個新的key,名字為View controller-based status bar appearance,并將其值設置為NO。
10.調節(jié)UILabel的行間距
- (NSAttributedString *)attributeText:(NSString *)text
{
NSMutableAttributedString *attributeText = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineSpacing = 10;
[attributeText addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, attributeText.length)];
return attributeText;
}
擴展:
iOS9以后出了平方字體,支持6種字重,適合瀏覽使用,詳情!PingFangSC,
注意:
iOS8及以下不支持平方字體,加入字體,會增大ipa的大小,我導入的字體80.4M打出的包,差不多80M左右。
11.Xcode的常用快捷鍵:
- command + shift + j 快速定位到當前打開的文件的位置
- command + shift + 0 打開文檔幫助
- command + . 暫停模擬器
- command + B 編譯代碼
- com + R 運行
- command +shift +K clean 項目
- command + shift +o 查找文件
- command + shift + F 查找
- cmd + F 查找當前文件
- options + 左鍵點擊 文件可以分欄顯示
- command + 1方法的所有調用者和被調用者
12.opaque告訴系統(tǒng)如何組合你的視圖。如果你的視圖的內容是完全不透明的,因此不顯示任何底層視圖的內容,請將此屬性設置為“是”。設置此屬性可以提高性能,可以消除不必要的合成操作。
UIView opaque
The opaque property tells the system how it should composite your view. Set this property to YES if your view’s content is fully opaque and therefore does not reveal any of the underlying view’s content. Setting this property to YES improves performance by eliminating unnecessary compositing operations.
13.網(wǎng)絡壓力測試
ab -n 10 -c 1 https://build.misfit.com/cloudapi/resource/v2/
14.iOS9 使用的URLScheme 必須加入到白名單 在你的應用的plist文件里的LSApplicationQueriesSchemes鍵值下添加。
參考
15.圖片的字符串轉UIImage,切記 此處必須判斷字符串是否為空,不然程序crash,
[_NSPlaceholderData initWithBase64EncodedData:options:]: nil data argument : NSString *imageStr = responseObj[@"binary"];
if(imageStr) {
NSData *imageData = [[NSData alloc] initWithBase64EncodedData:[imageStr dataUsingEncoding:NSDataBase64Encoding64CharacterLineLength] options:0];
if([imageData isKindOfClass:[NSData class]]) {
img = [UIImage imageWithData:imageData];
}
}
16.alwaysBounceVertical 垂直位置上決定collectionView 是否達到內容的結束位置可以拖動。(開發(fā)技巧-加載數(shù)據(jù)失敗,下拉刷新重新加載)。
collectionView.alwaysBounceVertical = YES;
17.私有api 檢查工具
18.去掉UITextView的放大功能 :textViewShouldBeginEditing: txtView.editable一定要設置成YES
//clear UITextView copy cut paste
- (BOO)canPerformAction:(SEL)action withSender:(id)sender
{
if([UIMenuController sharedMenuController]) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
選擇/復制的菜單、放大鏡全部禁用
- (BOOL)canBecomeFirstResponder
{
return NO;
}
19.NSData 和UIImage 的相互轉換
//data -> 轉換成 UIImage
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
UIImage *image = [UIImage imageWithData:imageData];
//UIImage -> UIData
UIData *imageData = UIImagePNGRepresentation(self.image);
UIImageJPEGRepresentation(strongSelf.shareImage,1.0)