iOS開發常用知識點(持續更新2018.6.25)

iOS App打包上架超詳細流程(手把手圖文教你)


https://www.jianshu.com/p/817686897ec1?open_source=weibo_search

ios開發證書,描述文件,bundle ID的關係

https://www.jianshu.com/p/21ebca8cadf6


1、去除數組中重複的對象

NSArray *newArr = [oldArr valueForKeyPath:@“@distinctUnionOfObjects.self"];
2、強/弱引用
#define WeakSelf(type)  __weak typeof(type) weak##type = type; // weak
#define StrongSelf(type)  __strong typeof(type) type = weak##type; // strong
3、獲取圖片資源
#define GetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
4、GCD代碼只執行一次
#define kDISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
5、自定義NSLog
#ifdef DEBUG
#define NSLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define NSLog(...)
#endif
6、在主線程上運行
#define kDISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);

7、開啓異步線程

#define kDISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);

8、通知

#define NOTIF_ADD(n, f)     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(f) name:n object:nil]
#define NOTIF_POST(n, o)    [[NSNotificationCenter defaultCenter] postNotificationName:n object:o]
#define NOTIF_REMV()        [[NSNotificationCenter defaultCenter] removeObserver:self]

9、修改textField的placeholder的字體顏色、大小

[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

10、身份證號驗證

- (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];
}

11、移除字符串中的空格和換行

+ (NSString *)removeSpaceAndNewline:(NSString *)str {
    NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return temp;
}

12、字符串是否爲空

+ (BOOL)isEqualToNil:(NSString *)str {
    return str.length <= 0 || [str isEqualToString:@""] || !str;
}

13、將tableView滾動到頂部

[tableView setContentOffset:CGPointZero animated:YES];
或者
[tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

14、UILabel設置內邊距

子類化UILabel,重寫drawTextInRect方法
- (void)drawTextInRect:(CGRect)rect {
    // 邊距,上左下右
    UIEdgeInsets insets = {0, 5, 0, 5};
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

15、讓手機震動一下

倒入框架
#import <AudioToolbox/AudioToolbox.h>
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
或者
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

16、在image上繪製文字並生成新的image

UIFont *font = [UIFont boldSystemFontOfSize:12];
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
    [[UIColor whiteColor] set];
    [text drawInRect:CGRectIntegral(rect) withFont:font]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

17、保存UIImage到本地

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];

[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];

18、通知監聽APP生命週期

    UIApplicationDidEnterBackgroundNotification 應用程序進入後臺
    UIApplicationWillEnterForegroundNotification 應用程序將要進入前臺
    UIApplicationDidFinishLaunchingNotification 應用程序完成啓動
    UIApplicationDidFinishLaunchingNotification 應用程序由掛起變的活躍
    UIApplicationWillResignActiveNotification 應用程序掛起(有電話進來或者鎖屏)
    UIApplicationDidReceiveMemoryWarningNotification 應用程序收到內存警告
    UIApplicationDidReceiveMemoryWarningNotification 應用程序終止(後臺殺死、手機關機等)
    UIApplicationSignificantTimeChangeNotification 當有重大時間改變(凌晨0點,設備時間被修改,時區改變等)
    UIApplicationWillChangeStatusBarOrientationNotification 設備方向將要改變
    UIApplicationDidChangeStatusBarOrientationNotification 設備方向改變
    UIApplicationWillChangeStatusBarFrameNotification 設備狀態欄frame將要改變
    UIApplicationDidChangeStatusBarFrameNotification 設備狀態欄frame改變
    UIApplicationBackgroundRefreshStatusDidChangeNotification 應用程序在後臺下載內容的狀態發生變化
    UIApplicationProtectedDataWillBecomeUnavailable 本地受保護的文件被鎖定,無法訪問
    UIApplicationProtectedDataWillBecomeUnavailable 本地受保護的文件可用了

19.NSString使用stringWithFormat拼接的相關知識

  • 保留2位小數點
//.2代表小數點後面保留2位(2代表保留的數量)
NSString *string = [NSString stringWithFormat:@"%.2f",M_PI];
//輸出結果是: 3.14
NSLog(@"%@", string);
  • 0補全的方法
NSInteger count = 5;
  //02代表:如果count不足2位 用0在最前面補全(2代表總輸出的個數)
  NSString *string = [NSString stringWithFormat:@"%02zd",count];
//輸出結果是: 05
NSLog(@"%@", string);
  • 字符串中有特殊符號%怎麼辦
NSInteger count = 50;
 //%是一個特殊符號 如果在NSString中用到%需要如下寫法
  NSString *string = [NSString stringWithFormat:@"%zd%%",count];
//輸出結果是: 50%
 NSLog(@"%@", string);
  • 字符串中有特殊符號"怎麼辦
NSInteger count = 50;
//"是一個特殊符號, 如果在NSString中用到"需要用\進行轉義
NSString *string = [NSString stringWithFormat:@"%zd\"",count];
//輸出結果是: 50"
 NSLog(@"%@", string);

20.Button禁止觸摸事件的2種方式

//會改變按鈕的狀態,顏色會變灰
button.enabled = NO;```
但是又有一個需求是既不能點擊也不要改變Button顏色:

//保持按鈕原來的狀態,顏色不會變
button.userInteractionEnabled = NO;

21.設置圓形圖片
(UIImage *)cutCircleImage {
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
// 獲取上下文
CGContextRef ctr = UIGraphicsGetCurrentContext();
// 設置圓形
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextAddEllipseInRect(ctr, rect);
// 裁剪
CGContextClip(ctr);
// 將圖片畫上去
[self drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
21.nil、Nil、NULL、NSNull的區別

nil:指向一個對象的空指針,對objective c id 對象賦空值.

Nil:指向一個類的空指針,表示對類進行賦空值.

NULL:指向其他類型(如:基本類型、C類型)的空指針, 用於對非對象指針賦空值.

NSNull:在集合對象中,表示空值的對象.


22.關閉UITableViewCell的點擊事件方法:

cell.selectionStyle = UITableViewCellSelectionStyleNone;   //這個屬性在cellForRowAtIndexPath:方法裏面寫;

23.關閉UITableViewCell被點擊之後的灰色背景效果:

[tableView deselectRowAtIndexPath:indexPath animated:YES];  //這個屬性在didSelectRowAtIndexPath:方法裏面寫;

24.去掉UITableViewCell之間的線條的方法:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; //這個方式去掉全部的cell線條,如果想去掉某一條需要自定義cell的線條

25.清空空白UITableViewCell的方法:

self.tableView.tableFooterView = [[UIView alloc] init];  或者    self.tableView.tableFooterView = UIView.new; 




參考文章

https://www.jianshu.com/p/1ff9e44ccc78

https://www.jianshu.com/p/9fcd37c0ea05

https://www.jianshu.com/p/8207621ddcaa

http://blog.csdn.net/CC1991_/article/details/53994578

發佈了125 篇原創文章 · 獲贊 159 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章