gpt4 book ai didi

ios - 如何从 objective-c 中模型数组内的模型数组中搜索单词

转载 作者:行者123 更新时间:2023-11-29 01:13:41 31 4
gpt4 key购买 nike

从另一个模型数组内的模型数组中搜索单词的最佳和最简单的方法是什么。例如,我有一个包含学生模型类的数组。所以学生有不止一位老师,这是在教师示范类的阵列中。当我搜索老师姓名时,我需要获取拥有该老师的学生列表。我知道如何使用模型类数组中的谓词进行搜索。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",@"sarah"];
searchArray = [NSMutableArray arrayWithArray:[studentArray filteredArrayUsingPredicate:predicate]];

我试过了,但没用。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.teachers.teacherName contains[c] %@",@"sarah"];

学生模型类将如下所示。

@property (nonatomic, assign) NSString *name;
@property (nonatomic, strong) NSString *address;
@property (strong, nonatomic) NSArray *teachers;

我知道如何通过分割数组来做到这一点并使用 for 循环来获取它。谁能帮我找到一种最简单的方法。谢谢。

最佳答案

好吧,可以说,正如您所提到的,您的模型类具有以下属性,例如在 Student.h 文件

 #import <Foundation/Foundation.h>

@interface Student : NSObject
@property (nonatomic, strong) NSString *name; //i am changing it to strong
@property (nonatomic, strong) NSString *address;
@property (strong, nonatomic) NSArray *teachers;
- (id)initWithName:(NSString *)name trachers:(NSArray *)teachers address:(NSString *)address;
@end

以及Student.m 文件

#import "Student.h"
@implementation Student

//custom initialiser
- (id)initWithName:(NSString *)name trachers:(NSArray *)teachers address:(NSString *)address
{
if(self = [super init])
{
self.name = name;
self.teachers = teachers;
self.address = address;
}
return self;
}
@end

ViewController.m 文件中,我正在创建 Student 的实例并将其添加到数组中,例如,

ViewController.h文件中,

#import <UIKit/UIKit.h>
#import "Student.h"
@property (nonatomic, strong) NSMutableArray *students; //to hold student instances
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;//to search the teachers
@property (weak, nonatomic) IBOutlet UITextView *textView;//to display the output
@end

ViewController.m 文件中,

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
_students = [[NSMutableArray alloc] init];
for(int k = 0 ; k < 5 ; k++)
{
NSMutableArray *teachers = [self getTeachersForStudent:k];//a helper method to fill teacher's name
Student *stud = [[Student alloc] initWithName:[NSString stringWithFormat:@"student_%d",k] trachers:teachers address:[NSString stringWithFormat:@"address_%d",k]]; //hear student name will be like "student_0"
[_students addObject:stud]; //finally add it to students array
}
}

//this is the helper method, for testing u can add different same name if you want
- (NSMutableArray *)getTeachersForStudent:(int)k
{
NSMutableArray *array = [[NSMutableArray alloc] init];
switch (k)
{
case 0:
[array addObjectsFromArray:@[@"anoop",@"shan",@"sara",@"peter",@"david",@"gopi"]];
break;
case 1:
[array addObjectsFromArray:@[@"hema",@"isah",@"sara",@"john"]];
break;
case 2:
[array addObjectsFromArray:@[@"venu",@"giri",@"kim",@"madu",@"shan"]];
break;
case 3:
[array addObjectsFromArray:@[@"venu",@"tom",@"yesh",@"vijay",@"shan"]];
break;
case 4:
[array addObjectsFromArray:@[@"vim",@"janu",@"jai",@"kiran",@"shan"]];
break;
default:
break;
}
return array;
}

最后在 ViewController.m 文件中的排序函数添加下面的方法,它将从搜索栏中获取初始字符串并搜索所有教师姓名,最后给出将显示在中的学生姓名 TextView

 - (void)refreshStudentList
{
__block NSMutableArray *result = [[NSMutableArray alloc] init];
[_students enumerateObjectsUsingBlock:^(Student* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
__block Student *presentStudent = obj;
__block NSArray *array = [presentStudent valueForKey:@"teachers"];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if([obj rangeOfString:self.searchBar.text options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[result addObject:presentStudent];
}
}];
}];
//at this point result array contains all the student student name of the teacher that u are searched
NSString *resultString = @"";
for(Student *s in result)
{
resultString = [NSString stringWithFormat:@"%@ \n %@", resultString, s.name];
}
_textView.text = resultString;
}

在相同的 ViewController.m 中添加搜索栏委托(delegate)方法并调用 refreshStudentList 方法

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self refreshStudentList];
if (searchText.length == 0)
_textView.text = @""; //simply resetting the text view
}

最终输出如下

enter image description here

关于ios - 如何从 objective-c 中模型数组内的模型数组中搜索单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35479096/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com