我一直在尝试抓取这些数据,将其加载到两个数组中,最终将这些数据传递到Swift-Linechart。 目前,我正在使用它来加载数据:
func loadUserData(){
var myDict: NSDictionary?
if let path = NSBundle.mainBundle().pathForResource("UserData", ofType: "plist") {
if let dict = NSDictionary(contentsOfFile: path) as? Dictionary<String, AnyObject> {
userData = dict
}
}
}
一旦我在dict中有了userData,我怎么把它变成一个CGFLoat值的数组,这样我就可以绘制它了?
UserData.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Triggers</key>
<dict>
<key>Alcoholic Drinks</key>
<array>
<array>
<date>2014-12-03T16:28:53Z</date>
<string>3</string>
</array>
<array>
<date>2014-12-04T16:29:33Z</date>
<string>0</string>
</array>
</array>
<key>Caffeinated Drinks</key>
<array>
<array>
<date>2014-12-03T16:30:22Z</date>
<string>2</string>
</array>
<array>
<date>2014-12-04T16:30:02Z</date>
<string>1</string>
</array>
</array>
</dict>
<key>Symptoms</key>
<dict>
<key>Redness</key>
<array>
<array>
<date>2014-12-03T16:25:21Z</date>
<string>0.75</string>
</array>
<array>
<date>2014-12-04T16:25:51Z</date>
<string>0.5</string>
</array>
</array>
<key>Swelling</key>
<array>
<array>
<date>2014-12-03T16:27:00Z</date>
<string>.6</string>
</array>
<array>
<date>2014-12-04T16:27:52Z</date>
<string>.2</string>
</array>
</array>
</dict>
</dict>
</plist>
答案 0 :(得分:1)
假设您将plist中的字符串转换为数字。这将提取症状 - &gt;红色数据:
func parseRednessData() {
let symptoms = userData?["Symptoms"] as [String: AnyObject]
let redness = symptoms["Redness"] as [[AnyObject]]
let floats = redness.map { $0[1] as Float }
println(floats)
}
使用以上作为提取其他数据位的示例。
修改强>
我注意到在原始代码中,floats
实际上是AnyObject
的数组。我解决了这个问题。