1.字符串處理: 截取字符串
- -(NSString *)substringToIndex:(NSUInteger)to;
- 不包括index處字符
- 判斷字符串長(zhǎng)度,否則可能崩潰
2.在導(dǎo)航欄添加UIBarButtonItem時(shí),在iOS7后會(huì)進(jìn)行自動(dòng)渲染
img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
3.遍歷數(shù)組刪除某個(gè)元素(不要forin正序遍歷時(shí)刪除,會(huì)崩潰)
- 逆序遍歷,然后查找刪除
NSEnumerator *enumerator = [array reverseObjectEnumerator];
//forin遍歷
for (AddressPerson *groupName in enumerator) {
if ([[groupName group] isEqualToString:@"Zhangsan"]) {
[array removeObject:groupName];
}
}
- 定義一個(gè)副本,遍歷副本找到想要?jiǎng)h除的元素index,然后在原數(shù)組中刪除對(duì)應(yīng)的元素
4. 數(shù)組元素去重
- 利用NSSet的allObjects方法
NSSet *set = [NSSet setWithArray:dataArray];
NSLog(@"%@",[set allObjects]);
- 利用數(shù)組的containsObject來去除
NSMutableArray *listAry = [[NSMutableArray alloc]init];
for (NSString *str in dataArray) {
if (![listAry containsObject:str]) {
[listAry addObject:str];
}
}
NSLog(@"%@",listAry);
- 利用NSDictionary的AllKeys(AllValues)方法
NSArray *dataArray = @[@"2014-04-01",@"2014-04-02",@"2014-04-03",
@"2014-04-01",@"2014-04-02",@"2014-04-03",
@"2014-04-01",@"2014-04-03",@"2014-04-03",
@"2014-04-01",@"2014-04-02",@"2014-04-03",
@"2014-04-01",@"2014-04-02",@"2014-04-03",
@"2014-04-01",@"2014-04-02",@"2014-04-03",
@"2014-04-04",@"2014-04-06",@"2014-04-08",
@"2014-04-05",@"2014-04-07",@"2014-04-09",];
NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithCapacity:0];
for(NSString *str in dataArray)
{
[dic setValue:str forKey:str];
}
NSLog(@"%@",[dic allKeys]);
5. 如果設(shè)置了啟動(dòng)圖片而沒有生效,需要將原來的程序卸載在運(yùn)行即可.
6.添加4s模擬器
/Applications(應(yīng)用程序)/Xcode.app(顯示包內(nèi)容)/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/CoreSimulator/Profiles/Runtimes 后面兩級(jí)目錄需要自己創(chuàng)建
Xcode 顯示包內(nèi)容 Contents-->Developer-->Platforms-->iPhoneOS.platform-->DeviceSupport
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
7.通過狀態(tài)欄判斷當(dāng)前的網(wǎng)絡(luò)連接狀態(tài)
/*
* 0 = No wifi or cellular
* 1 = 2G and earlier? (not confirmed)
* 2 = 3G? (not yet confirmed)
* 3 = 4G
* 4 = LTE
* 5 = Wifi
*/
- (int)dataNetworkTypeFromStatusBar
{
UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
NSNumber *dataNetworkItemView = nil;
for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
dataNetworkItemView = subview;
break;
}
}
int netType = 0;
NSNumber * num = [dataNetworkItemView valueForKey:@"dataNetworkType"];
if (num == nil) {
netType = 0;
}else{
int n = [num intValue];
if (n == 0) {
netType = 0;
}else if (n == 1){
netType = 1;
}else if (n == 2){
netType = 2;
}else{
netType = 3;
}
}
return netType;
}
# import <CoreTelephony/CTTelephonyNetworkInfo.h>
# import <CoreTelephony/CTCarrier.h>
**獲取運(yùn)營(yíng)商信息**
-(void)getcarrierName{
CTTelephonyNetworkInfo *telephonyInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [telephonyInfo subscriberCellularProvider];
NSString *currentCountry=[carrier carrierName];
NSLog(@"[carrier isoCountryCode]==%@,[carrier allowsVOIP]=%d,[carrier mobileCountryCode=%@,[carrier mobileCountryCode]=%@",[carrier isoCountryCode],[carrier allowsVOIP],[carrier mobileCountryCode],[carrier mobileNetworkCode]);
serverLabel.text=currentCountry;
}
8.搜索框添加 “廣告”標(biāo)簽
- (void)addADFlagOnSearchBar {
NSString *key = [self.requestDict objectForKey:@"key"];
if (key && key.length > 0) {
self.searchBar.showsSearchResultsButton = NO;
} else {
self.searchBar.showsSearchResultsButton = YES;
NSString *imageName = (iPhone6Plus? @"xfList_AD_iPhone6plus_icon.png": @"xfList_AD_iPhone5_icon.png");
[self.searchBar setImage:[UIImage imageNamed:imageName] forSearchBarIcon:UISearchBarIconResultsList state:UIControlStateNormal];
UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];
if (searchField) {
searchField.rightView.bounds = CGRectMake(0, 0, 27, 14);
searchField.rightViewMode = UITextFieldViewModeUnlessEditing;
}
[self.searchBar setPositionAdjustment:UIOffsetMake(5, 0) forSearchBarIcon:UISearchBarIconResultsList];
}
}
9.禁止手機(jī)睡眠 參考鏈接
UIApplication.shared.isIdleTimerDisabled = true
iOS8系統(tǒng)中丸相,tableView最好實(shí)現(xiàn)下-tableView: heightForRowAtIndexPath:這個(gè)代理方法,要不然在iOS8中可能就會(huì)出現(xiàn)顯示不全或者無法響應(yīng)事件的問題
// 必須寫的方法般眉,和editActionsForRowAtIndexPath配對(duì)使用凳鬓,里面什么不寫也行
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
10.collectionView的內(nèi)容小于其寬高的時(shí)候是不能滾動(dòng)的罐监,設(shè)置可以滾動(dòng):
collectionView.alwaysBounceHorizontal = YES;
collectionView.alwaysBounceVertical = YES;
11.強(qiáng)引用 / 弱引用????
#define WeakSelf(type) __weak typeof(type) weak##type = type; // weak
#define StrongSelf(type) __strong typeof(type) type = weak##type; // strong
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
12.image圓角
- (UIImage *)circleImage
{
// NO代表透明
UIGraphicsBeginImageContextWithOptions(self.size, NO, 1);
// 獲得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 添加一個(gè)圓
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
// 方形變圓形
CGContextAddEllipseInRect(ctx, rect);
// 裁剪
CGContextClip(ctx);
// 將圖片畫上去
[self drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *)resizableImage:(NSString *)imageName
{
UIImage *image = [UIImage imageNamed:imageName];
CGFloat imageW = image.size.width;
CGFloat imageH = image.size.height;
return [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageH * 0.5, imageW * 0.5, imageH * 0.5, imageW * 0.5) resizingMode:UIImageResizingModeStretch];
}
13.身份證號(hào)驗(yàn)證
- (BOOL)validateIdentityCard {
BOOL flag;
if (self.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:self];
}
14.拿到當(dāng)前正在顯示的控制器裂问,不管是push進(jìn)去的豫缨,還是present進(jìn)去的都能拿到
- (UIViewController *)getVisibleViewControllerFrom:(UIViewController*)vc {
if ([vc isKindOfClass:[UINavigationController class]]) {
return [self getVisibleViewControllerFrom:[((UINavigationController*) vc) visibleViewController]];
}else if ([vc isKindOfClass:[UITabBarController class]]){
return [self getVisibleViewControllerFrom:[((UITabBarController*) vc) selectedViewController]];
} else {
if (vc.presentedViewController) {
return [self getVisibleViewControllerFrom:vc.presentedViewController];
} else {
return vc;
}
}
}
15.真機(jī)測(cè)試的時(shí)候報(bào)錯(cuò):Could not launch "你的 App"谒兄,process launch failed: Security
- 因?yàn)槟愕腶pp沒有上線摔桦,iOS9開始社付,需要手動(dòng)信任Xcode生成的描述文件承疲,打開手機(jī)設(shè)置->通用->描述文件->點(diǎn)擊你的app的描述文件->點(diǎn)擊信任
真機(jī)測(cè)試的時(shí)候報(bào)錯(cuò):Could not find Developer Disk Image
- 這是因?yàn)槟愕脑O(shè)備系統(tǒng)版本大于Xcode能兼容的系統(tǒng)版本邻耕,比如你的設(shè)備是iOS10.3,而Xcode版本是8.2(Xcode8.2最大兼容iOS10.2)燕鸽,就會(huì)報(bào)這個(gè)錯(cuò)誤兄世。解決辦法就是升級(jí)Xcode!
16.移除字符串中的空格和換行
+ (NSString *)removeSpaceAndNewline:(NSString *)str {
NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
return temp;
}
17.字符串是否為空
+ (BOOL)isEqualToNil:(NSString *)str {
return str.length <= 0 || [str isEqualToString:@""] || !str;
}
18.判斷字符串中是否有空格
+ (BOOL)isBlank:(NSString *)str {
NSRange _range = [str rangeOfString:@" "];
if (_range.location != NSNotFound) {
//有空格
return YES;
} else {
//沒有空格
return NO;
}
}
19.從NSURL中拿到鏈接字符串
NSString *urlString = myURL.absoluteString;
20.如果用addTarget:action:forControlEvents:方法為一個(gè)button添加了很多點(diǎn)擊事件啊研,在某個(gè)時(shí)刻想一次刪除怎么辦御滩?只需要調(diào)用下邊這句代碼
[youButton removeTarget:nil action:nil forControlEvents:UIControlEventAllEvents];
21.tableViewCell分割線頂?shù)筋^
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setSeparatorInset:UIEdgeInsetsZero];
[cell setLayoutMargins:UIEdgeInsetsZero];
cell.preservesSuperviewLayoutMargins = NO;
}
- (void)viewDidLayoutSubviews {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
防止離屏渲染為image添加圓角
// image分類
- (UIImage *)circleImage
{
// NO代表透明
UIGraphicsBeginImageContextWithOptions(self.size, NO, 1);
// 獲得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 添加一個(gè)圓
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
// 方形變圓形
CGContextAddEllipseInRect(ctx, rect);
// 裁剪
CGContextClip(ctx);
// 將圖片畫上去
[self drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
22.判斷rect是否相等交叉
// 是否相交
if (CGRectIntersectsRect(rect1, rect2)) {
}
if (CGRectEqualToRect(rect1, rect2)) { // 兩個(gè)區(qū)域相等
// do some
}
if (CGPointEqualToPoint(point1, point2)) { // 兩個(gè)點(diǎn)相等
// do some
}
if (CGSizeEqualToSize(size1, size2)) { // 兩個(gè)size相等
// do some
}
23.判斷字符串是否為數(shù)字
NSCharacterSet *notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([str rangeOfCharacterFromSet:notDigits].location == NSNotFound)
{
// 是數(shù)字
} else
{
// 不是數(shù)字
}
23、判斷一個(gè)view是否為另一個(gè)view的子視圖
// 如果myView是self.view本身党远,也會(huì)返回yes
BOOL isSubView = [myView isDescendantOfView:self.view];
24.UIWebView添加單擊手勢(shì)不響應(yīng)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(webViewClick)];
tap.delegate = self;
[_webView addGestureRecognizer:tap];
// 因?yàn)閣ebView本身有一個(gè)單擊手勢(shì)削解,所以再添加會(huì)造成手勢(shì)沖突,從而不響應(yīng)沟娱。需要綁定手勢(shì)代理氛驮,并實(shí)現(xiàn)下邊的代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
25 字符串轉(zhuǎn)數(shù)值問題
- 非純數(shù)字的字符串轉(zhuǎn)doublevalue floatvalue 都是 0
- 如和判斷一個(gè)字符串是否為數(shù)字和小數(shù)點(diǎn)
- (BOOL)isPureNumandCharacters:(NSString *)string
{
NSCharacterSet *numberSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
string = [string stringByTrimmingCharactersInSet:numberSet];
if(string.length > 0)
{
return NO;
}
return YES;
}
26 字符串包含
- (BOOL)containsString:(NSString *)str
- str不能為nil,否則會(huì)崩潰
- API_AVAILABLE ios(8.0)