Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Enums and Structs Structs and their Methods Struct Methods

type for return value , when creating a struct method !!

Hello every one :) i did this code :

    struct StudentsInAlBahaUNI {
var StudentName : String
var ID : Int
var Phnumber : Int
var level = 1



init (fandlName : String , PhoneN: Int , StudentNumber : Int ) {
    self.StudentName = fandlName
    self.ID = StudentNumber
    self.Phnumber = PhoneN
    self.level = 1
}
func AllInformation () -> (String) {
    return "\(self.StudentName) \(self.Phnumber) \(self.ID) \(self.level) "
}

}

and it gave me a true outcome but i'm confused because when i tried to specify a (String , Int) type to what will return it showed an errors !!

3 Answers

  1. In your code above all information is returned as a single string value using string interpolation, therefore, it does not generate any error.
  2. In my solution each part is returned as a separate value and the reason why we need to mention Int 3 times is that we are returning 3 integer values. The second option has more usage because you are returning values separately, which can be referred to later in the app ,if required. For example: You can assign each value returned by function to seperat variables:
var name = frstStudent.AllInformation().0
var id = frstStudent.AllInformation().2

Or We can make it more descrpitve by changing the function declaration as following:

func AllInformation() -> (name: String, phone: Int, ID: Int, level: Int) {
        return (self.StudentName, self.Phnumber, self.ID, self.level)
    }

In the function above, we have named our parameters so we can refer to them by name. Example:

var name = frstStudent.AllInformation().name
var level = frstStudent.AllInformation().level

This way we can work with each value returned by function separately while it is not possible when returning them as a single string.

The concept I explained above is covered in Tuples lesson.

نو پروبلم :)

Hello, did you mean something like this? If yes, the reason why it was showing you error was because your function was returning a string.

struct StudentsInAlBahaUNI { var StudentName : String var ID : Int var Phnumber : Int var level = 1

init (fandlName : String , PhoneN: Int , StudentNumber : Int ) {
    self.StudentName = fandlName
    self.ID = StudentNumber
    self.Phnumber = PhoneN
    self.level = 1

}

func AllInformation() -> (String, Int, Int, Int) { return (self.StudentName, self.Phnumber, self.ID, self.level) } }

Hope it helps

السلام عليكم فرهد أنا جالسة أقول أنو حددت String و Int وطلع لي خطأ في كتابة البرنامج ولما حددت String فقط طلعت لي المخرجات صح !! فاستغربت هو المفروض يطلب مني تحديد String و Int !!! خذ الكود اللي انا كاتبته وجربه يطلع صح مايطلع أي خطأ

السلام عليكم فرهد أنا جالسة أقول أنو حددت String و Int وطلع لي خطأ في كتابة البرنامج ولما حددت String فقط طلعت لي المخرجات صح !! فاستغربت هو المفروض يطلب مني تحديد String و Int !!! خذ الكود اللي انا كاتبته وجربه يطلع صح مايطلع أي خطأ

واو جربت الكود حقك وطلع لي صح ! ليش طيب ! يمكن لأنو انت ماتب ثلاث مرات Int !! طيب إذا انو هذا السبب فإيش الفرق يعني أقصد ليه مانكتب Int مره وحدة فقط !!

علیکم السلام٬انا لست عرب لكنني أحاول تعلم اللغة العربية. وأنا أعلم فصحی العربية قليلا. Ok what i understood from your message is that why do we need to put Int 3 times. It is because the function is returning 1 string and 3 integer values ( phone number, ID, level)

سلام

Please correct me if I didn't understand what you meant

' but all are the same type why we have to specify them 3 times?! beside the thing that i'm confused about is in my code the way that you see above is give me a correct outcomes , you know it didn't ask me to specify another return type as Int to represent the ( phone number, ID, level)

if you didn't understand what is my purpose please check out my code in the editor and see the outcomes . . .

and sorry i thought you are Arabian . . . :)

' but all are the same type why we have to specify them 3 times?! beside the thing that i'm confused about is in my code the way that you see above is give me a correct outcomes , you know it didn't ask me to specify another return type as Int to represent the ( phone number, ID, level)

if you didn't understand what is my purpose please check out my code in the editor and see the outcomes . . .

and sorry i thought you are Arabian . . . :)