使用核心数据在窗口应用程序中不清楚使用@property [英] unclear use of @property in window app using core data
问题描述
浏览一个用于实验的基于Windows的应用程序ive作为了解核心数据的一种方式,ive注意到该appdelegate具有以下代码
Looking through a Window based application ive used to experiment with as a way of getting my head around core data, ive noticed that the appdelegate has the following code
myAppDelegate .h
myAppDelegate.h
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface myAppDelegate : NSObject <UIApplicationDelegate> {
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
UIWindow *window;
UITabBarController *tabBarController;
}
myAppDelegate.m
myAppDelegate.m
#import "myAppDelegate.h"
@interface myAppDelegate (PrivateCoreDataStack)
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@end
@implementation myAppDelegate
@synthesize window, tabBarController;
// code ....
//
- (NSManagedObjectContext *) managedObjectContext {
}
- (NSManagedObjectModel *)managedObjectModel {
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
}
// etc.....
我想了解一些有关该项目正在做什么的事情
i want to understand a few things about what this project is doing
- 为什么类别声明中的属性? (如果我删除了privateCoreDataStack类别,那么一切仍然有效...)
- 为什么属性似乎与实现中的方法链接在一起……managedObjectContext {}等
- 为什么.h文件中的成员与属性和方法同名
- 为什么代码补全让我使用点'。'来访问成员,但是然后无法编译,说无法找到吸气剂
- why the properties in the category declaration? (if i delete the privateCoreDataStack category everything still works...)
- why the properties appear to be linked with the methods in the implementation ... managedObjectContext {} etc
- why the members in the .h file have the same name as the properties and the methods
- why code completion lets me use dot '.' to access the members but then fails on compilation say it cant find a getter
谢谢!
推荐答案
这些是Objective-C的基本概念,但这是一个简短的总结:
These are basic concepts in Objective-C, but here's a quick rundown:
实例成员是通常被认为是私有的,您应该永远不要通过访问器访问其他对象的实例成员。因此,对
Instance members are generally considered to be private, and you should kind of never access the instance members of other objects except through accessors. Hence the need for the
属性声明的需求与方法定义类似; @property(…,只读)NSManagedObjectModel * managedObjectModel;
基本上与-(NSManagedObjectModel *)managedObjectModel;
,除了它启用点标记的事实。
property declarations are like method definitions; @property (…, readonly) NSManagedObjectModel *managedObjectModel;
is basically the same as - (NSManagedObjectModel *)managedObjectModel;
, except for the fact that it enables dot-notation.
类别用于从目录隐藏这些访问器。类的用户(无论出于何种原因);因此名称为 PrivateCoreDataStack
。类别是点符号起作用的原因;
The category is used to hide these accessors from the users of the class (for whatever reason); hence the name PrivateCoreDataStack
. The category is the reason dot-notation works; if you remove it, it won't.
关于名称;方法名称和实例成员名称位于单独的命名空间中;意味着它们可以具有相同的名称;这非常方便,因为它告诉您-(id)somePropery;
访问 id someProperty;
;或 @属性(非原子,保留)NSManagedObjectContext * managedObjectContext;
访问 NSManagedObjectContext * managedObjectContext
。
As for the names; method names and instance members name live in separate namespaces; meaning that they can have the same name; this is very handy, because it tells you that - (id)somePropery;
accesses id someProperty;
; or that @property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
accesses NSManagedObjectContext *managedObjectContext
.
这篇关于使用核心数据在窗口应用程序中不清楚使用@property的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!