iOS開發(fā)小記!

1:Block 循環(huán)引用的問題

-(void)test
{
    __weak typeof(self) WeakSelf = self;
    void(^testBlock)(void) = ^(void) {
        __strong typeof(self) StrongSelf = WeakSelf;
        [StrongSelf test2];
    };
    testBlock();
}

宏定義

// weak
#define WeakSelf(type) __weak typeof(type) weak##type = type;
// strong
#define StrongSelf(type) __strong typeof(type) type = weak##type;

2: 修改textField的占位符(placeholder)的字體顏色、大小

比較簡潔的方法 調(diào)用kvc的方法

self.textField.placeholder = @"username is in here!";
[self.textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

修改attributedString進(jìn)行設(shè)置

NSString *string = @"UITextFiled修改占位符的方法";
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];

    [attributedString addAttribute:NSForegroundColorAttributeName
                             value:[UIColor redColor]
                             range:NSMakeRange(0, [string length])];

    [attributedString addAttribute:NSFontAttributeName
                             value:[UIFont systemFontOfSize:16]
                             range:NSMakeRange(0, [string length])];

    self.textField.attributedPlaceholder = attributedString;

3:生成參數(shù)字典的時(shí)候用 NSDictionaryOfVariableBindings

For Example

    NSString *title = @"請(qǐng)求的title";
    NSNumber *age = @56;
    NSArray *movies = @[@"肖申克的救贖",@"可可西里的美麗傳說"];
    NSDictionary *dict = NSDictionaryOfVariableBindings(title,age,movies);
image.png

4:去除數(shù)組中重復(fù)的對(duì)象

補(bǔ)充:這種方法篩選的數(shù)據(jù)是無序的~~~~~~~~~~

    NSArray *oldArr = @[@"5",@"3",@"6",@"9",@"5",@"5",@"5",@"5"];
    NSArray *newArr = [oldArr valueForKeyPath:@"@distinctUnionOfObjects.self"];
image.png

5:ATS設(shè)置

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
      <true/>
</dict>

6:字符屬性

字符屬性可以應(yīng)用于 attributed string 的文本中。
NSString *const NSFontAttributeName;(字體)
NSString *const NSParagraphStyleAttributeName;(段落)
NSString *const NSForegroundColorAttributeName;(字體顏色)
NSString *const NSBackgroundColorAttributeName;(字體背景色)
NSString *const NSLigatureAttributeName;(連字符)
NSString *const NSKernAttributeName;(字間距)
NSString *const NSStrikethroughStyleAttributeName;(刪除線)
NSString *const NSUnderlineStyleAttributeName;(下劃線)
NSString *const NSStrokeColorAttributeName;(邊線顏色)
NSString *const NSStrokeWidthAttributeName;(邊線寬度)
NSString *const NSShadowAttributeName;(陰影)(橫豎排版)
NSString *const NSVerticalGlyphFormAttributeName;

7:GCD便利數(shù)據(jù)

-(void)apply
{
    NSLog(@"BEGAN");
    dispatch_apply(5, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
        NSLog(@"下標(biāo)-----%ld  當(dāng)前線程-----%@",(long)index,[NSThread currentThread]);
    });
    //便利完之后 會(huì)執(zhí)行
    NSLog(@"END");
}

輸出結(jié)果:幾乎是同時(shí)進(jìn)行的便利 效率更高 但是是無序的


Snip20180130_1.png

8:iOS10之后,訪問用戶手機(jī)相冊(cè) 或者 其他權(quán)限,需要在info plist 文件配置相關(guān)的信息

如果不配置 應(yīng)用會(huì)閃退 相關(guān)的錯(cuò)誤信息

This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.

需要寫清楚為什么要用到這個(gè)權(quán)限,如果只是簡單的寫的話.蘋果審核會(huì)不通過!

升到iOS10之后,需要設(shè)置權(quán)限的有:

麥克風(fēng)權(quán)限:Privacy - Microphone Usage Description 是否允許此App使用你的麥克風(fēng)?

相機(jī)權(quán)限: Privacy - Camera Usage Description 是否允許此App使用你的相機(jī)?

相冊(cè)讀取權(quán)限:  Privacy - Photo Library Usage Description   是否允許此APP讀取您的媒體資料庫?
相冊(cè)寫入權(quán)限: Privacy - Photo Library Usage Description 是否允許此App訪問你的媒體資料庫?

通訊錄權(quán)限: Privacy - Contacts Usage Description 是否允許此App訪問你的通訊錄?

藍(lán)牙權(quán)限:Privacy - Bluetooth Peripheral Usage Description 是否許允此App使用藍(lán)牙?

語音轉(zhuǎn)文字權(quán)限:Privacy - Speech Recognition Usage Description 是否允許此App使用語音識(shí)別?

日歷權(quán)限:Privacy - Calendars Usage Description

定位權(quán)限:Privacy - Location When In Use Usage Description

定位權(quán)限: Privacy - Location Always Usage Description

位置權(quán)限:Privacy - Location Usage Description

媒體庫權(quán)限:Privacy - Media Library Usage Description

健康分享權(quán)限:Privacy - Health Share Usage Description

健康更新權(quán)限:Privacy - Health Update Usage Description

運(yùn)動(dòng)使用權(quán)限:Privacy - Motion Usage Description

音樂權(quán)限:Privacy - Music Usage Description

提醒使用權(quán)限:Privacy - Reminders Usage Description

Siri使用權(quán)限:Privacy - Siri Usage Description

電視供應(yīng)商使用權(quán)限:Privacy - TV Provider Usage Description

視頻用戶賬號(hào)使用權(quán)限:Privacy - Video Subscriber Account Usage Description

關(guān)于照片的問題

一個(gè)是讀取內(nèi)容一個(gè)是寫入文件內(nèi)容
直接以 source code 的形式寫入plist 文件
  <key>NSPhotoLibraryUsageDescription</key>
  <string>APP需要您的同意,才能訪問讀取媒體資料庫</string>
  <key>NSPhotoLibraryAddUsageDescription</key>
  <string>App需要您的同意,才能訪問寫入媒體資料庫</string>

其他一些常用的

<key>NSContactsUsageDescription<key>
<string>通訊錄</string>

<key>NSMicrophoneUsageDescription<key>
<string>麥克風(fēng)</string>

<key>NSLocationAlwaysUsageDescription<key>
<string>地理位置</string>

<key>NSLocationWhenInUseUsageDescription<key>
<string>地理位置</string>

9屏蔽cocoapods 的警告

target 'Test' do

inhibit_all_warnings!

pod 'xxxxxxxxx'
end

10解決 Alfred 每次開機(jī)都提示請(qǐng)求通訊錄權(quán)限的問題

安裝完 Alfred 以后,每次開機(jī)都會(huì)提示請(qǐng)求通訊錄權(quán)限,把設(shè)置里的通訊錄關(guān)掉也沒用,每次都提示又非常煩人,這里把解決方法記錄一下。

依次打開 應(yīng)用程序 - Alfred 3.app - 右鍵顯示包內(nèi)容 -
Contents - Frameworks - Alfred Framework.framework - Versions - A 下找到一個(gè)叫 Alfred Framework 的文件。

接著打開終端,輸入以下命令(最后的 - 后面有個(gè)空格)。
sudo codesign -f -d -s - 
然后將剛才的 Alfred Framework 文件直接拖到終端,接著回車輸入用戶密碼。
最后提示 replacing existing signature 就表示成功了。
或者直接在終端中輸入
sudo codesign -f -d -s - /Applications/Alfred\ 3.app/Contents/Frameworks/Alfred\ Framework.framework/Versions/A/Alfred\ Framework

11 刪除項(xiàng)目 Git .DS_Store

項(xiàng)目目錄中
git clean -d -fx
關(guān)閉.DS_Store
 步驟一:刪除所有隱藏.DS_store文件,打開命令行窗口
 sudo find / -name ".DS_Store" -depth -exec rm {} \

 步驟二: 設(shè)置不再產(chǎn)生選項(xiàng), 執(zhí)行如下命令
defaults write com.apple.desktopservices DSDontWriteNetworkStores true 

12查看Mac本機(jī)地址

ifconfig en0
Snip20180625_3.png

13:#pragma 處理警告 clang diagnostic 的使用

#pragma clang diagnostic push   
#pragma clang diagnostic ignored "-相關(guān)命令"  
 //  coding
#pragma clang diagnostic pop

下邊是方法警告的詳細(xì)說明

  1. 方法棄用告警: "-Wdeprecated-declarations"
  2. 不兼容指針類型:"-Wincompatible-pointer-types"
  3. 循環(huán)引用 :"-Warc-retain-cycles"
  4. 未使用變量:"-Wunused-variable"
  5. switch 未使用default:"-Wcovered-switch-default"
  6. performSelector系列方法編譯器警告:"-Warc-performSelector-leaks"
  7. 內(nèi)存泄漏警告 :"-Warc-performSelector-leaks"
    注:警告信息在日志里邊可以找到


    Snip20180801_1.png

14:GCD定時(shí)器的使用

-(void)gcdTimerTest{
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    //設(shè)置時(shí)間
    uint64_t start = 1.0;
    uint64_t spacetimer = 2.0;
    //參數(shù)以此是 timer 開始時(shí)間 間隔時(shí)間 誤差時(shí)間
    //NSEC_PER_SEC 毫秒
    dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, start*NSEC_PER_SEC), spacetimer*NSEC_PER_SEC, 0);
    //設(shè)置定時(shí)器的回調(diào)。還有一個(gè)設(shè)置方法
   // dispatch_source_set_event_handler_f(<#dispatch_source_t  _Nonnull source#>, <#dispatch_function_t  _Nullable handler#>)
    dispatch_source_set_event_handler(timer, ^{
        NSLog(@"GCD的定時(shí)器");
    });
    //執(zhí)行定時(shí)器
    dispatch_resume(timer);
    //需要聲明強(qiáng)引用 避免 釋放
    self.timer = timer;
    // 消除定時(shí)器
    // dispatch_source_cancel(self.timer)
}

15:Xcode低版本支持高版本的iOS系統(tǒng)

打開Xcode顯示包內(nèi)容找到DeviceSupport文件夾
Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
將真機(jī)包拷貝進(jìn)去

16:iOS 本地化:如何找到項(xiàng)目中所有漢字

Snip20181120_1.png
@"[^"]*[\u4E00-\u9FA5]+[^"\n]*?"
不能發(fā)現(xiàn) xib中的漢字。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容