我有这个数组,用于在'块上存储一些小数据。在swift。
我曾经有过三个名为blockNames,blockTypes和blockImages的词典。但是,我想把所有东西放在一个地方。我想使用以下格式,但我假设我可以像读取它一样; blockData[0.0]["image"]
获取块0.0的图像。但是它将数组定义为双打,将其中的字典定义为NSObjects。
这是我在谈论的数组
let blockData = [
// ===== 0.0 - Air =====
0.0:[
"name":"Air",
"image":"0.0.png",
"type":"custom"
],
0.1:[
"name":"barrier",
"image":"barrier",
"type":"custom"
],
// ===== 1.0 - Dirt =====
1.0:[
"name":"Dirt - Center",
"image":"1.0.png",
"type":"solid"
],
1.1:[
"name":"Dirt width Grass - Center",
"image":"1.1.png",
"type":"solid"
],
1.2:[
"name":"Dirt width Grass - Left",
"image":"1.2.png",
"type":"solid"
],
1.3:[
"name":"Dirt width Grass - Right",
"image":"1.3.png",
"type":"solid"
],
1.4:[
"name":"Dirt - Left",
"image":"1.4.png",
"type":"solid"
],
1.5:[
"name":"Dirt - Right",
"image":"1.5.png",
"type":"solid"
],
1.6:[
"name":"Dirt with Flat Top - Center",
"image":"1.6.png",
"type":"solid"
],
1.7:[
"name":"Dirt with Flat Top - Left",
"image":"1.7.png",
"type":"solid"
],
1.8:[
"name":"Dirt with Flat Top - Right",
"image":"1.8.png",
"type":"solid"
],
1.9:[
"name":"Dirt - Center",
"image":"1.9.png",
"type":"solid"
],
1.11:[
"name":"Dirt - Bottom Center",
"image":"1.11.png",
"type":"solid"
],
...
]
我想读取数组如下;
let blockID: CGFloat = 0.0
let blockType = blockData[Float(blockID)]["type"]
谢谢。
答案 0 :(得分:0)
问题似乎是你会从blockData[0.0]
获得一个可选项
您需要使用if let
语句或强制解包来解包该可选项。
let blockData = [
// ===== 0.0 - Air =====
0.0:[
"name":"Air",
"image":"0.0.png",
"type":"custom"
],
0.1:[
"name":"barrier",
"image":"barrier",
"type":"custom"
],
// ===== 1.0 - Dirt =====
1.0:[
"name":"Dirt - Center",
"image":"1.0.png",
"type":"solid"
],
1.1:[
"name":"Dirt width Grass - Center",
"image":"1.1.png",
"type":"solid"
],
1.2:[
"name":"Dirt width Grass - Left",
"image":"1.2.png",
"type":"solid"
]
]
blockData[0.0]!["type"]
在swift 2.0中完美运行