class func shareString(title : String, array: [AnyObject]) -> String {
var strValue: String!
//strValue = "\(title)"
for i in 0...array.count-1 {
let share:[String: String?] = array[i] as! [String : String?]
strValue = "\n\n \(strValue), \n\n \(share["title"]) \n\n\(share["value"])"
}
return strValue!
}
//Optional("\n\n Optional(\"\\n\\n nil, \\n\\n Optional(Optional(\\\"\\\\n LayFlat\\\")) \\n\\nOptional(Optional(\\\"252.0\\\"))\"), \n\n Optional(Optional(\"\\n Die Diameter\")) \n\nOptional(Optional(\"25.0\"))"),
Optional(Optional("\n Output"))
Optional(Optional("6.42096"))
答案 0 :(得分:1)
使用像bellow
这样的可选链接strValue =“\ n \ n(strValue ??”“),\ n \ n(分享[”title“] ??”“) \ n \ n(分享[“值”] ??“”)“
如果值为
,则使用force unwrap变量strValue =“\ n \ n(strValue!),\ n \ n(分享[”标题“]!) \ n \ n(股[ “值”]!)“
答案 1 :(得分:1)
for i in 0...array.count-1 {
let share:[String: String?] = array[i] as! [String : String?]
if let title = share["title"], let value = share["value"]{
print("title: \(title) , Value: \(value)")
//output: title: 252.0 , Value: Die Diameter
}
}
答案 2 :(得分:1)
当你下标时它已经返回可选,你设置值为String?
的字典意味着你得到两次可选的同时指定AnyObject的数组使它成为[[String:String]]
的数组所以不需要把它写成字母再次。还用空字符串初始化strValue
。
var strValue = ""
if let dicArray = array as? [[String:String]] {
for dic in dicArray {
if let title = dic["title"], let value = dic["value"] {
strValue += "\n\n \(title) \n\n\(value)"
}
}
}