1

I have done this in Objective-C but I can't do what I want to do in Swift.
I am trying to rotate a 2 dimensional array of any Type. I am using generics so that I could Strings & Ints or any other type.

import UIKit

let someArray = [[1,2,3],[7,8,9],[11,93,87]]

print(someArray[0])


func rotateArray<T> (array:[[T]]) ->Array{
    var tempArray = [[T]]()

    for i in 0..<array.count{

        for j in 0..<array.count{

            tempArray[j][array.count-i-1] = array[i][j]
        }

    }
    return tempArray

}

someArray.count

let x = rotateArray(someArray)

However I get the following errors ( There could be other errors that I am not aware of), I also read this question and some others but couldn't relate to it.

  • reference to generic type 'Array' requires arguments in <..>
  • Binary Operator '..<' Cannot be applied to two 'Int' operands
  • Edit after fixing the initial two errors: fatal error: Index out of range

What are the things that I am doing wrong? Kindly include details, I am a complete noob.

0

2 Answers 2

2

You have written the return type -> Array, but since Array is generic, you need to specify what it contains, such as Array<Something> or equivalently [Something].

Seemingly, you want to return the same "shape"/type of array as the input, so you can use -> [[T]] for your return type.

(I'm not sure why the compiler produced an error about ..<, but it goes away if you fix the first issue.)

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

4 Comments

OK, now everything is fine, except for this line: let x = rotateArray(someArray) which generates this error: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0) and looking into here doesn't seem to solve my issue since I have passed in the correct parameter...
If you run the code, you'll see fatal error: Index out of range in the console output. Now that it compiles, you just need to debug your algorithm :-)
I am not so sure but it seems the error is a result of me jumping into placing a value an index other than 0. Because I printed all the value of indexes and I don't get any negative value. My first index is [0][2] and right there I get the fatal error. Any idea? Can you direct me into some direction?
I think the problem is how you are using tempArray. You can't use tempArray[0]=x unless the index 0 already exists, but your tempArray starts out empty. Perhaps you can start with an array full of zeros, or you can use append.
1

In addition to making your method return type [[T]], you have other problems here. You are instantiating tempArray (the array that will hold the arrays inside), but you are not instantiating those inner arrays. And you can't just use subscript operator, but rather you have to append to your respective arrays.

For example, if you want to rotate clockwise 90 degrees, it would be:

func rotateArray<T> (array:[[T]]) -> [[T]] {
    var tempArray = [[T]]()

    for column in 0 ..< array.first!.count {
        var rowArray = [T]()
        for row in (0 ..< array.count).reverse() {
            rowArray.append(array[row][column])
        }
        tempArray.append(rowArray)
    }

    return tempArray
}

Thus

[[1, 2, 3],
[7, 8, 9],
[11, 93, 87]]

Becomes

[[11, 7, 1],
[93, 8, 2],
[87, 9, 3]]

6 Comments

Thanks Rob. How exactly are you solving the issue of the inner array--not being instantiated? Also why can't we use subscript operator?
The first thing we do in the for j ... loop is we append a [T]() to the array. That's where we're instantiating the inner array. Why can't you use the subscript operator? The array subscript operator (unlike dictionaries) means "replace the item at this index with this value". It's not used for appending to arrays, only updating them.
you mean if I had filled the array with nils or anything before, then I would have been able to use subscript operators?
Yeah, but then you'd have to be dealing with an matrix of optional T objects (perhaps implicitly wrapped ones), which seems like you're changing the nature of the matrix that was passed to us.
Thanks. I had to study stride...this is much more readable. My own answer's logic is OK only the index would still be out of bounds because it is jumping to array [0][2] in the beginning. You are doing in a reverse format which doesn't jump into a specific index. Just for learning purposes can you tell me how it can be done my way? And also why did you add the first!
|

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.