Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How do I add two values to a Constant?

Im currently doing a school project where im supposed to write a food into a textfield and two values will return in the app, the price and the calories.

This is the example i mean (What im doing might be totally wrong though)

let Chilaquiles = (value1 = 100) (value2 = 370)

When I write in my app "Chilaquiles" I will receive back the price (value 1) and the calories (value 2).

Hope you can help

EDIT: Forgot to add the example, LOL.

2 Answers

Everton Carneiro
Everton Carneiro
15,994 Points

Hello mate. You can use a tuple to do this. For example:

let apple = (price: 10, calories: 100)
// And then access it like this:
apple.price
apple.calories

There is a lot of better ways to do that like making custom types. But I believe that this is the answer you are looking for. I hope that helps, cheers.

I am fairly new to Swift and programming as a whole, how to i create a personal custom type and how do I use them?

Everton Carneiro
Everton Carneiro
15,994 Points

If you are very new to programming, I suggest to start from the beginning. If you follow the basic iOS development track, you should have a good grasp of programming using swift. To create custom types, you need to understand Object oriented programming and since you are very novice, I suggest to learn the basics first like variables, constants, strings, collections, loops and so on. Be patient, and keep it up, mate.

Matthew Long
Matthew Long
28,407 Points

I don't see the example you're referring to. However, it sounds like what you want is a tuple.

You can learn about tuple types on the swift dev page.