저는 델리게이트에 NSManageObject를 저장하려고 할 때 많은 문제가 있으며, 객체를 읽는 데 최악의 문제가 있습니다. 내가 할 때 첫 번째보기에서NSManagedObject를 appDelegate에 저장하려고 시도했습니다.
- (void) setCar:(Garage *)Car
{
car = Car;
}
- (void) setTrip:(Trips *)Trip
{
trip = Trip;
}
- (Trips *) gettrip
{
return trip;
}
- (Garage *) getcar
{
return car;
}
:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
trip = (Trips *)[NSEntityDescription insertNewObjectForEntityForName:@"Trips" inManagedObjectContext:context];
[appDelegate setTrip:trip];
가 보인다 설정하고 얻을
@class ViewController;
@class RootViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
Trips *trip;
Garage *car;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property (strong, nonatomic) RootViewController *rootviewcontroller;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, retain) Trips *trip;
@property (nonatomic, retain) Garage *car;
- (NSString *)applicationDocumentsDirectory;
- (void) setCar:(Garage *)Car;
- (void) setTrip:(Trips *)Trip;
- (Trips *) gettrip;
- (Garage *) getcar;
@end
완전한 방법이 같다 : 내 대표는이 같은 것입니다 NSLog (@ "% @", [appDelegate gettrip])를 수행하더라도 OK입니다. , 문제가없는 객체를 보여줍니다. 그러나 다른보기에서 [appDelegate gettrip]을 사용하여 동일한 여행을 읽으려고하면 실제로 작동하지 않습니다. 실제로 NSLog는 객체가 Trip이 아닌 Garage 유형이라고 말합니다.
내가 뭘 잘못하고 있는지 모르겠다. 도움.
'- [NSEntityDescription insetNewObjectForEntityForName : inManagedObjectContext :]'는'id'를 반환하므로 반환하는 객체를 형 변환 할 필요가 없습니다. –
여행 및 자동차 속성에 대해 rigmarole 메서드를 구현하고 멤버 변수를 선언하는 이유는 무엇입니까? 혼란스럽고 문제의 원인이 될 수 있습니다. 객체를 속성에 설정하기 만하면 모든 것이 처리됩니다 :'appDelegate.trip = trip'과'appDelegate.car = car'. – Jeremy
고마워요 제레미 나는 당신이 말한대로했고 더 나아 졌어.하지만 문제는 NSManagedObjectContext와 같다. 왜냐하면 나는 데이터를 저장하는 데 문제가 있고, [컨텍스트 재설정]을 추가하기 때문에 y를 저장할 수 있지만 저장 한 데이터를 잃어 버렸다. 대의원. 나는 [문맥의 재실행]을 시도하지만, 작동하지 않습니다. 또는 변경 사항을 저장하거나 델리게이트에 여행을 저장합니다. 나는 둘 다 할 수 없다. –