【问题标题】:issues with Searching in Core Data在核心数据中搜索的问题
【发布时间】:2015-02-26 19:35:21
【问题描述】:

我正在使用这段代码来查找数据的存在。但是我收到了 Use of Undeclared Identifier "appDelegate" 这个错误。

- (IBAction)findContact:(id)sender {
CoreDataAppDelegate *appDelegate =
   [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context =
   [appDelegate managedObjectContext];

NSEntityDescription *entityDesc =
   [NSEntityDescription entityForName:@"Contacts"
   inManagedObjectContext:context];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];

NSPredicate *pred =
   [NSPredicate predicateWithFormat:@"(name = %@)",
    _name.text];
[request setPredicate:pred];
NSManagedObject *matches = nil;

NSError *error;
NSArray *objects = [context executeFetchRequest:request
    error:&error];

if ([objects count] == 0) {
   _status.text = @"No matches";
} else {
   matches = objects[0];
   _address.text = [matches valueForKey:@"address"];
   _phone.text = [matches valueForKey:@"phone"];
   _status.text = [NSString stringWithFormat:
       @"%lu matches found", (unsigned long)[objects count]];
}
}

我已包含 #import <CoreData/CoreData.h> 。我是否必须包含其他文件。当我保存数据时,它工作正常。

【问题讨论】:

  • 导入CoreDataAppDelegate文件
  • 在哪里可以找到 CoreDataAppDelegate 文件@Arunakumari
  • 你的应用委托文件@rahul
  • 哦......愚蠢的错误...... :( @Arunakumari
  • @Arunakumari 如何从实体中删除一些文本,就好像我想将搜索替换为删除一样\

标签: ios xcode core-data xcode6


【解决方案1】:

确保您已导入应用委托标头

#import "CoreDataAppDelegate.h"

尝试将 AppDelegate 转换为 CoreDataAppDeleage

CoreDataAppDelegate *appDelegate = (CoreDataAppDelegate*)[[UIApplication sharedApplication] delegate];

【讨论】:

  • 如何从实体中删除一些文本,就好像我想将搜索替换为删除\ – @Adam
【解决方案2】:
CoreDataAppDelegate *appDelegate =
   [[UIApplication sharedApplication] delegate];

将上面的行替换为下面的行

     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

【讨论】:

    【解决方案3】:

    用于删除基于 Id 的文本

    #pragma mark - delete tracking details
    
    - (void)deleteTrackingId:(NSString *)trackingId
    
        {
            appDelegate = [[UIApplication sharedApplication] delegate];
            managedObjectContext = appDelegate.managedObjectContext;
            NSError *error;
    
            NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
            NSEntityDescription *entityOne = [NSEntityDescription entityForName:@"TrackingList" inManagedObjectContext:managedObjectContext];
    
            [fetchRequest setEntity:entityOne];
            NSPredicate *predicate=[NSPredicate predicateWithFormat:@"trackingId == %@",trackingId];
    
            [fetchRequest setPredicate:predicate];
            NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
            if (fetchedObjects == nil) {
                NSLog(@"Could not delete Entity Objects");
            }
    
            for (TrackingList *trackingObject in fetchedObjects) {
                [managedObjectContext deleteObject:trackingObject];
            }
    
            [appDelegate saveContext];
        }
    

    【讨论】:

    • 假设我有一行三列名称年龄工资。如果我在跟踪 ID 中传递名称,它将删除整行还是仅删除名称 @Arunakumari
    • 如果 tracking id 为 1 表示相关信息已删除。tracking id 是 primarykey.so 基于主键我删除了这些细节。
    • 我们可以在聊天室讨论吗chat.stackoverflow.com/rooms/71170/ios-magicians@Arunakumari
    • 或者你想删除基于工资列的详细信息提供该列而不是 trackingId
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 2014-05-06
    • 1970-01-01
    相关资源
    最近更新 更多