Taking a screenshot of the Xcode window is the obvious approach, and it gives the worst result. The capture brings along everything else that was on screen: the cursor, the selection highlight, the gutter, and the editor's width rather than the width your code needs.
Copy the text instead. Here is the whole process.
1. Copy the code, not the screen
Select the code in Xcode and press ⌘C. Then paste it into Codeshot with ⌘⇧V.
The snippet is highlighted again from scratch, so none of Xcode's editor state comes with it. No cursor, no selection, no gutter icons. Just the code.
It also fixes indentation. Xcode uses tabs by default, and many places you paste will render a tab far wider than you expect. Starting from the text means you control how it looks.
2. Pick a theme
Codeshot ships an Xcode theme in light and dark. It matches what your audience already looks at every day, so it is a safe default.
The reason to pick something else is contrast. Xcode's light theme is fairly soft, which is fine full size in an editor and less fine once the image is scaled down in a feed. If it is going somewhere people will view it small, choose a higher-contrast theme. You can compare all 30 side by side here.
3. Trim it down
Ten to fifteen lines is about the limit for something readable on a phone. A whole file is not.
Fold the parts that are not the point before you copy, and keep only what makes your argument. If context is genuinely needed, one comment line at the top does more work than twenty lines of surrounding code.
4. Size it and export
Let the size follow your content, or lock it to a ratio. Wider ratios like 16:9 keep long lines from wrapping. Taller ones like 3:4 waste less space when the snippet is narrow.
Export at 2× or 3×, never 1×, or it will look soft on a retina display. Then save with ⌘S, or copy straight to the clipboard with ⌘⇧C.
That is it
Copy, theme, trim, export. Once your theme and size are set they persist, so every snippet after the first takes a few seconds.
Codeshot runs entirely on your Mac and never uploads your code, which matters if what you are posting belongs to a client or an unreleased build.
There is a Swift-specific page with theme suggestions, and the general workflow if you are not coming from Xcode.