Xcode幫助文檔

2017-03-31 iOS_Docs

變更記錄

序號 錄入時間 備注
1 2017-03-31 新建文章
2 2018-02-28 更新引言
3 2018-04-22 更新目錄,去除標(biāo)簽
4 2019-09-30 更新圖片
5 2020-03-21 更新格式

TODO

  • 更新Xib和AutoLayout
  • Xcode11新特性

引言

對于iOS開發(fā)中,Xcode是我們天天打交道使用的官方開發(fā)工具,經(jīng)過許多個版本的迭代,Xcode已經(jīng)越來越好用,功能越來越強(qiáng)大,開發(fā)者雖然經(jīng)常使用,但是不一定清楚Xcode的一切奇贏技巧。本篇將介紹從常用快捷鍵、debug方法以及常見問題等來全面介紹Xcode。

目錄

[Build settings reference](#Build settings reference)
[Scheme Menu](#Scheme Menu)
添加文件
搜索
Signing
Distribution發(fā)布App
Debug
[Code Test](#Code Test)
附錄
參考

<a id="Build settings reference"></a>Build settings reference

構(gòu)建設(shè)置參考

<a id="Scheme Menu"></a>Scheme Menu

Use the scheme menu to switch the active scheme and destination. You can also use the scheme menu to create, edit, and manage schemes, and to add and download simulators.
使用“計劃”菜單切換活動方案和目標(biāo)。您還可以使用“計劃”菜單創(chuàng)建、編輯和管理計劃,并添加和下載模擬器。

image

Schemes and destinations are distinct. Schemes specify the settings for building, running, testing, profiling, analyzing, and archiving your project.
Destinations specify installation locations for your app.
Typically, a scheme exists for each target in your project. Destinations exist for physical devices and simulators.

計劃和目的地是不同的。計劃指定設(shè)置、運行、測試、分析、分析和歸檔項目的設(shè)置。目的地指定應(yīng)用程序的安裝位置。通常,項目中的每個目標(biāo)存在一個計劃。目的地存在物理設(shè)備和模擬器。

<a id="添加文件"></a>添加文件

  1. 如果要將文件和文件夾駐留在項目文件夾中,請選擇此選項。
    Copy the files and folders into the project:
    Select “Copy items if needed.”

  2. 如果希望該組結(jié)構(gòu)與文件結(jié)構(gòu)相同
    Create groups for each folder: Select “Create groups.”

  3. 如果要將文件夾及其內(nèi)容顯示在項目導(dǎo)航器中,但實際上不駐留在項目中,請選擇此選項。文件夾引用是項目導(dǎo)航器中文件系統(tǒng)中的文件夾的引用。
    Create references for each folder: Select “Create folder references.”

Alt text

刪除文件

Alt text

瀏覽項目中的類、函數(shù)和其他符號

Alt text

<a id="搜索"></a>搜索

1. Find and replace content in a project

Alt text

2. Find symbol references

Alt text

3. Find the call hierarchy of a method

Alt text

4. Scope a find or replace operation

Alt text

Add code annotations to the jump bar

Alt text
  1. Add a to-do item: Insert a comment with the prefix TODO:. For example: // TODO: [your to-do item].

  2. Add a bug fix reminder: Insert a comment with the prefixFIXME:. For example: // FIXME: [your bug fix reminder].

  3. Add a heading: Insert a comment with the prefix MARK:. For example: // MARK: [your section heading].

  4. Add a separator line: To add a separator above an annotation, add a hyphen (-) before the comment portion of the annotation. For example: // MARK: - [your content]. To add a separator below an annotation, add a hyphen (-) after the comment portion of the annotation. For example: // MARK: [your content] -.

Create and use code snippets

Alt text

Auto Layout Guide

AutoLayout Guide

Alt text

<a id="Signing"></a>Signing

1. Add apple ID

Alt text

2. Assign a project to a team

In order to sign your app, each target in your Xcode project must be assigned to the same team. If you enroll as an individual, you’re considered a one-person team. If you are not a member of the Apple Developer Program, Xcode will create a personal team for you.

Alt text

3. Launch an app on a device

Alt text

4. Manually sign your app

Use manual signing if you want to create your own provisioning profiles and signing identities. For example, you use one build configuration to develop for the store and another build configuration to develop for distribution outside of the store. You’ll need to sign in to your developer account to perform some of the signing steps manually. Your team role determines what tasks you can perform in Xcode and the developer account.

4.1 Enable manual signing

Alt text

4.2 Download the provisioning profile from your developer account

Alt text

4.3 Import the provisioning profile

Alt text

4.4 Profile講解

Alt text

商店供應(yīng)配置文件是分發(fā)配置文件,授權(quán)您的應(yīng)用程序使用某些應(yīng)用程序服務(wù),并確保你是一個上傳你的應(yīng)用程序。存儲配置文件包含單個應(yīng)用程序ID,該應(yīng)用程序ID匹配一個或多個應(yīng)用程序和分發(fā)證書。你的配置應(yīng)用程序ID間接通過Xcode使用某些應(yīng)用程序的服務(wù)。通過設(shè)置權(quán)限和執(zhí)行其他配置步驟,啟用和配置應(yīng)用程序服務(wù)。有的權(quán)利是App ID啟用(一套你的團(tuán)隊創(chuàng)建的應(yīng)用程序),和其他人在Xcode項目。當(dāng)你上傳你的應(yīng)用程序商店,Xcode簽署應(yīng)用程序包與分布在存儲配置文件引用的證書。

https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/Art/store_provisioning_2x.png

Alt text

查閱設(shè)備上的provisioning profiles

Alt text
Alt text

4.5 檢查Code Signing

Alt text

4.6 **Ad Hoc講解

An ad hoc provisioning profile is a distribution provisioning profile that allows your app to be installed on designated devices and to use app services without the assistance of Xcode. It’s one of the two types of distribution provisioning profiles that you can create for apps. (You use the other type of distribution provisioning profile later to submit your app to the store.) An ad hoc provisioning profile ensures that test versions of your app aren’t copied and distributed without your knowledge.
When you’re ready to distribute your app to testers, you create an ad hoc provisioning profile specifying an App ID that matches one or more of your apps, a set of test devices, and a single distribution certificate.

https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/Art/ad_hoc_provisioning_2x.png

Alt text

Each device in an ad hoc provisioning profile is identified by its unique device ID (UDID). The devices you register and add to a provisioning profile are stored in your developer account. Each individual or organization can register up to 100 devices per product family per membership year for development and testing. You can register 100 devices of each type per year. For iOS apps, you can register 100 iPad, 100 iPhone, and 100 iPod Touch devices.

https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/Art/AdHocProfileLaunch_2x.png

Alt text

You signed the iOS App file using the distribution certificate specified in the ad hoc provisioning profile. The ad hoc provisioning profile was included in the app bundle when you built and archived the app. Then you installed the app on a test device. The app successfully launches if the app’s bundle ID matches the App ID, the signature matches the distribution certificate, and the device is in the device list of the ad hoc provisioning profile.

Bundle ID講解

Bundle ID精確地標(biāo)識單個應(yīng)用程序。在開發(fā)過程中,當(dāng)應(yīng)用程序分發(fā)給客戶時,一個束ID被用來提供設(shè)備和操作系統(tǒng)。例如,游戲中心和應(yīng)用程序購買使用Bundle ID,以確定您的應(yīng)用程序時,使用這些應(yīng)用程序服務(wù)。首選項系統(tǒng)使用此字符串來標(biāo)識給定偏好應(yīng)用程序。同樣,啟動服務(wù)使用Bundle ID來定位一個能夠打開特定文件的應(yīng)用程序,它使用給定標(biāo)識符的第一個應(yīng)用程序。Bundle ID也用于驗證應(yīng)用程序的簽名。

Bundle ID字符串必須是一個統(tǒng)一的類型標(biāo)識符(UTI)只包含字母數(shù)字字符(a-z、A-Z、0-9),連字符(-),和周期(。)。字符串應(yīng)該是反向DNS格式。例如,如果您的組織的域名acme.com和你創(chuàng)建一個應(yīng)用程序命名為您好,您可以指定字符串com.acme.hello作為你的應(yīng)用程序的包ID。
在開發(fā)過程中,您使用一個應(yīng)用程序的BundleID在許多不同的地方,以鑒定你的應(yīng)用程序。

Alt text

Specifically, the bundle ID is located and used as follows:
In the Xcode project, the bundle ID is stored in the information property list file (Info.plist). This file is later copied into your app’s bundle when you build the project.
In iTunes Connect, you enter the bundle ID to identify your app. After your first build is uploaded to iTunes Connect, you can’t change your bundle ID or delete the associated explicit App ID.
In your developer account, Xcode creates an App ID that matches the app’s bundle ID. If the App ID is an explicit App ID, it exactly matches the bundle ID. However, unlike domain names, bundle IDs are case sensitive. If the App ID is lowercase, your bundle ID needs to be lowercase, too.
In iCloud, the container IDs you specify in your Xcode project are based on the bundle IDs of one or more apps.

<a id="Distribution發(fā)布App"></a>Distribution發(fā)布App

Validate an archive of your app

Validate an archive of your app to find out whether it meets minimum store submission requirements.

開發(fā)完打包成Archive后,在提交到AppStore之前可以通過Validate archive來驗證Archive是否有配置問題。

Alt text

Review validation issues found, if any, and click Done.
If no issues are found, a green checkmark appears.
If Xcode doesn’t find an iTunes Connect record for your app, the validation fails. In iTunes Connect, create a record for your app and ensure that the bundle ID matches the bundle ID in your Xcode project.


Alt text

<a id="Debug"></a>Debug

  1. 使用調(diào)試區(qū)域來控制和檢查正在運行的應(yīng)用程序

變量視圖顯示可用在代碼您現(xiàn)在的位置范圍檢查的變量列表。這份名單是披露的層次結(jié)構(gòu),揭示了一個可變的結(jié)構(gòu)各部分的值作為你逐步擴(kuò)大披露三角形。

Alt text

該控制臺包含一個交互式終端類文本區(qū)域。您可以使用它來直接與互動LLDB,請參閱使用打印描述的輸出,并與您的應(yīng)用程序的標(biāo)準(zhǔn)輸入和輸出工作。

Alt text

在調(diào)試區(qū)域中查看變量

  • 在調(diào)試區(qū)域中,單擊暫停按鈕或等待您的應(yīng)用程序在您先前設(shè)置的斷點處停止。

  • 范圍中的變量出現(xiàn)在調(diào)試區(qū)域的變量視圖中。

  • 要查看變量的結(jié)構(gòu),請展開變量左側(cè)的公開三角形。

  • 從變量視圖左下角的彈出菜單中選擇一個范圍選項。

  • 顯示最近訪問的變量:選擇自動。

  • 僅顯示局部變量:選擇本地。

  • 顯示所有變量,寄存器,全局變量和靜態(tài):選擇全部。

  • 要過濾結(jié)果,請在右下角的搜索字段中輸入文本。

Alt text

Specialized Debugging Workflows

This chapter focuses on often encountered but more specialized debugging scenarios and highlights the Xcode debugging tools used to work with them.

Enable Zombie Objects.

Enable Zombie Objects. Replace deallocated objects with a “zombie” object that traps any attempt to use it. When you send a message to a zombie object, the runtime logs an error and crashes. You can look at the backtrace to see the chain of calls that triggered the zombie detector.
開啟Zombie模式之后會導(dǎo)致內(nèi)存上升,因為所以已經(jīng)被釋放(引用計數(shù)為0)的對象被僵尸對象取代,并未真的釋放掉。這個時候再給僵尸對象發(fā)送消息,就會拋出異常,并打印出異常信息,你可以輕松的找到錯誤代碼位置,結(jié)束Zombies時會釋放。它的主要功能是檢測野指針調(diào)用。iOS開發(fā)之Xcode常用調(diào)試技巧總結(jié)

Using the Address Sanitizer

一旦啟用了地址清潔程序,Xcode將在下次運行時重新編譯您的應(yīng)用程序,并添加工具來立即捕獲內(nèi)存違規(guī)并停止應(yīng)用程序。你可以在這樣發(fā)生的地方檢查問題。還提供其他診斷信息,例如故障地址與堆上的有效對象之間的關(guān)系以及分配/釋放信息,這有助于您快速找出并修復(fù)問題。

將應(yīng)用程序的代碼添加到應(yīng)用程序的代碼中會產(chǎn)生性能損失,但是地址清潔程序的效率足以使用交互式應(yīng)用程序定期使用。有關(guān)更多信息和地址清潔劑的演示,請參見此視頻演示:WWDC 2015:高級調(diào)試和地址消毒劑。

Using the Thread Sanitizer

When enabled, the thread sanitizer checks for uninitialized mutexes, thread leaks, unsafe calls in signal handlers, and data races.

Like the address sanitizer, running an app with the thread sanitizer introduces a small performance penalty, which should be negligible when interacting with most apps. For more information and a demo of thread sanitizer in action, see this video presentation: WWDC 2016: Thread Sanitizer and Static Analysis.

參考

  1. iOS崩潰crash大解析
  2. 如何定位Obj-C野指針隨機(jī)Crash(一):先提高野指針Crash率
    Alt text

更多Debug知識點請參考
Debugging with Xcode

Alt text

斷點

發(fā)生異常時暫停


Alt text

當(dāng)測試發(fā)生故障暫停


Alt text

<a id="Code Test"></a>Code Test

View and adjust performance tests

Alt text

<a id="附錄"></a>附錄

調(diào)試技巧

真機(jī)調(diào)試直接截圖到Mac

Alt text

Xcode快捷鍵

1. Open Quickly

都知道 Cmd + Option + O ,但是你知道怎么在輔助編輯器中打開嗎?按住Alt + Enter, enjoy~

2. 交換上下行代碼: Cmd + Option + [ or Cmd + Option + ]

3. To jump to the definition. 光標(biāo)所在處Command + Control + J

或者在輔助編輯器中打開

???+J.

4.切換多個編輯器焦點:

Command + J

5. 文件切換

  • ?+1 - Related items menu that shows files related to what you are currently working on
  • ?+2 - Previous and next buttons to navigate
    between your most recent files
    The rest of the jump bar consists of heierarchial navigation with each level given a separate shortcut.
  • ?+4 - Project level navigation
  • ?+5 - Switch between header and implementation files
  • ?+6 - Navigation within a file

6. 在輔助編輯器中跳轉(zhuǎn)到變量的定義: Command + Alt + 鼠標(biāo)點擊對應(yīng)變量名

7. 在點擊文件名時候,按住Alt將讓文件在輔助編輯器中打開

8. 在光標(biāo)位置顯示Help: ? + ? + ? + /

9. Filter in Navigator

本來熱鍵是Cmd + Option + J,發(fā)現(xiàn)貌似不起作用,就改成Cmd + Option + O

10. 斷點全部disable

Cmd+ Y,灰色
刪除添加某個斷點 Cmd + \

[Xcode8 適配iOS10時遇見的一些坑

](http://m.blog.csdn.net/article/details?id=52602098)
注意:Xcode8內(nèi)置了開啟注釋的功能,位置在這里

Alt text

快捷鍵的設(shè)置在這里


Alt text

加速你的Xcode!

Xcode啟動的時候,設(shè)置Behaviors:
默認(rèn)是:

image

可以將調(diào)試的Debug放在另一個窗口,隱藏一些不需要的NaviBar

image

在斷點的時候:


image

還可以自定義場景,比如寫代碼的時候:


image

還可以跑腳本

Xcode 8的坑

  1. 代碼注釋不能用的解決辦法
    這個是因為蘋果解決xcode ghost,把插件屏蔽了。
    解決方法
    打開終端,命令運行:
    sudo /usr/libexec/xpccachectl
    然后必須重啟電腦后生效
  2. 權(quán)限以及相關(guān)設(shè)置
    注意,添加的時候,末尾不要有空格
    我們需要打開info.plist文件添加相應(yīng)權(quán)限的說明,否則程序在iOS10上會出現(xiàn)崩潰。
    具體如下圖:


    Alt text
  • 麥克風(fēng)權(quán)限:Privacy - Microphone Usage Description 是否允許此App使用你的麥克風(fēng)?
  • 相機(jī)權(quán)限: Privacy - Camera Usage Description 是否允許此App使用你的相機(jī)?
  • 相冊權(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使用語音識別?
  • 日歷權(quán)限:Privacy - Calendars Usage Description 是否允許此App使用日歷?
  • 定位權(quán)限:Privacy - Location When In Use Usage Description 我們需要通過您的地理位置信息獲取您周邊的相關(guān)數(shù)據(jù)
  • 定位權(quán)限: Privacy - Location Always Usage Description 我們需要通過您的地理位置信息獲取您周邊的相關(guān)數(shù)據(jù)
  • 定位的需要這么寫,防止上架被拒。
  1. 字體變大,原有frame需要適配
    經(jīng)有的朋友提醒,發(fā)現(xiàn)程序內(nèi)原來2個字的寬度是24,現(xiàn)在2個字需要27的寬度來顯示了。。
    希望有解決辦法的朋友,評論告我一下耶,謝謝啦
  2. 推送
    如下圖的部分,不要忘記打開。所有的推送平臺,不管是極光還是什么的,要想收到推送,這個是必須打開的喲


    Alt text

PS.蘋果這次對推送做了很大的變化,希望大家多查閱查閱,處理推送的代理方法也變化了。
// 推送的代理
[<UNUserNotificationCenterDelegate>]
iOS10收到通知不再是在
[application: didReceiveRemoteNotification:]方法去處理, iOS10推出新的代理方法,接收和處理各類通知(本地或者遠(yuǎn)程)

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { //應(yīng)用在前臺收到通知 NSLog(@"========%@", notification);}- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { //點擊通知進(jìn)入應(yīng)用 NSLog(@"response:%@", response);}
  1. 屏蔽雜亂無章的bug
    更新Xcode8之后,新建立工程,都會打印一堆莫名其妙看不懂的Log.
    如這些
    subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1,
    屏蔽的方法如下:
    Xcode8里邊 Edit Scheme-> Run -> Arguments, 在Environment Variables里邊添加
    OS_ACTIVITY_MODE = Disable
Alt text
  1. iOS 10 字體隨著手機(jī)系統(tǒng)字體而改變
    當(dāng)我們手機(jī)系統(tǒng)字體改變了之后,那我們App的label也會跟著一起變化,這需要我們寫很多代碼來進(jìn)一步處理才能實現(xiàn),但是iOS 10 提供了這樣的屬性adjustsFontForContentSizeCategory來設(shè)置。因為沒有真機(jī),具體實際操作還沒去實現(xiàn),如果理解錯誤幫忙指正。
    UIFont 的preferredFontForTextStyle: 意思是指定一個樣式,并讓字體大小符合用戶設(shè)定的字體大小。
   */
    myLabel.font =[UIFont preferredFontForTextStyle: UIFontTextStyleHeadline]; /*
 Indicates whether the corresponding element should automatically update its font when the device’s UIContentSizeCategory is changed.
 For this property to take effect, the element’s font must be a font vended using +preferredFontForTextStyle: or +preferredFontForTextStyle:compatibleWithTraitCollection: with a valid UIFontTextStyle.
 */
//是否更新字體的變化
myLabel.adjustsFontForContentSizeCategory = YES;

自定義Xcode快捷鍵

image

<a id="參考"></a>參考

  1. Xcode Help 官方文檔
  2. Supercharging Your Xcode Efficiency
  3. Xcode Tips and Tricks - Part 1: Introduction | Ray Wenderlich
  4. PART 16: 208: Xcode Tips & Tricks
  5. Becoming an Xcode Power User
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,675評論 5 6
  • 下午好! 早上去面試了,感覺就像走過場,可惜這次我沒有熟人,大概又是瀟灑地打一回醬油,以及游車河。 給六枝玫瑰花換...
    傅則寧閱讀 162評論 0 1
  • 2月8日日精進(jìn):敬畏—進(jìn)入—體驗—交給—持續(xù) 1,缺啥補(bǔ)啥,怕啥練啥; 2,一切為我所用,所用為團(tuán)隊家; 3,我...
    王全峰閱讀 139評論 0 0
  • 01 為了生存和溫飽而工作 外出務(wù)工已經(jīng)很多年了,大多數(shù)人都知道出去務(wù)工可以掙錢,比在自己的家鄉(xiāng)要掙得多,因此現(xiàn)在...
    紫小兔閱讀 1,309評論 0 2
  • 接龍客棧懸賞任務(wù).十月瑞麟作。 楓紅如舊, 走兔飛鳩, 莫道來年依然,且把今日留。 獨赴異鄉(xiāng)為異客, 一眼一回眸,...
    瑞麟閱讀 413評論 10 7

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