CKJBeOS
CKJBeOS

Reputation: 83

Dictionnary and Tuple definition in swift

on a classs definition i can set this :

private let GameWallDefinition = ["LL3":(104,0.0,62.0,10),"LL2":(99,0.0,60.0,20),"L0":(94,0.0,0.0,50)]

but i can for more esay way to code add the name to the data of the tuple as :

private let GameWallDefinition[String:(sprite:Int,x:CGFloat,y:CGFloat,z:Int)]() = ["LL3":(104,0.0,62.0,10),"LL2":(99,0.0,60.0,20),"L0":(94,0.0,0.0,50)]

it return an error it is possible to do this ?

Upvotes: 0

Views: 48

Answers (1)

Duncan C
Duncan C

Reputation: 131471

Most of the answer to your question was provided in comments. Pulling it together in an answer:

You have 2 syntax errors:

You need a colon after your variable name, and you shouldn't have parens after the type. You can catch these typos more easily if you format it more nicely:

private let GameWallDefinition: [String: (sprite: Int, x: CGFloat, y: CGFloat, z: Int)] = [
    "LL3": (104, 0.0, 62.0, 10),
    "LL2": ( 99, 0.0, 60.0, 20),
     "L0": ( 94, 0.0,  0.0, 50),
]

You could make it a little easier to follow with a typeAlias for your tuple:

typealias SpritePoint = (sprite:Int,x:CGFloat,y:CGFloat,z:Int)

private let GameWallDefinition: [String: SpritePoint] = [
    "LL3": (104, 0.0, 62.0, 10),
    "LL2": ( 99, 0.0, 60.0, 20),
     "L0": ( 94, 0.0,  0.0, 50),
]

But better yet, make your SpritePoint a struct:

struct SpritePoint {
    let sprite: Int
    let x: CGFloat
    let y: CGFloat
    let z: Int
}

private let GameWallDefinition: [String: SpritePoint] = [
    "LL3": SpritePoint(sprite: 104, x: 0, y: 62.0, z: 10),
    "LL2": SpritePoint(sprite:  99, x: 0, y: 60.0, z: 20),
     "L0": SpritePoint(sprite:  94, x: 0, y: 60.0, z: 50),
]

Upvotes: 1

Related Questions