PadCodedocs

Variables

Keep your app's data in variables: add one, pick its type, set its starting value, rename it, and remember it after the app closes.

Variables hold your app's data: a count, the name someone types, a list of tasks. Views show them, controls change them, and formulas use them by name. You find them in the Library's Data & Logic tab, under Variables.

Data & Logic in an invoice app: each variable with its name, its type and its starting value.

#Add a variable

  1. In the Library (the left panel), open the Data & Logic tab.
  2. Under Variables, tap Add variable.
  3. Pick what it holds, like Whole Number. For a list, choose List of…, then what each item is.

The new variable is named after its type: count for a Whole Number, text for Text. If that name is taken, PadCode adds a number: count2, count3. Rename it to say what it's for (see Rename a variable).

The Add variable menu: Text, Whole Number, Decimal Number, True / False, Date & Time, Color, Web Address, Record, Anything and List of….

#Types

A variable's type says what it holds. It shows under the variable's name; tap ⓘ beside it for a short explanation.

TypeWhat it holdsA new one is namedStarts as
TextWords and sentences, like a name or a message.textEmpty
Whole NumberWhole numbers like 0, 7 or −3: counts, scores, quantities. Decimals are rounded.count0
Decimal NumberNumbers with decimals, like 3.5 or 19.99: prices and measurements.amount0
True / FalseYes or no, on or off: whether a switch is on, whether a task is done.isOnOff (false)
Date & TimeA day and time, like a birthday or a deadline.dateThe start of today
ColorA color, like one someone picks for a theme.color#5B6CFF
Web AddressA web address starting with https://, like a profile link.linkEmpty
RecordNamed fields kept together, like a person with a name, an age and an email.recordNo fields
AnythingAnything at all. Handy for an API's answer before you know its shape.valueEmpty
List of…An ordered list of one kind of item: Text, Whole Numbers, Decimals, True / False, Dates or Records.itemsEmpty

A list's type reads like List of Records or List of Whole Numbers.

#Starting values

The box under a variable's type is its starting value: what it holds each time the app opens. The canvas shows starting values, so you design with real-looking data. Each type has its own editor:

TypeHow you set the starting value
TextType in the box.
Web AddressType an address. If it doesn't start with https://, a note under the box says so.
Whole NumberType a whole number.
Decimal NumberType a number, like 19.99.
True / FalseFlip the switch: On (true) or Off (false).
Date & TimeTap the date to pick a day and a time.
ColorTap the color well to pick a color. Its code, like #5B6CFF, shows beside it.
RecordAdd fields, each with a name and a value. See Records.
List of…Add items, numbered from 0. See Lists.
AnythingNothing to set. It says "Starts empty. Pick a type to give it a starting value."

#Records

A record keeps several named values together, like a client with a name and an email.

  1. Tap Add field and pick the field's type: Text, Decimal Number, True / False, Date & Time, Color, Web Address, List of Text or Record.
  2. The field is named field1 (then field2, and so on). Tap the name and type a better one. It applies when you press Return or leave the box; a name another field already has puts the old one back.
  3. Set the field's value in the box under its name.

Tap ⊖ beside a field to remove it. Numbers in a record are edited as decimals, so typing 9.99 isn't rounded.

In a formula, read a field with a dot: client.email. Give fields names made of letters, numbers and _, so formulas can read them.

#Lists

  1. Tap Add item. The item gets the list's type and its starting value.
  2. Set the item's value.

Items are numbered from 0: the first item is items[0], the second items[1]. Tap ⊖ beside an item to remove it.

In a List of Records, a new item gets the same fields as the last one, empty, so the list stays like a table.

A List of Records in Data & Logic: each item numbered, with its fields, and Add field and Add item under them.

Lists and records can sit inside each other. A few levels down, the editor shows a short summary instead of every field.

#Rename a variable

  1. Tap the variable's name.
  2. Type the new name.
  3. Press Return, or tap anywhere else.

A name starts with a letter, then uses letters, numbers or _, like totalCost, and has up to 40 characters. While you type a name that breaks the rule, a red note under it says so. Spaces and other characters are left out when the name applies. If the new name is empty or another variable has it, the old name comes back.

PadCode renames the variable everywhere it's used, on every screen and in reusable actions: links made with ⚡︎, steps that change it, formulas, text with {braces} and Show when conditions. In formulas, only the name standing on its own changes. Words in quotes, and names after a dot like params.total, stay as they are.

#Change a variable's type

  1. Tap the type under the variable's name, like Text.
  2. Pick the new type. The menu is the same as Add variable's.
A variable's type menu open in Data & Logic.

PadCode keeps the starting value when it can convert it:

  • A number becomes text: 5 becomes "5".
  • Text that is a number becomes that number, and a Whole Number rounds: 2.6 becomes 3.
  • Text becomes Off (false) when it's empty, "false", "no", "off" or "0", and On (true) otherwise. The number 0 becomes Off; other numbers become On.
  • One value becomes a list with that one item.

If the value can't be converted, like the text "hello" as a Whole Number, the variable starts over with the new type's starting value. A variable with no value at all, like an Anything that's still empty, stays empty.

A variable whose type you picked keeps it while the app runs, too: a value put into it is converted the same way. "false" put into a True / False variable is Off, and 2.6 put into a Whole Number is 3.

#Remember a value

Each time the app opens, a variable starts from its starting value. Remember keeps its latest value instead, so a score, a setting or a list of tasks is still there next time.

  1. Tap the ⋯ button at the right of the variable's name.
  2. Choose Remember on This iPad or Remember in iCloud.

The ⋯ button turns into a green iPad or cloud, so you can see which variables are remembered. To stop, choose Don't Remember.

While the app runs, in Preview, in My Apps or from your Home Screen, PadCode saves a remembered variable each time it changes. The next time the app opens, the variable starts with that saved value. A variable that isn't remembered starts from its starting value again.

Remembered variables: goal and history show a green iPad instead of the ⋯ button.

Remembered values are what an app's widgets show: a widget made for the app reads them on the Home Screen, and its buttons can change them, like a + Glass button that adds to today's count.

#Delete a variable

  1. Tap the ⋯ button at the right of the variable's name.
  2. Choose Delete.

The variable is gone right away, with its type and its Remember setting. Undo brings it back. Views and steps that used it aren't changed: formulas and text with {braces} that still name it show up as errors in Issues, so you can find and fix them.

#Issues about variables

Issues checks your variables as you work:

MessageWhat to do
"“name” isn't used anywhere. Show it, change it, or delete it."A warning: nothing shows or changes this variable. Link it to a view, or delete it.
"“name” can't be used in formulas. Start it with a letter, then use letters, numbers or _."An error: rename the variable.
"There's nothing called name in “…”. Check the spelling, or add it in Data & Logic."An error: a formula names a variable that doesn't exist. Fix the spelling, or add the variable.

#Use a variable

#See also