3

I am new to swift and trying to implement a simple function that takes minimum and max number as input and returns an array with all the numbers in the limit. I am getting an error //Error: Reference to generic type 'Array' requires arguments in <...> may I know what I am missing on?

func serialNumberLimits(minimumNumber n1:Int, maximumNumber n2:Int) -> Array {

// Initialized an empty array
var array = Int[]()

//Initialized a "Temp" variable
var temp:Int = 0


for index in n1..n2 {

    temp += n1
    n1++

    if index == 1 { array.insert(temp, atIndex: 0) }

    else { array.insert(temp, atIndex: index-1) }

}

return array

}
1
  • 1
    Note that [Int](n1 ... n2) gives you an integer array with given range (using the "new" array syntax from Beta 3). Commented Jul 22, 2014 at 20:48

1 Answer 1

5

Use following function
1)As you are using n1 in function and changing its value so declare it as var as all parameters are constants in swift by default

2)Use Array<Int> as it needs to be define which type of array is in swift.Swift is strongly typed language so all type need to be defined.

Run following code it will compile with no errors

func serialNumberLimits(var minimumNumber n1:Int, maximumNumber n2:Int) -> Array<Int> {

    // Initialized an empty array
    var array = Int[]()

    //Initialized a "Temp" variable
    var temp:Int = 0


    for index in n1..n2 {

        temp += n1
        n1++

        if index == 1 { array.insert(temp, atIndex: 0) }

        else { array.insert(temp, atIndex: index) }

    }

    return array

}
Sign up to request clarification or add additional context in comments.

3 Comments

This won't compile on the newest beta. The exclusive range operator has been replaced by ..< and the array convenience syntax has been changed to [Int].
Yes it is of beta 2 code as posted in question.I have compiled on Beta 3 and change for beta 2
@Revanth get new xcode as many things changes in beta 3 and 4 like array define var array = [Int](), n1..<n2 changed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.