现在苹果已轻易摆脱NSString
和String
自动兼容性,我在两者之间有点噩梦。我从词典中获取了几个NSString
,我无法将它们转换为常规String
...
我试过了:
let fileNameString: String = String(format: "%@", filename!)
let fileNameString: String = (filename as! String)
let fileNameString = filename as? String
let fileNameString = (filename as? String) ?? ""
if let fileNameString = filename as? String {
println("\(fileNameString)")
}
但都会产生错误。
我已经在转换点被打破,并且看不到NSStrings都没有:
但两者都没有喜悦。获得Thread 1: EXC_BAD_ACCESS (code=1, address=0x20)
。我错过了一些明显的东西吗?
即使只是在转换前尝试打印NSString filename
也会导致同样的错误..
在转化之前发布代码尝试查看是否与其有关...
// First we create a head request as the info I need is in the headers
var newRequest: NSMutableURLRequest = NSMutableURLRequest(URL: request.URL!)
newRequest.HTTPMethod = "HEAD"
var response: NSURLResponse?
NSURLConnection.sendSynchronousRequest(newRequest, returningResponse: &response, error: nil)
// Unwrap response as httpResponse in order to access allHeaderFields
if let httpResponse = response as? NSHTTPURLResponse {
let headerString = "sfn-Document-Filename"
let headerNSString = headerString as NSString
let filetypeString = "Content-Type"
let filetypeNSString = filetypeString as NSString
// This is a dictionary where the keys are NSCFStrings
// (NSStrings, hence creating the NSStrings above)
var allHeaders = httpResponse.allHeaderFields
// Getting the filename out here only works with as? NSString. as? String creates the same error as converting.
let filename = allHeaders[headerNSString] as? NSString
// This is a string which contains the type as 'application/pdf' for example. We only need the part after the /.
// Again, trying to get this out as a String fails
let typeString = allHeaders[filetypeNSString] as? NSString
var typeArray = typeString?.componentsSeparatedByString("/") as! [NSString]
let filetype = typeArray[1]
}
答案 0 :(得分:2)
如果这是NSString
,那么您需要做的就是filename as String
(无!
)。但听起来问题是filename
,可选类型NSString?
,nil
。 (按住Option键并单击filename
以确认其类型)
如果有合理的默认值(例如,空字符串),请尝试
let fileNameString = (filename as? String) ?? ""
或者如果您需要使用特定代码处理nil
:
if let fileNameString = filename as? String {
// use fileNameString, which will be unwrapped and of type String
}
else {
// log error or similar
}
或者,如果你想推迟展开,但想要改变可能值内的类型,你可以做到
let fileNameString = filename as? String
// which is a less long-winded way of saying
let fileNameString = filename.map { $0 as String }
一般来说,您应该尝试减少!
用法,因为它会导致这样的问题。 !
仅适用于您从代码中知道值绝对不能为nil
的那些时间。
编辑:根据您的示例代码,尝试以下操作:
let url = NSURL(string: "http://www.google.com")
let request = url.map { NSMutableURLRequest(URL: $0) }
request?.HTTPMethod = "HEAD"
let response: NSHTTPURLResponse? = request.flatMap {
var response: NSURLResponse?
NSURLConnection.sendSynchronousRequest($0, returningResponse: &response, error: nil)
return response as? NSHTTPURLResponse
}
let headers = response?.allHeaderFields as? [String:String]
// google.com has no such header but still...
let filename = headers?["sfn-Document-Filename"]
// bear in mind someArray[1] will also crash if there's no such entry,
// first/last are better if that's what you want
let type = headers?["Content-Type"]?
.componentsSeparatedByString(";").first?
.componentsSeparatedByString("/").last
这里的所有内容都是可选的,但是很安全,因此您可以在各个点测试nil以进行日志记录/错误报告。