使用xcode开发c++_使用xcode片段加快开发速度

使用xcode开发c++

Hi everybody, I’m Riccardo. Senior iOS Engineer at Bending Spoons, I breathe iOS development, both apps and tools and I love to share my knowledge with others.

大家好,我是Riccardo。 Bending Spoons的高级iOS工程师,我负责iOS开发,包括应用程序和工具,我喜欢与他人分享我的知识。

Hi everybody, today I’d like to share a trick I use to save time while developing. It’s useful when I write several pieces of code whose structure is repetitive (Test skeleton, Views, Dispatchables, Pattern, …): I write it once and then I save it as an Xcode snippet to reuse it later.

大家好,今天我想分享一个技巧,以节省开发时间。 当我编写几段结构重复的代码(测试框架,视图,可分发对象,模式等)时,这很有用:先编写一次,然后将其另存为Xcode代码段,以供日后重复使用。

如何创建摘录 (How to create a Snippet)

The creation of a snippet in Xcode is extremely easy.

在Xcode中创建代码段非常容易。

First of all, write the piece of code that you want to save as a snippet. For example, let’s write the ViewModel and the View of a screen of your app:

首先,编写要另存为代码段的代码。 例如,让我们编写应用程序屏幕的ViewModel和View:

All the views and view models we have to write share this common structure. The only things that we have to change are the names of the view and the names of the view model. This structure is a great candidate to be saved as a snippet.

我们必须编写的所有视图和视图模型都共享此通用结构。 我们唯一需要更改的是视图的名称和视图模型的名称。 此结构非常适合保存为摘要。

To do so, just select all this code and right-click on it. Choose Create Code Snippet from the contextual menu. The following screen will appear:

为此,只需选择所有这些代码,然后右键单击它。 从上下文菜单中选择“ Create Code Snippet 。 将出现以下屏幕:

Image for post

In this screen, you can customize different aspects of the snippet. In particular, you can:

在此屏幕中,您可以自定义代码段的不同方面。 特别是,您可以:

  • Give a name to the snippet, so it’s easier to find it.

    为代码段命名,以便更轻松地找到它。
  • Choose the programming language.

    选择编程语言。
  • Choose the platform in which it will be used, so that only snippets for the right platforms will appear.

    选择将在其中使用平台的平台,以便仅出现正确平台的代码片段。
  • Choose a completion code. This is extremely useful because whenever in Xcode you type the completion code and press Enter, Xcode will replace the completion code with the snippet code.

    选择一个完成代码。 这非常有用,因为每当您在Xcode中键入完成代码并按Enter ,Xcode就会用代码段代码替换完成代码。

  • The scope availability: where you want to use the snippet.

    作用域可用性:您要在其中使用代码段的位置。

You can also edit the code you want to save as a snippet: you can change the parts of the snippets that you want to customize later by adding the placeholders. To replace a bit of code with a related placeholder, prepend the <# string and append the #> string. So, for example, AVM will become <#AVM#>. After doing that, you will see the snippet appearance change to this:

您还可以编辑要另存为代码段的代码:您可以通过添加占位符来更改要自定义的代码段部分。 要用相关的占位符替换一些代码,请在<#字符串前添加#>字符串。 因此,例如, AVM将变为<#AVM#> 。 完成此操作后,您将看到代码段外观更改为:

Image for post

From this point on, whenever you type TVVM in the editor, Xcode will insert the snippet code and will prompt you to fill the placeholders, as it already does when we use the autocompletion feature for functions’ signatures. Cool, isn’t it?

从这一刻起,每当您在编辑器中键入TVVM时,Xcode都会插入代码段代码,并提示您填充占位符,就像我们在函数自动签名中使用自动完成功能时一样。 不错,不是吗?

Now… how can we get the max out of it? Well, we already saw one of the main use cases: save the structure of some code that you have to replicate over and over. Are there any other use cases?

现在……我们如何才能获得最大收益? 好了,我们已经看到了主要的用例之一:保存必须重复复制的某些代码的结构。 还有其他用例吗?

模式 (Patterns)

An interesting way to use snippets is to build your own library of patterns that can be reused when you need them. Let’s suppose that we have an application that made wide use of the observer pattern.

使用片段的一种有趣方法是构建自己的模式库,可以在需要它们时重用它们。 假设我们有一个广泛使用观察者模式的应用程序。

We can write the following code:

我们可以编写以下代码:

At this point, you can just create the snippet, give it a name, and a completion code and you can reuse it in all the classes that require you to implement the observer pattern. As you can see, the customization points are:

在这一点上,您只需创建代码段,为其指定名称和完成代码,即可在需要实现观察者模式的所有类中重用它。 如您所见,定制点是:

  • the type of the observer, that you should define in your app/class;

    您应该在您的应用/类中定义的观察者的类型;
  • the parameter list of the notify method;

    notify方法的参数列表;
  • the method that should be called on the observers.

    应该在观察者上调用的方法。

自动完成/自动填充 (Autocomplete/autofill)

We all know that Xcode autocomplete is not that good. Sometimes it just stops working and it does not suggests autocomplete or does implements the protocols when you need them. This happens very often when generics are involved.

我们都知道Xcode自动完成不是那么好。 有时,它只是停止工作,并不建议自动完成或在需要时实施协议。 当涉及泛型时,这种情况经常发生。

Another way that I found snippets really useful is to save the default implementations of complex types, that I do not remember, and that I know the autocomplete struggles to help me.

我发现代码片段真正有用的另一种方式是保存复杂类型的默认实现,这是我不记得的,并且我知道自动完成功能很难为我提供帮助。

An example is the dispatchables of our Katana and Tempura apps: I always forget the right interfaces. Therefore I saved the snippets for the base AppStateUpdater and AppSideEffect so that I can recall them immediately in my code.

例如,我们的Katana和Tempura应用程序的可调度对象:我总是忘记正确的界面。 因此,我为基本AppStateUpdaterAppSideEffect保存了代码片段,以便可以在代码中立即调用它们。

结论 (Conclusion)

These ideas can be pushed further but they should not replace proper factorization of the code: this is a sort of copy-and-paste that should be used only when we can’t avoid repetition by using the proper patterns.

这些想法可以进一步推动,但不能代替代码的适当分解:这是一种复制粘贴,仅当我们无法避免通过使用正确的模式进行重复时才应使用。

I hope you can find this article useful. These tricks help me being very fast when I have to write long parts of simple code, removing the cognitive load of remembering implementation details.

希望本文对您有所帮助。 这些技巧可帮助我在必须编写大量简单代码时非常快速,从而消除了记住实现细节的认知负担。

翻译自: https://medium.com/swlh/speed-up-development-with-xcode-snippets-1301803a4b9a

使用xcode开发c++

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值