gpt4 book ai didi

iOS:如何分隔数组中的特定字符串元素?

转载 作者:行者123 更新时间:2023-11-29 00:33:39 25 4
gpt4 key购买 nike

服务器响应如下:

[{"bp":"000/000","dateTime":"05/12/2016 01:02:59 PM","doc":{"email_id":"batra@gmail.com","exception":0,"gender":"Male","id":0,"mobile_no":8055621745,"name":"Batra","profile_id":0,"qualification":"MD(Doctor)","reg_id":157,"salutation":"Mr","wellness_id":"251215782521"},"follow_up":"17","id":37,"medicine":["Syrup,Decold Total,20,0-0-1,Before Meal,1","Injection,Insulin,1,0-0-1,Before Meal,1","no","no","no","no","no","no","no","no"],"patient":{"email_id":"bishtrohit1989@gmail.com","exception":0,"gender":"Male","id":0,"mobile_no":8055621745,"name":"Rohit","profile_id":0,"qualification":"","reg_id":150,"salutation":"Mr","wellness_id":"290119935030"},"weight":"000"}]

据此,我将 medicine 数组分开,如下所示:

NSMutableArray *Myarray = [NSMutableArray new];
for (int i=0; i<_menuItems.count; i++) {
[Myarray addObject:[[_menuItems objectAtIndex:i] objectForKey:@"medicine"]];
NSLog(@"medicine: %@",Myarray);

我得到的输出如下:

medicine: (
(
"Syrup,Decold Total,20,0-0-1,Before Meal,1",
"Injection,Insulin,1,0-0-1,Before Meal,1",
no,
no,
no,
no,
no,
no,
no,
no
)
)

现在我想要的是:

1) 删除所有no元素。

2) 之后,我只需要每个字符串中的第二个元素。

简而言之,我希望我的最终输出是这样的:

[冷总量,胰岛素];

但我不知道该怎么做......?

请任何人都可以解决我的问题。帮助将是可观的。

最佳答案

您需要在 Myarray 上使用 NSPredicate 并对其进行过滤。

像这样制作你的Myarray。NSMutableArray *Myarray = [NSMutableArray new];

for (int i=0; i<_menuItems.count; i++) {
[Myarray addObjectsFromArray:[[_menuItems objectAtIndex:i] objectForKey:@"medicine"]];
}

1) 删除所有 no 元素。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF = %@)",@"no"];
NSArray *filterArray = [Myarray filteredArrayUsingPredicate:predicate];

2) 每个字符串只需要第二个元素

NSMutableArray *medicineArray = [[NSMutableArray alloc] init];
for (NSString* medicine in filterArray) {
NSArray *arr = [medicine componentsSeparatedByString:@","];
if (arr.count >= 2) {
[medicineArray addObject:[arr objectAtIndex:1]];
}
}

关于iOS:如何分隔数组中的特定字符串元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41161601/

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