首页 > 其他分享 >Xcode Snippets 功能详解

Xcode Snippets 功能详解

时间:2023-08-04 10:06:20浏览次数:39  
标签:code text Xcode snippet 详解 Snippets your snippets


http://nshipster.com/xcode-snippets/


iOS development all but requires the use of Xcode. To its credit, Xcode has improved pretty consistently over the last couple of years. Sure, it still has its… quirks, but hey—things could be much, much worse.

Working in an IDE may not be as cool as working in your favorite decades-old editor (or that other one), but you know what is cool? Autocompletion. Not to mention Build & AnalyzeBreakpoints, andInstruments.

This is all to say: if we’re resigned to use an IDE in our development workflow, we might as well make the most of it, right? So this week on NSHipster, we’re going to talk about one of the more powerful yet underused features of Xcode: Code Snippets.


From @interface declarations to if (!self) return nil;

Using Xcode Snippets

To see the available code snippets, show the Utilities panel, to the right of your editor. On the bottom half the Utilities panel, there will be a horizontal divider with 4 icons.

Click the { }

There are two ways to insert a snippet into your code:

You can drag and drop from the code snippets library into your editor:

…or for snippets that include a text completion shortcut, you can start typing that:

To get a sense of what you can do with snippets, here’s an overview of the ones built-in to Xcode:

typedef

  •  declarations for 

enum

structunion

  • , and blocks
  • C control flow statements like 

if

if

else

  • , and 

switch

  • C loops, such as 

for

while

  • , and 

do

while

  • C inline block variable declaration
  • Objective-C declarations for 

@interface

  •  (including for class extensions and categories),

@implementation

@protocol

  • Objective-C boilerplate for KVO, including the relatively obscure

keyPathsForValuesAffecting<Key>

  • , used for registering dependent keys
  • Objective-C boilerplate for Core Data fetches, property accessors, and property validation
  • Objective-C idioms for enumerating 

NSIndexSet

  • Objective-C incantation for 

init

initWithCoder:

  •  and 

initWithFrame:

  • Objective-C 

@try

  •  / 

@catch

  •  / 

@finally

  •  and 

@autorelease

  • GCD idioms for 

dispatch_once

  •  and 

dispatch_after

Creating Xcode Snippets

Of course, what really makes snippets such a powerful feature is the ability to create your own.

The process of creating a snippet is actually pretty unintuitive and difficult to explain. It uses an obscure OS X system feature that allows users to create a “Text Clipping” by dragging and dropping selected text. Much easier to just show it in action:

After being added to the code snippet library, a user-defined snippet can be edited by double-clicking its listing:

Each snippet has the following fields:

  • Title
  • Summary
  • Platform
  • Language
  • Completion Shortcut
  • Completion Scopes - Limits the snippet visibility for text completion to the specified scopes. For example, an 

if

  •  / 

else

  •  statement should only be auto-completed from within a method or function implementation. Any combination of the following:
  • All
  • Class Implementation
  • Class Interface Methods
  • Class Interface Variables
  • Code Expression
  • Function or Method
  • Preprocessor Directive
  • String or Comment
  • Top Level

Each Xcode snippet has a file representation in~/Library/Developer/Xcode/UserData/CodeSnippets/

Placeholder Tokens

Something you may have noticed in using other Xcode snippets are placeholder tokens:

In Xcode, placeholder tokens are delimited by <# and #>, with the placeholder text in the middle. Go ahead—try typing that into Xcode, and watch as the text between the octothorp tags magically transforms right in front of your eyes.

Include placeholder tags to add a dash of dynamism in your own snippets!

Third-Party Xcode Snippets

A list of generally useful code snippets can be found in this GitHub project (pull requests welcome!). If nothing else, this also serves as an example of what’s possible.


Programming isn’t about being an expert typist, so don’t make it any more difficult for yourself than it needs to be. If you find yourself groaning while typing some inane, rote-memorized code idiom, take a minute to create a snippet for it instead!


nsmutablehipster

Questions? Corrections? Issues and pull requests are always welcome — NSHipster is made better by readers like you.

Find status information for all articles on the status page.



标签:code,text,Xcode,snippet,详解,Snippets,your,snippets
From: https://blog.51cto.com/u_16124099/6957173

相关文章

  • React Hooks 中的属性详解
    ReactHooks是React16.8版本中新增的特性,允许我们在不编写class的情况下使用state和其他的React特性。Hooks是一种可以让你在函数组件中“钩入”React特性的函数。以下是一些常用的ReactHooks,并附有详细的用法和代码示例。1.useStateuseState是一个Hook函数,让......
  • 第一节:Lvs软件负载技术详解
    一.        二.        三.         !作       者:Yaopengfei(姚鹏飞)博客地址:http://www.cnblogs.com/yaopengfei/声     明1:如有错误,欢迎讨论,请勿谩骂^_^。声     明2:原创博客请在转载......
  • Hive Merge详解
    说明Hive在2.2版本之后开始支持Merge操作,并且Merge只能在支持ACID的表上执行语法MERGEINTO<targettable>ASTUSING<sourceexpression/table>ASSON<booleanexpression1>WHENMATCHED[AND<booleanexpression2>]THENUPDATESET<setclauselist>WHEN......
  • 9.2.Config Server 配置规则详解
    9.2.ConfigServer配置规则详解在上面,我们用于测试的配置文件:futurecloud.ymlfuturecloud-dev.ymlfuturecloud-test.ymlfuturecloud-pre.ymlfuturecloud-stable.ymlfuturecloud-apigetway-zuul.yml“-”前面的部分可以随便定义,一般我们用应用名来定义,后面的deb、test…也可以随......
  • spring-mvc系列:详解@RequestMapping注解(value、method、params、header等)
    目录一、@RequestMapping注解的功能二、@RequestMapping注解的位置三、@RequestMapping注解的value属性四、@RequestMapping注解的method属性五、@RequestMapping注解的params属性六、@RequestMapping注解的header属性七、SpringMVC支持ant分格的路径八、SpringMVC支持路径中的占......
  • 【jmeter系列】仅一次控制器+正则表达式用法详解
    一、仅一次控制器做性能测试经常遇到这样的问题:要做接口压力测试,但是需要登录接口返回的token,但是不需要对登录接口进行压测,这个时候jmeter仅一次控制器就起到了关键性的作用。具体操作如下:1、添加仅一次控制器,并在控制器下添加登录接口 2、登录接口返回响应数据-Responesb......
  • update-alternatives 使用详解
    alternatives管理方式$ls-l/usr/bin/pythonlrwxrwxrwx1rootroot2411202017/usr/bin/python->/etc/alternatives/python$ls-l/etc/alternatives/pythonlrwxrwxrwx1rootroot1811212017/etc/alternatives/python->/usr/bin/python2.7python这个可执行命......
  • JAVA系列之类加载机制详解
    文章目录一、类加载过程二、类生命周期1、加载2、验证3、准备4、解析5、初始化三、双亲委派机制四、自定义类加载器的应用1、源代码加密2、隔离加载类3、热加载/热部署4、扩展加载源类的加载机制?双亲委派机制?什么是类加载器?自定义类加载器有哪些应用场景?通常,在关于Java的......
  • ios自动化-Xcode、WebDriverAgent环境部署
    环境准备:Mac电脑一台(更新到最新的macOS,此文档使用的为macOS12.3.1,Intel芯片)Xcode最新稳定版(此文档使用的为13.4.1)IOS设备(此文档使用了10.3.3-15.1均有,且都安装WebDriverAgent成功)保证自己电脑有大于25G的可用内存空间,Xcode很大!Xcode下载https://developer.apple.com/xcode/r......
  • mysql事务和索引详解
    mysql事务和索引详解1.事务注:事务就是一组操作的集合,成功一起成功,失败一起失败事务控制:开启事务:starttransaction;/begin;提交事务:commit;回滚事务:rollback;使用方法:首先graphTBid1[开启事务]-->id2{执行mysql}id2-->|语句全部执行成功|id3>提交事务]id2-->|语......