ios - Swift 2.0 Migration errors -
i've watched wwdc sessions, reading new programmers book on swift, , reading related questions on stack overflow find. fixed errors in app after migrating swift 1.2 swift 2.0.
however there's still few i've not managed solve.
downcasting anyobject
error:
cannot downcast '[anyobject]' more optional type '[nsmanagedobject]'
code:
let fetchrequest = nsfetchrequest(entityname: formulaentity) var error: nserror? { let fetchedresults = try managedcontext.executefetchrequest(fetchrequest) as! [nsmanagedobject]? if let results = fetchedresults { stocks = results } else { print("could not fetch \(error), \(error!.userinfo)") } } catch { print("error: \(error)") }
the error shown happening in let fetchedresults = try...
line
another strange error i'm having in appdelegate:
error:
'nsmutabledictionary' not convertible '[nsobject : anyobject]'
code:
lazy var persistentstorecoordinator: nspersistentstorecoordinator? = { // persistent store coordinator application. implementation creates , return coordinator, having added store application it. property optional since there legitimate error conditions cause creation of store fail. // create coordinator , store var coordinator: nspersistentstorecoordinator? = nspersistentstorecoordinator(managedobjectmodel: self.managedobjectmodel) let url = self.applicationdocumentsdirectory.urlbyappendingpathcomponent("stocks.sqlite") var error: nserror? = nil var failurereason = "there error creating or loading application's saved data." { try coordinator!.addpersistentstorewithtype(nssqlitestoretype, configuration: nil, url: url, options: nil) } catch var error1 nserror { error = error1 coordinator = nil // report error got. let dict = nsmutabledictionary() dict[nslocalizeddescriptionkey] = "failed initialize application's saved data" dict[nslocalizedfailurereasonerrorkey] = failurereason dict[nsunderlyingerrorkey] = error error = nserror(domain: "your_error_domain", code: 9999, userinfo: dict [nsobject : anyobject]) // replace code handle error appropriately. // abort() causes application generate crash log , terminate. should not use function in shipping application, although may useful during development. nslog("unresolved error \(error), \(error!.userinfo)") abort() } catch { fatalerror() } return coordinator }()
i have not ever touched code above. have no idea why wasn't migrated properly, apple's migration tool.
another error in appdelegate:
binary operator '&&' cannot applied 2 bool operands
call can throw, not marked 'try' , error not handled.
code:
func savecontext () { if let moc = self.managedobjectcontext { var error: nserror? = nil if moc.haschanges && !moc.save() { // replace implementation code handle error appropriately. // abort() causes application generate crash log , terminate. should not use function in shipping application, although may useful during development. nslog("unresolved error \(error), \(error!.userinfo)") abort() } } }
again havn't touched part of appdelegate, , not sure wrong code above.
cannot downcast '[anyobject]' more optional type '[nsmanagedobject]'
in swift 1.2, executefetchrequest(:_)
returned [anyobject]?
. in swift 2, returns [anyobject]
because new try
… syntax returns non-optional.
(in case method return nil
, method not return @ all, , control move catch
block.)
'nsmutabledictionary' not convertible '[nsobject : anyobject]'
this means you're trying insert nsmutabledictionary
can't converted objective-c object. in case, think it's because error
struct conforming errortype
, rather nserror
object. try adding error1
instead.
call can throw, not marked 'try' , error not handled.
save()
might throw error needs executed try
, instead of being evaluated bool
. martin r. points out in comments, answer this question provides complete solution won't rehash here.
Comments
Post a Comment