ios - ViewController does not have a member named managedObjectContext -


i trying use coredata in popoverpresentationviewcontroller keeps saying view controller called popovervc not have member named managedobjectcontext.

i have tried:

1. initializing inside init popoverviewcontroller:

required init(coder adecoder: nscoder) {     super.init(coder: adecoder)      self.appdelegate = (uiapplication.sharedapplication().delegate) as! appdelegate     self.managedobjectcontext = appdelegate.managedobjectcontext! } 

2. passing view controller got calling app delegate

override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject!) {     switch(segue.identifier!) {         case "popoversegue":             let controller = segue.destinationviewcontroller as! popovervc             controller.managedobjectcontext  = self.managedobjectcontext!             break         default:             break      } }    

normally can access in other view controller using:

((uiapplication.sharedapplication().delegate) as! appdelegate).managedobjectcontext 

which heard maybe bad practice, won't work in popover view controller. none of solutions on other stackoverflow questions work either.

edit, below how popoverviewcontroller class looks like, including of make sure isn't stupid syntax mistake somewhere breaking code. declaring managedobjectcontext getter property. still same error.

class popoverviewcontroller: uiviewcontroller, uipopoverpresentationcontrollerdelegate, uipickerviewdelegate {  @iboutlet weak var testinglabel: uilabel! @iboutlet weak var apicker: uipickerview!  required init(coder adecoder: nscoder) {     super.init(coder: adecoder)      //cancel button     navigationitem.rightbarbuttonitem = uibarbuttonitem(barbuttonsystemitem: uibarbuttonsystemitem.cancel, target: self, action: "tapcancel:")      // popover settings     modalpresentationstyle = .popover     popoverpresentationcontroller!.delegate = self      self.preferredcontentsize = cgsize(width:400,height:600) }  //var appdelegate: appdelegate //var managedobjectcontext:nsmanagedobject var appdelegate: appdelegate {     return uiapplication.sharedapplication().delegate as! appdelegate }  var managedobjectcontext: nsmanagedobjectcontext {     return self.appdelegate.managedobjectcontext! }  var pickerdata = ["mozzarella","gorgonzola","provolone","brie","maytag blue","sharp cheddar","monterrey jack","stilton","gouda","goat cheese", "asiago"]  let fetchrequest = nsfetchrequest(entityname: "region")   var fetchresults = managedobjectcontext!.executefetchrequest(fetchrequest, error: nil) [region] //pickerdata = fetchresults  func numberofcomponentsinpickerview(pickerview: uipickerview) -> int{     return 2 }  func pickerview(pickerview: uipickerview, numberofrowsincomponent component: int) -> int{     return pickerdata.count }  func pickerview(pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string!{     //return pickerdata[row] }  @ibinspectable var popoveroniphone:bool = false @ibinspectable var popoveroniphonelandscape:bool = true  func adaptivepresentationstyleforpresentationcontroller(pc: uipresentationcontroller) -> uimodalpresentationstyle{      //this methods called system when screen has compact width      //return .none means still want popover when adaptive on iphone     //return .fullscreen means we'll modal presetaion on iphone      switch(popoveroniphone, popoveroniphonelandscape){     case (true, _): // popover on iphone         return .none      case (_, true): // popover on landscape on iphone         let size = pc.presentingviewcontroller.view.frame.size         if(size.width>320.0){ //landscape             return .none         }else{             return .fullscreen         }      default: // no popover on iphone         return .fullscreen     }   }  func presentationcontroller(_: uipresentationcontroller, viewcontrollerforadaptivepresentationstyle _: uimodalpresentationstyle)     -> uiviewcontroller?{         return uinavigationcontroller(rootviewcontroller: self) }  func tapcancel(_ : uibarbuttonitem) {     //tap cancel     dismissviewcontrolleranimated(true, completion:nil); } 

}

you have declared managedobjectcontext read-only computed property, cannot assign value it.

to use assignment have in prepareforsegue need make straight-forward optional property -

var managedobjectcontext: nsmanagedobjectcontext? 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -