0
    var playerOneArray : Array<(Int,Int,Int)> = []
    var currentPerson = 1
    var currentWeapon = 1
    var currentRoom = 1
    var currentPlayer = 1

    currentPerson = everyPicker.selectedRowInComponent(0)
    currentWeapon = everyPicker.selectedRowInComponent(1)
    currentRoom = everyPicker.selectedRowInComponent(2)
    currentPlayer = playerPicker.selectedRowInComponent(0)

    //In an if statement
    playerOneArray.append(currentRoom, currentPerson, currentWeapon) as (Int,Int,Int)
    // Error tuple types () and (Int,Int,Int) have a different number of elements (0 vs. 3)

even if i take away the as int,int,int there is still an error and i don't know why this is happening. the error that comes up if i take it away is accessing members of protocol 'int' is unimplemented.

1
  • Looks like you're missing parens .append((a,b,c) as (Int,Int,Int)) Commented Apr 8, 2015 at 22:42

3 Answers 3

2

You are not closing the parenthesis of the append call.

However, because swift knows playerOneArray is an array of 3 Ints. You can simply pass the append method the 3 variables as follows:

 playerOneArray.append(currentRoom, currentPerson, currentWeapon) 

Assuming (currentRoom, currentPerson, currentWeapon) is a tuple of Int values. This will store (currentRoom, currentPerson, currentWeapon) into playerOneArray[0].

As a side note, it seems you are wanting an array of players which holds each players details. If this is the case you should rename the playerOneArray to players and simply add each player's information. That way each index will represent the players information (the tuple of Ints).

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

2 Comments

swiftc returns an error Missing argument for parameter #2 in call. on this. It attempts to expand the tuple into three parameters.
@Brian, yes that is correct, the append method is smart enough to recognize the parameters of the tuple so you can simply pass the 3 Ints. I have updated my code.
0

You've got the right idea but your syntax is incorrect.

The way it's written, Swift is looking for a method with the signature:

func append(Int, Int, Int) -> (Int, Int, Int)

That is, a function named append that takes three Ints and returns a tuple of three Ints. The error you're getting is probably because Swift sees the definition append(T) -> () and is complaining that you return 3 components rather than zero.

You could try to just pass a tuple by adding parenthesis but this would fail because Swift treats a single tuple as a list of parameters so it looks for a signature append(Int, Int, Int) -> () which does not exist:

playerOneArray.append((currentRoom, currentPerson, currentWeapon)) // Missing argument for parameter #2 in call.

The correct solution looks very close to what you were doing (maybe you were hinted in that direction):

playerOneArray.append((currentRoom, currentPerson, currentWeapon) as (Int,Int,Int))

This tells Swift that you mean that tuple to really be a tuple and it successfully finds the signature: append((Int, Int, Int)) -> ().

As a side note, tuples are intended for transferring data more so than storing it. If you expect this data to persist long term you should put it in a struct:

struct Player {
    var person:Int
    var weapon:Int
    var room:Int
}

var playerOneArray:[Player] = []

let player = Player(
    person: everyPicker.selectedRowInComponent(0),
    weapon: everyPicker.selectedRowInComponent(1),
    room: everyPicker.selectedRowInComponent(2))

playerOneArray.append(player)

Comments

0

append is getting three parameters instead of one tuple. Try this:

playerOneArray.append((currentRoom, currentPerson, currentWeapon))

Comments

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.