cocoa - Can’t seem to avoid force unwrapping in Swift -


update: regarding mac app created document-based application template in xcode, , i’m overriding

override func readfromfilewrapper(filewrapper: nsfilewrapper, oftype typename: string, error outerror: nserrorpointer) -> bool 

i’m trying read in file nsfilewrapper , seems can't away having @ least 1 ! in there.

first, tried

if let rtfdata = files["textfile.rtf"]?.regularfilecontents,         newstring = nsmutableattributedstring(data: rtfdata, options: [nsdocumenttypedocumentattribute : nsrtftextdocumenttype], documentattributes: nil, error: nil) {                 text = newstring                 return true         } 

so error

value of optional type 'nsdata?' not unwrapped; did mean use '!' or '?'?

and have either cast regularfilecontents nsdata! or have force unwrap in nsmutableattributedstring's initializer (data: rtfdata!).


so tried

let rtfdata = files["textfile.rtf"]?.regularfilecontents  if (rtfdata != nil) {     if let newstring = nsmutableattributedstring(data: rtfdata, options: [nsdocumenttypedocumentattribute : nsrtftextdocumenttype], documentattributes: nil, error: nil) {         text = newstring         return true     } } 

which results in

cannot find initializer type 'nsmutableattributedstring' accepts argument list of type '(data: nsdata??, options: [string : string], documentattributes: nil, error: nil)'"

so have change initializer data: rtfdata!!, i'm not sure means.

or, can cast regularfilecontents nsdata?, , can use 1 ! in initializer.

update: here's thing i’ve tried since posting. figured double ? in nsdata?? due me optionally unwrapping nsfilewrapper (files["textfile.rtf"]?) tried this:

if let rtfwrapper = files["textfile.rtf"],     rtfdata = rtfwrapper.regularfilecontents,     newstring = nsmutableattributedstring(data: rtfdata, options: [nsdocumenttypedocumentattribute : nsrtftextdocumenttype], documentattributes: nil, error: nil) {         text = newstring         return true     } 

the compiler still wants me force unwrap nsdata.


i pretty thoroughly confused.

what nsdata??, why “doubly” optional, why result of .regularfilecontents, , how can safely, without !-ing way, nsdata , pass nsmutableattributedstring’s intializer?


another update

i figured maybe files constant source of optional wrapping:

let files = filewrapper.filewrappers 

but compiler won't let me if let it. if try, example,

if let files = filewrapper.filewrappers {     if let rtffile = files["textfile.rtf"] {         if let rtfdata = rtffile.regularfilecontents {             if let newstring = nsmutableattributedstring(data: rtfdata, options: [nsdocumenttypedocumentattribute : nsrtftextdocumenttype], documentattributes: nil, error: nil) {                 text = newstring                 return true             }         }     } } 

the compiler gives error:

bound value in conditional binding must of optional type

about filewrapper.filewrappers

the filewrapper variable parameter of method,

override func readfromfilewrapper(filewrapper: nsfilewrapper, oftype typename: string, error outerror: nserrorpointer) -> bool 

you can use optional binding file first, , proceed there:

if let file = files["textfile.rtf"],     contents = file.regularfilecontents,     newstring = nsmutableattributedstring(data: contents, options: [nsdocumenttypedocumentattribute : nsrtftextdocumenttype], documentattributes: nil, error: nil) {         text = newstring         return true } 

if you're using filewrappers dictionary (a [nsobject : anyobject]), have additional casting. example using file happen have in file wrapper, illustrates idea:

var text: nsstring!  func somemethod(filewrapper: nsfilewrapper) {     let files = filewrapper.filewrappers      if let file = files["test.json"] as? nsfilewrapper,         contents = file.regularfilecontents,         newstring = nsstring(data: contents, encoding: nsutf8stringencoding)  {             text = newstring             println(text)     } } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -