Little SwiftUI Tip: Copy Tables. To Notes, To Numbers!

Copy Text Strings are simple and Tables are just 0.01 step further! Either Mac or iOS!It is actually really (like REALLY) simple but unfortunately I didn’t realize that we can do this until currently, due to my ignorance!Plus, there are actually couple…


This content originally appeared on Level Up Coding - Medium and was authored by Itsuki

Copy Text Strings are simple and Tables are just 0.01 step further! Either Mac or iOS!

It is actually really (like REALLY) simple but unfortunately I didn’t realize that we can do this until currently, due to my ignorance!

Plus, there are actually couple little points to be a little careful about here!

So!

Just in case you have couple seconds here, let’s check out on how we can copy some tables to Notes or Numbers really quick!

What about Excels?

The approach SHOULD work, but unfortunately I don’t have it on my laptop, nor want to install it to try it out! So, If that is what you are interested in, give it a try yourself!

Let’s start!

Recap On Copying String

For MacOS, we can use NSPasteboard.

let pasteboard = NSPasteboard.general
pasteboard.clearContents()
let result = pasteboard.setString("Hello from Itsuki!", forType: .string)

There are couple points to be careful about while using NSPasteboard. And one of the most important ones here is to make sure to call either

before writing. Otherwise, the writing will fail, ie: the result will be false.

Also, there are couple little more things we can do with this NSPasteboard, if you are interested, please check out my previous article SwiftUI/MacOS: Working with NSPasteboard.

For iOS, we have this UIPasteboard.

let pasteboard = UIPasteboard.general
pasteboard.string = "Hello from Itsuki!"

⭐⭐⭐ Solution: TSV ⭐⭐⭐

Yes!

Notes & Numbers supports TSV (Tab-Separated Values) string format!

Which means!

All we have to do is to

  1. Format each row into a a tab-separated string, and
  2. Connect the rows into a multiline string with one row per line.

Let’s check it out with some code!

I will be demoing with SwiftUI Tables here just for a little easier visualization, but anything that can be formatted as rows and columns will work!


import SwiftUI

struct CopyTableDemo: View {
@State private var pokemons = Pokemon.pokemonList

@State private var error: Bool = false
var body: some View {

VStack(spacing: 24) {
VStack(alignment: .leading) {
HStack(spacing: 24, content: {
Text("Copy-able Table")
.font(.title3)
.fontWeight(.bold)

Button(action: {
error = false
let pasteboard = NSPasteboard.general
// Before we write, we HAVE TO call either declareTypes(_:owner:), or clearContents()
// Otherwise write will fail, ie: `result` will be `false`.
pasteboard.clearContents()
let tableTSV = getTableTSV()
// for type, use string instead of tabular data!
let result = pasteboard.setString(tableTSV, forType: .string)
error = !result
print(result)
}, label: {
Text("Copy")
})
})

if error {
Text("Error copying table")
.foregroundStyle(.red)
}
}


Table(pokemons) {
TableColumn(Pokemon.indexHeader, value: \.index)
.width(56)
TableColumn(Pokemon.nameHeader, value: \.name)
.width(120)
TableColumn(Pokemon.typeHeader, value: \.typesDisplayString)
.width(120)
}
.frame(width: 320, height: 140)
}
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}


// Create a TSV string for table data compatible with pasting into Notes & Numbers.
func getTableTSV() -> String {
// Map each row into a tab-delimited line.
var tableRowData = pokemons.map(\.dataRow)
tableRowData.insert(Pokemon.headerRow, at: 0)
// Create a multiline string with one row per line.
return tableRowData.joined(separator: "\n")
}
}


struct Pokemon: Identifiable {
var id: Int
var name: String
var types: [PokemonType]

var typesDisplayString: String {
self.types.map({ $0.rawValue}).joined(separator: ", ")
}

var index: String {
return "No.\(self.id)"
}

var dataRow: String {
let values = [self.index, self.name, self.typesDisplayString]
return values.joined(separator: "\t")
}
}

extension Pokemon {
static let indexHeader: String = "Index"
static let nameHeader: String = "Name"
static let typeHeader: String = "Type"

static var headerRow: String {
let headerKeys = [indexHeader, nameHeader, typeHeader]
return headerKeys.joined(separator: "\t")
}
}


enum PokemonType: String {
case poison
case fire
case water
case grass
case electric
}

extension Pokemon {
static let pokemonList = [bulbasaur, charmander, squirtle, pikachu]
static let bulbasaur = Pokemon(id: 1, name: "bulbasaur", types: [.grass, .poison])
static let charmander = Pokemon(id: 4, name: "charmander", types: [.fire])
static let squirtle = Pokemon(id: 7, name: "squirtle", types: [.water])
static let pikachu = Pokemon(id: 25, name: "pikachu", types: [.electric])
}

A lot of boilerplate code, but the only important bit here is the getTableTSV function! Creating a TSV string for table data compatible with pasting into Notes & Numbers!

Another little note here about the NSPasteboard!

If you are like me using the setString(_:forType:) method to add items for copying, the PasteboardType we use here is string! NOT tabularText for Tab-separated fields of text!

If we have used tabularText, we won’t be able to paste anything! I think that this is because what Command+V does under hood is when it tries to retrieve the copied text using string(forType:) , the type it uses is string. Which means if we have used tabularText, all it will see is a nil as the result.

If you have UIPasteboard, it is even simpler!

UIPasteboard.general.string = tableTSV

Done!

Thank you for reading!

That’s it for this little article!

Happy copying & pasting!


Little SwiftUI Tip: Copy Tables. To Notes, To Numbers! was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Itsuki


Print Share Comment Cite Upload Translate Updates
APA

Itsuki | Sciencx (2025-10-22T21:58:14+00:00) Little SwiftUI Tip: Copy Tables. To Notes, To Numbers!. Retrieved from https://www.scien.cx/2025/10/22/little-swiftui-tip-copy-tables-to-notes-to-numbers/

MLA
" » Little SwiftUI Tip: Copy Tables. To Notes, To Numbers!." Itsuki | Sciencx - Wednesday October 22, 2025, https://www.scien.cx/2025/10/22/little-swiftui-tip-copy-tables-to-notes-to-numbers/
HARVARD
Itsuki | Sciencx Wednesday October 22, 2025 » Little SwiftUI Tip: Copy Tables. To Notes, To Numbers!., viewed ,<https://www.scien.cx/2025/10/22/little-swiftui-tip-copy-tables-to-notes-to-numbers/>
VANCOUVER
Itsuki | Sciencx - » Little SwiftUI Tip: Copy Tables. To Notes, To Numbers!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/22/little-swiftui-tip-copy-tables-to-notes-to-numbers/
CHICAGO
" » Little SwiftUI Tip: Copy Tables. To Notes, To Numbers!." Itsuki | Sciencx - Accessed . https://www.scien.cx/2025/10/22/little-swiftui-tip-copy-tables-to-notes-to-numbers/
IEEE
" » Little SwiftUI Tip: Copy Tables. To Notes, To Numbers!." Itsuki | Sciencx [Online]. Available: https://www.scien.cx/2025/10/22/little-swiftui-tip-copy-tables-to-notes-to-numbers/. [Accessed: ]
rf:citation
» Little SwiftUI Tip: Copy Tables. To Notes, To Numbers! | Itsuki | Sciencx | https://www.scien.cx/2025/10/22/little-swiftui-tip-copy-tables-to-notes-to-numbers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.