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.
#Add a variable
- In the Library (the left panel), open the Data & Logic tab.
- Under Variables, tap Add variable.
- 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).
#Types
A variable's type says what it holds. It shows under the variable's name; tap ⓘ beside it for a short explanation.
| Type | What it holds | A new one is named | Starts as |
|---|---|---|---|
| Text | Words and sentences, like a name or a message. | text | Empty |
| Whole Number | Whole numbers like 0, 7 or −3: counts, scores, quantities. Decimals are rounded. | count | 0 |
| Decimal Number | Numbers with decimals, like 3.5 or 19.99: prices and measurements. | amount | 0 |
| True / False | Yes or no, on or off: whether a switch is on, whether a task is done. | isOn | Off (false) |
| Date & Time | A day and time, like a birthday or a deadline. | date | The start of today |
| Color | A color, like one someone picks for a theme. | color | #5B6CFF |
| Web Address | A web address starting with https://, like a profile link. | link | Empty |
| Record | Named fields kept together, like a person with a name, an age and an email. | record | No fields |
| Anything | Anything at all. Handy for an API's answer before you know its shape. | value | Empty |
| List of… | An ordered list of one kind of item: Text, Whole Numbers, Decimals, True / False, Dates or Records. | items | Empty |
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:
| Type | How you set the starting value |
|---|---|
| Text | Type in the box. |
| Web Address | Type an address. If it doesn't start with https://, a note under the box says so. |
| Whole Number | Type a whole number. |
| Decimal Number | Type a number, like 19.99. |
| True / False | Flip the switch: On (true) or Off (false). |
| Date & Time | Tap the date to pick a day and a time. |
| Color | Tap the color well to pick a color. Its code, like #5B6CFF, shows beside it. |
| Record | Add fields, each with a name and a value. See Records. |
| List of… | Add items, numbered from 0. See Lists. |
| Anything | Nothing 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.
- Tap Add field and pick the field's type: Text, Decimal Number, True / False, Date & Time, Color, Web Address, List of Text or Record.
- The field is named
field1(thenfield2, 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. - 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
- Tap Add item. The item gets the list's type and its starting value.
- 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.
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
- Tap the variable's name.
- Type the new name.
- 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
- Tap the type under the variable's name, like Text.
- Pick the new type. The menu is the same as Add variable's.
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.
- Tap the ⋯ button at the right of the variable's name.
- 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 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
- Tap the ⋯ button at the right of the variable's name.
- 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:
| Message | What 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
- Show it: link a view's setting with ⚡︎, or put the name in braces in text:
Hello, {name}!. See Connecting views to data and Text with values. - Change it: link a control (a Text Field, a Switch, a Slider) to it, or use a step like Set Value, Flip On/Off or Add to List.
- Work something out from it: use its name in a formula:
count + 1,client.email,items[0].