如何匹配CoreSet中NSSet的对象值? [英] How To Match object value of NSSet in CoreData?

查看:138
本文介绍了如何匹配CoreSet中NSSet的对象值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经取得一个核心数据一个到多个关系。
一个人可以有多个银行帐户。

I Have Made One Core Data ONE TO MANY Relationships. A person can have multiple Bank’s Accounts.

@synthesize setContainer,textField;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.setContainer = [[NSMutableSet alloc]init];
    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSEntityDescription *entityPerson = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:appDelegate.managedObjectContext];
    NSManagedObject *newPerson = [[NSManagedObject alloc]initWithEntity:entityPerson insertIntoManagedObjectContext:appDelegate.managedObjectContext];
    [newPerson setValue:@"Steve" forKey:@"name"];
    [newPerson setValue:@"UK" forKey:@"address"];
    [newPerson setValue:@123456 forKey:@"mobile_no"];

    NSEntityDescription *entityAccount = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:appDelegate.managedObjectContext];
    NSManagedObject *newaccount = [[NSManagedObject alloc]initWithEntity:entityAccount insertIntoManagedObjectContext:appDelegate.managedObjectContext];
    [newaccount setValue:@98765432100 forKey:@"acc_no"];
    [newaccount setValue:@"Saving" forKey:@"acc_type"];
    [newaccount setValue:@1000000 forKey:@"balance"];

    // Add Address to Person
   setContainer = [newPerson mutableSetValueForKey:@"person"];
    [self.setContainer addObject:newaccount];
    NSLog(@"%@",self.setContainer);  // Save Managed Object Context
    NSError *error = nil;
    if (![newPerson.managedObjectContext save:&error]) {
        NSLog(@"Unable to save managed object context.");
        NSLog(@"%@, %@", error, error.localizedDescription);
    }



我的问题是



当我在文本字段上键入我的手机号码并按下检查您的余额时,我的余额应该打印在日志中,我已输入核心数据值。

My Question is

When I type My mobile Number on Text Field and press button "Check Your Balance" ,My Balance should be Print In Log which , I have entered in Core Data value.

推荐答案

可以使用NSPredicate根据所选的过滤器获取实体,例如电话号码

You can use NSPredicate to fetch the entity based on a selected filter, on your case the Phone number

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"mobile_no = %@",phoneNumber];

NSFetchRequest *fetchRequest = [NSFetchRequest new];
[fetchRequest setPredicate:predicate];
NSError *error;
NSArray *result = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

基于此,如果用户有多个银行帐户, (假设他在所有银行帐户上使用相同的电话号码)

Based on this, you can perform the computation of amount if the user has multiple bank account since you already have all the details (assuming that he uses the same phone number on all his bank accounts)

这是有关 NSPredicate

还可以找到一个很好的备忘单此处

A good cheat sheet as well can be found here

这篇关于如何匹配CoreSet中NSSet的对象值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆