【问题标题】:No visible @interface for 'NSManagedObjectContext' declares'NSManagedObjectContext' 没有可见的@interface 声明
【发布时间】:2015-02-16 08:57:06
【问题描述】:

[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;
}

【问题讨论】:

  • 问题解决了吗?
  • 你能解决这个问题吗? @Bhargav?
  • 是的,我通过修改 Appdelegate.m [self.managedObjectContext deleteObject:lastPoint]; 中的行来解决这个问题
  • @Bhargav 请在后面解释。我面临同样的错误stackoverflow.com/questions/33365748/…

标签: ios iphone core-data


【解决方案1】:

错误信息

“NSManagedObjectContext”没有可见的@interface 声明 选择器“已删除对象”。

告诉你NSManagedObjectContext 类没有实现deletedObjects 方法。您可以在API documentation 中查看。

您可以使用deleteObject: 删除单个对象。所以把你的代码改成:

[self.managedObjectContext deleteObject:lastPoint];

【讨论】:

  • 你能出示你的财产声明managedObjectContext吗?也许这实际上不是NSManagedObjectContext 类型的对象(尽管我认为这不太可能)
  • #import &lt;UIKit/UIKit.h&gt; #import &lt;CoreData/CoreData.h&gt; @interface AppDelegate : UIResponder &lt;UIApplicationDelegate&gt; @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
  • 对不起,我不能在这里做对齐
  • 我在我的问题中添加了它
  • 嗯,通常最好更新问题而不是在 cmets 中发布代码 :) 无论如何,看起来您的财产 managedObjectContext 确实是 NSManagedObjectContext 类型.
【解决方案2】:

正如the documentation 所说的deletedObjects 是只读属性,所以它只有getter 方法,没有任何参数 所以你应该使用 next 来访问它

self.managedObjectContext.deletedObjects

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2021-09-22
    相关资源
    最近更新 更多