简体   繁体   English

无法将类型 [NSObject : AnyObject] 转换为字符串

[英]Cannot convert the type [NSObject : AnyObject] to string

I have this code in Swift using Xcode 7.2 ..我使用 Xcode 7.2 在 Swift 中有此代码..

func silentPostData(serverName: String, serverport serverPort: String, serverurl serverURL: String, parameters: [NSObject : AnyObject], urldata urlData: NSData) {
    let persist = Persistence()
    var finalURl = "https://\(serverName):\(serverPort)/\(serverURL)"
    for key : String in parameters  {
        finalURL = finalURL.stringByAppendingString(key)
        finalURL = finalURL.stringByAppendingString("=")
       finalURL = finalURL.stringByAppendingString((parameters[key] as! String))
        finalURL = finalURL.stringByAppendingString("&")
    }
}

I get an error(mentioned as the title) in the line "for key : String in parameters {" .我在“for key : String in parameters {”行中收到一个错误(作为标题提到)。 Based on the previous answer '(NSObject, AnyObject)' is not convertible to 'String' , i tried casting it to NSString , but it didn't work.So Anyone knows how to convert [NSObject:AnyObject] to string ?基于之前的答案'(NSObject, AnyObject)' 不能转换为 'String' ,我尝试将它转换为 NSString ,但它没有用。所以有人知道如何将 [NSObject:AnyObject] 转换为字符串吗? Thank You in advance.先感谢您。

Like the comments said, it's a dictionary and you could get the strings by doing this:就像评论说的那样,它是一本字典,您可以通过执行以下操作来获取字符串:

parameters.keys.flatMap { $0 as? String }.forEach {
    finalURL = finalURL.stringByAppendingString($0)
    finalURL = finalURL.stringByAppendingString("=")
    finalURL = finalURL.stringByAppendingString(parameters[$0] as? String ?? "")
    finalURL = finalURL.stringByAppendingString("&")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将&#39;NSObject - &gt;() - &gt; PostFeed&#39;类型的值转换为预期的参数类型&#39;AnyObject?&#39; - Cannot convert value of type 'NSObject -> () -> PostFeed' to expected argument type 'AnyObject?' 无法转换NSMutableDictionary类型的值?预期的参数类型[NSObject:AnyObject]! - Cannot convert value of type NSMutableDictionary? to expected argument type [NSObject: AnyObject]! 无法将类型“ PFUser?”的值转换为类型“ [NSObject:AnyObject] - Cannot convert value of type “PFUser?” to type "[NSObject : AnyObject] 不能下标'[NSObject:AnyObject]类型的值吗?索引类型为'String' - Cannot subscript a value of type '[NSObject : AnyObject]?' with an index of type 'String' 无法用索引类型“字符串”下标“ [NSObject:AnyObject]”类型的值 - Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'String' 无法将“NSMutableDictionary”类型的值转换为强制类型为“iSO Analytics”的类型“[NSObject:AnyObject]” - Cannot convert value of type 'NSMutableDictionary' to type '[NSObject: AnyObject]' in coercion for google ios Analytics 无法为类型为参数列表(字符串:字符串,属性:[NSObject:AnyObject]的NSAttributedString类型)调用初始化程序。 - Cannot invoke initializer for type NSAttributedString with an argument list of type (string: String, attributes: [NSObject: AnyObject]?) 无法下标&#39;[NSObject:AnyObject]类型的值?” 索引类型为&#39;String&#39;的Swift错误 - Cannot subscript a value of type '[NSObject : AnyObject]?' with an index of type 'String' Swift Error “无法下标‘字典’类型的值<nsobject, anyobject> ' 索引类型为 'String' 错误</nsobject,> - "Cannot subscript a value of type 'Dictionary<NSObject, AnyObject>' with an index of type 'String" error 错误无法下标“字典”类型的值 <NSObject, AnyObject> &#39;的索引类型为&#39;String&#39; - Error Cannot subscript a value of type 'Dictionary<NSObject, AnyObject>' with an index of type 'String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM