简体   繁体   中英

Is is possible to enable Xcode autocompletion of Swift code snippets like 'iflet' in a top level context?

Is is possible to enable Xcode autocompletion of basic Swift language code snippets like 'iflet' in a top level context ? If yes, then how?

Use case: Just experimenting with simple basic Swift syntax in the main.swift file of a new Command Line Tool project. Just minimalist tinkering stuff that is not required to be inside of struct , func , etc.

main.swift Xcode template

import Foundation

print("Hello, World!")

In the top level context of a freshly created main.swift , ifl does not autocomplete to anything useful:

在此处输入图像描述

However, inside a func inside a struct , then ifl autocompletes to an 'If-Let Statement':

在此处输入图像描述

Another use case: Or, maybe just doing some simple math. A quick calculator.

Another use case: Or, maybe just demonstrating a basic "if statement" to a young not-yet-a-programmer without complicating the demo with other programming constructs.

A workaround is to manually create a duplicate user entry and then change the availability scope.

Some observations of this workaround approach are:

  1. be careful not to mask a completion that you do not want hidden.
  2. does not have an explicit snippet import/export for backing up and/or sharing a snippet set.
    • The user snippets can be found in ~/Library/Developer/Xcode/UserData/CodeSnippets .
    • The auto generated UUID filename be changed to be a user readable filename.
    • The XML .codesnippet file can be moved to (add to) another Xcode installation.
  3. it is tedious to make a duplicate user set by hand of the basic Swift constructs that work at the top code level.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM