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

时间:2022-07-11 15:21:26

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 {" . 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 ? Thank You in advance.

我在“for key:String in parameters {”行中得到一个错误(在标题中提到)。基于前面的答案'(NSObject,AnyObject)'不能转换为'String',我试着将它转换为NSString,但它没有工作。所以任何人都知道如何将[NSObject:AnyObject]转换为字符串?先谢谢你。

1 个解决方案

#1


1  

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("&")
}

#1


1  

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("&")
}