ios - Can't get values out of allHeaderFields dictionary -


for reason i'm not able right values key in dictionary need in swift. works in objective c..

var newrequest: nsmutableurlrequest = nsmutableurlrequest(url: navigationaction.request.url!) newrequest.httpmethod = "head" var response: nsurlresponse? var filename: anyobject? var filetype: anyobject? nsurlconnection.sendsynchronousrequest(newrequest, returningresponse: &response, error: nil) if let httpresponse = response as? nshttpurlresponse {     let allheaders = httpresponse.allheaderfields     filename = allheaders["sfn-document-filename"]     let typestring: anyobject? = allheaders["content-type"]     let typearray: array = typestring!.componentsseparatedbystring("/")     filetype = typearray[1] } 

this dictionary object looks allheaders [nsobject : anyobject] 6 key/value pairs.

screenshot of dictionary keys/values

here's objective c still works.

nsmutableurlrequest *newrequest = [nsmutableurlrequest requestwithurl:[request url]]; [newrequest sethttpmethod:@"head"]; nshttpurlresponse *response; [nsurlconnection sendsynchronousrequest:newrequest returningresponse:&response error: null]; if ([response respondstoselector:@selector(allheaderfields)]) {     nsdictionary *dictionary = [response allheaderfields];     filename = [[response allheaderfields] objectforkey:@"sfn-document-filename"];     nsstring *typestring = [dictionary objectforkey:@"content-type"];     nsarray *typearray = [typestring componentsseparatedbystring:@"/"];     filetype = [typearray objectatindex:1]; } 

it doesn't need done same way obj-c, it's worked , seemed place start.

the value i'm getting filename filename = (anyobject?) some

i've tried changing to:

var filename: string? filename = allheaders["sfn-document-filename"] as? string 

but nil string , thread 1: exc_bad_access (code=1, address=0x20)

the reason happening because value of key in dictionary appears nsstring , can't find string (i thought supposed interchangeable though?).

either way. doing worked:

var newrequest: nsmutableurlrequest = nsmutableurlrequest(url: request.url!) newrequest.httpmethod = "head" var response: nsurlresponse? nsurlconnection.sendsynchronousrequest(newrequest, returningresponse: &response, error: nil) var filename: nsstring? var filetype: string? if let httpresponse = response as? nshttpurlresponse {     let headerstring = "sfn-document-filename"     let headernsstring: nsstring = (headerstring nsstring)     let filetypestring = "content-type"     let filetypensstring: nsstring = (filetypestring nsstring)      var allheaders = httpresponse.allheaderfields      filename = allheaders[headernsstring] as? nsstring      let typestring = allheaders[filetypensstring] as? nsstring     let typearray = typestring!.componentsseparatedbystring("/")     filetype = typearray[1] } 

now need figure out why componentsseparatedbystring isn't working...


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -