最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dictionary - Dictionnary and Tuple definition in swift - Stack Overflow

programmeradmin0浏览0评论

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 ?

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 ?

Share Improve this question asked Feb 23 at 11:29 CKJBeOSCKJBeOS 817 bronze badges 5
  • 1 Why not create a struct/class to represent a wall? – Sweeper Commented Feb 23 at 11:41
  • With dictionary i can get the data with the key and it's very easy and powerful. I just want to use named variable into my Tuple for esay using instead of just the "index" of the data in the Tuple – CKJBeOS Commented Feb 23 at 12:17
  • 1 You can still keep the dictionary. I'm talking about then (sprite:Int,x:CGFloat,y:CGFloat,z:Int) part. That should be a struct. – Sweeper Commented Feb 23 at 12:22
  • 1 Your code contains two basic syntax errors that can be easily fixed. Look up how to declare a property in swift and your declaration will work the way you want it to. – Joakim Danielson Commented Feb 23 at 12:47
  • Hi @JoakimDanielson I know i have error but can't know to correct them :) I learn Swift syntax day after day, i used before Obj-C – CKJBeOS Commented Feb 23 at 14:46
Add a comment  | 

1 Answer 1

Reset to default 1

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),
]
发布评论

评论列表(0)

  1. 暂无评论