Reputation: 207
While trying to distribute my managedObjectContext to a view controller, I receiver that error: Property 'managedObjectContext' not found on object of type 'UIViewController *' This code in AppDelegate.m is the origin of the error:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *searchController = [[SearchCriteriaViewController alloc] initWithNibName:@"SearchCriteriaViewController" bundle:nil];
UIViewController *managementController = [[WineManagementViewController alloc] initWithNibName:@"WineManagementViewController" bundle:nil];
managementController.managedObjectContext = self.managedObjectContext;
The code in WineManagementViewController looks like this :
@interface WineManagementViewController : UIViewController <NSFetchedResultsControllerDelegate>
{
IBOutlet UIButton *countryButton;
WineStore *store;
}
- (IBAction)newCountry:(id)sender;
@property (strong, nonatomic) UIPopoverController *poCtrl;
@property (strong, nonatomic) WineStore *store;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
And this is the beginning of the implementation:
@implementation WineManagementViewController
@synthesize store, managedObjectContext;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
...
The property can't be found if I want to access it in that way or if I want to access it with the setter method. Does anyone know why this property can't be found?
Upvotes: 1
Views: 70
Reputation: 150665
The thing about using dot syntax is that it does a bit more type checking.
What you've done is declared your new managementController object as a UIViewController
instead of it's actual type WineManagementViewController
. This is perfectly valid under the Liskov Substitution principle.
However - you are using dot syntax to set the property value, and the compiler can't see that the object is actually a WineManagementViewController
object instead of a UIViewController
object, that does not have a managedObjectContext property.
This is where the using method sending syntax would help you. If you leave the declaration as it then you have no problem writing code such as:
UIViewController *managementController = [[WineManagementViewController alloc] initWithNibName:@"WineManagementViewController" bundle:nil];
[managementController setManagedObjectContext:self.managedObjectContext];
Because using method sending syntax doesn't do the same type checking. It will happily send the message to the object at runtime, and if the method is not implemented, then it will throw an exception.
So; what you are doing isn't wrong, it's just the compiler being picky.
Upvotes: 0
Reputation: 16674
Should be:
WineManagementViewController *managementController = [[WineManagementViewController alloc] initWithNibName:@"WineManagementViewController" bundle:nil];
Upvotes: 1