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:
在尝试将我的managedObjectContext分发到视图控制器时,我收到了错误:在'UIViewController *'类型的对象上找不到属性'managedObjectContext'这个代码在AppDelegate.m中是错误的起源:
- (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 :
WineManagementViewController中的代码如下所示:
@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?
如果我想以这种方式访问它或者我想使用setter方法访问它,则找不到该属性。有谁知道为什么找不到这个属性?
2 个解决方案
#1
1
Should be:
应该:
WineManagementViewController *managementController = [[WineManagementViewController alloc] initWithNibName:@"WineManagementViewController" bundle:nil];
#2
0
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.
您所做的是将新的managementController对象声明为UIViewController,而不是它的实际类型WineManagementViewController。根据Liskov Substitution原则,这完全有效。
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.
但是 - 您使用点语法来设置属性值,并且编译器无法看到该对象实际上是WineManagementViewController对象而不是没有managedObjectContext属性的UIViewController对象。
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.
所以;你正在做的事情没有错,只是编译器挑剔。
#1
1
Should be:
应该:
WineManagementViewController *managementController = [[WineManagementViewController alloc] initWithNibName:@"WineManagementViewController" bundle:nil];
#2
0
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.
您所做的是将新的managementController对象声明为UIViewController,而不是它的实际类型WineManagementViewController。根据Liskov Substitution原则,这完全有效。
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.
但是 - 您使用点语法来设置属性值,并且编译器无法看到该对象实际上是WineManagementViewController对象而不是没有managedObjectContext属性的UIViewController对象。
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.
所以;你正在做的事情没有错,只是编译器挑剔。