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 Generics in Swift Generic Functions, Parameters and Constraints Generic Functions

Aananya Vyas
Aananya Vyas
20,157 Points

confused with the return type.

the question is:

Write a function named duplicate with a single generic type parameter T. The function takes two arguments, item of type T, and numberOfTimes of type Int, and returns an array of type T. The function simply creates an array containing the element duplicated by the number of times specified. For example, calling duplicate(item: 1, numberOfTimes: 4) returns [1, 1, 1, 1].

pl go through the code attached (cant understand how to define the return type.)

generics.swift
 let arr : T

func duplicate<T>(item : inout T , numberOfTimes: Int) -> arr {

    var i = 0
    for i in numberOfTimes {
        arr[i] = item
        i++
    }
    }
   print(arr)


duplicate(item: &2, numberOfTimes: 2)

8 Answers

Here's the code I used to complete this task. Hope it helps :)

func duplicate <T> (item : T,numberOfTimes: Int)-> [T]{
    var arr : [T] = [T]()
    for _ in 0...(numberOfTimes - 1){
        arr.append(item)
    }
    return arr
}
Randell Pur
Randell Pur
4,934 Points

Could you explain how you came to this answer? It worked for me but I am still confused on how this works.

as the very moment you put this -> in your function you'll need the return key if not it will be a simple function with no return key as

func anyfunc() {

}

Qasa Lee
Qasa Lee
18,915 Points
import Foundation
//Write a function named duplicate with a single generic type parameter T. The function takes two arguments, item of type T, and numberOfTimes of type Int, and returns an array of type T.
//The function simply creates an array containing the element duplicated by the number of times specified. For example, calling duplicate(item: 1, numberOfTimes: 4) returns [1, 1, 1, 1].

func duplicate<T>(item: T, numberOfTimes: Int) -> Array<T> {
    var a: [T] = []
    for _ in (1...numberOfTimes) {
        a.append(item)
    }
    return a
}

//duplicate(item: 1, numberOfTimes: 4)
//duplicate(item: "Hi~", numberOfTimes: 3)

This may help, good luck!

carriebarnett
carriebarnett
16,732 Points

After researching a bit, I found a link that had a code example that is very close to what we are working on here in the code challenge and it helped me figure it out. http://austinzheng.com/2015/09/29/swift-generics-pt-2/

Look under the Subtile "Generic Functions" and there is a code example.

// Given an item, return an array with that item repeated n times func duplicate<T>(item: T, numberOfTimes n: Int) -> [T] { var buffer : [T] = [] for _ in 0 ..< n { buffer.append(item) } return buffer }

great find!

Yuqing Lu
Yuqing Lu
7,032 Points

func duplicate<T>(item: T, numberOfTimes: Int) -> [T] { return [T](repeating: item, count: numberOfTimes) }

it works for me.

Clinton Johnson
Clinton Johnson
28,713 Points

Aananya Vyas yea i agree with you and the example above just made it more confusing, Can someone explain it for us.

func duplicate<T>(item: T, numberOfTimes: Int) -> [T] {
    var arr: [T] = []
    for _ in 0..<numberOfTimes {
        arr.append(item)
    }
    return arr
}

As a returning function you need to use the return key in your function it should looks like

func (a)->arr {

var i = 0 return(i) }