“NSManagedObjectContext”没有可见的@interface声明

时间:2015-02-16 08:57:06

标签: ios iphone core-data

enter image description here

[self.managedObjectContext deletedObjects:lastPoint]; 这一行显示错误

'NSManagedObjectContext'没有可见的@interface声明选择器'deletedObjects'。

这是我的代码 任何人都可以解决这个问题吗?

Appdelegate.h

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;


@end

Appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    /* crete the fetch request first*/
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Rectangle"];
    NSError *requestError = nil;
    /*And execute the fetch request on the context*/
    NSArray *rectangle = [self.managedObjectContext executeFetchRequest:fetchRequest error:&requestError];

    /*make sure we get the array*/

    if ([rectangle count] > 0) {
        /*delete the last person in the array*/
        Rectangle *lastPoint = [rectangle lastObject];
        [self.managedObjectContext deletedObjects:lastPoint];

        if ([lastPoint isDeleted]) {
            NSLog(@"Successfully deleted the last point...");

            NSError *savingError = nil;
            if ([self.managedObjectContext save:&savingError]) {
                NSLog(@"successfully saved the context");
            } else {
                NSLog(@"Failed to save the context");
            }
        } else {
            NSLog(@"Failed to delete the last point");
        }
    } else {
        NSLog(@"Could not find any rectangle entities in the context.");
    }
    return  YES;
}

2 个答案:

答案 0 :(得分:2)

错误消息

  

'NSManagedObjectContext'没有可见的@interface声明了   选择器'deletedObjects'。

告诉您,类NSManagedObjectContext未实现方法deletedObjects。您可以在API documentation

中查看此内容

您可以使用deleteObject:删除单个对象。所以将代码更改为:

[self.managedObjectContext deleteObject:lastPoint];

答案 1 :(得分:0)

由于the documentation表示deletedObjects是只读属性,因此它只有getter方法而没有任何参数 所以你应该只使用下一个

来访问它
self.managedObjectContext.deletedObjects