Ios 如何从我使用的以下代码中删除可选字符串
使用可选链接,如bellow strValue=“\n\n(strValue??”),\n\n(共享[“标题”]??”) \n\n(共享[“值”]??“Ios 如何从我使用的以下代码中删除可选字符串,ios,swift3,Ios,Swift3,使用可选链接,如bellow strValue=“\n\n(strValue??”),\n\n(共享[“标题”]??”) \n\n(共享[“值”]??“ 如果“强制展开”变量有值,请使用该变量 strValue=“\n\n(strValue!),\n\n(共享[“标题”]!) \n\n(共享[“值”]!) 使用可选链接,如bellow strValue=“\n\n(strValue??”),\n\n(共享[“标题”]??”) \n\n(共享[“值”]??“ 如果“强制展开”变量有值,请使用
当您下标时,它将返回可选,并且您正在使用值
String设置dictionary?
意味着您将获得可选的两次,同时指定AnyObject的数组,使其成为[[String:String]]]
的数组,因此无需再次将其转换为字母。还使用空字符串初始化了strValue
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
}
}
当您下标时,它将返回可选,并且您正在使用值
String设置dictionary?
意味着您将获得可选的两次,同时指定AnyObject的数组,使其成为[[String:String]]]
的数组,因此无需再次将其转换为字母。还使用空字符串初始化了strValue
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
}
}
您需要使用if-let或guard-let来删除选项use-if-let-share:[String:String?]=array[i]as![String:String?]{….}我只是简单地使用了!我的问题是解决了类func shareString(title:String!,array:[AnyObject])->String{var strValue:String!strValue=“(title!)”如果array.count>0{for i in 0…array.count-1{let share:[String:String]=array[i]as![String:String]strValue=“\n\n(strValue!)(共享[“title”]!)(共享[“value”]!”}返回strValue!}如果要删除,则需要使用if let或guard let选项如果让共享:[String:String?]=array[i]as![String:String?]{….}我只是简单地使用了!我的问题是解决了类func shareString(title:String!,array:[AnyObject])->String{var strValue:String!strValue=“(title!)”如果array.count>0{for i in 0…array.count-1{let share:[String:String]=array[i]as![String:String]strValue=“\n\n(strValue!)(share[“title”!)(share[“value”!)”}返回strValue!}
title
和value
仍然是选项
,因为字典的类型是[String:String?]
意味着值是可选的,因此您需要包装可选的两次。您应该使用正确的swift模式,例如用于数组中的{……如果让元素为?[String:String?]{..标题和值仍然是可选的,因为字典的类型是[String:String]
意味着值是可选的,因此您需要包装可选的两次。您应该使用正确的swift模式,例如,用于数组中的{……如果让元素为?[String:String?]{..strValue!
将从OP开始崩溃,尚未初始化strValuestrValue!
将从OP开始崩溃,尚未初始化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)"
}
}
}