谓词判断数组中是否存在某值ios bool数组 IOS

C#中怎么判断一个数组中是否存在某个数组值_百度知道
C#中怎么判断一个数组中是否存在某个数组值
提问者采纳
今天发现一个怪问题,同样是.net3.5环境下的两个项目,一个里支持arr.contains(1),一个就不支持,代码完全相同也不行.有时在不支持项目里加新文件支持,但有时又不支持,发现在支持的项目里contains的注释是&extension方法,但是查不出在哪定义的.
在网上查到一段代码,虽然和这不太一样,但是可以使数组使用contains
数组有Contains方法,在.NET Framework 2.0 版中,Array 类实现ICollection&T接口,但必须如下书写代码:1.
string[] ss =newstring[] {1,2};2.
boolb = (ssasICollection&string
).Contains(1);
MSDN中说明如下:
在.NET Framework 2.0 版中,哗处糕肺蕹镀革僧宫吉Array 类实现 System.Collections.Generic.IList&T、System.Collections.Generic.ICollection&T 和 System.Collections.Generic.IEnumerable&T 泛型接口。由于实现是在运行时提供给数组的,因而对于文档生成工具不可见。因此,泛型接口不会出现在 Array 类的声明语法中,也不会有关于只能通过将数组强制转换为泛型接口类型(显式接口实现)才可访问的接口成员的参考主题。将某一数组强制转换为这三种接口之一时需要注意的关键一点是,添加、插入或移除元素的成员会引发
NotSupportedException。
下例的第二种方法是上文里讲到的三种方法之一.
(1) 第一种方法:
int[] ia = {1,2,3};
int id = Array.IndexOf(ia,1);
// 这里的1就是你要查找的值if(id==-1)// 不存在else// 存在(2) 第二种方法:
string[] strArr = {a,b,c,d,e};
bool exists = ((IList)strArr).Contains(a);if(exists)// 存在else// 不存在注意:用IList需要
其他类似问题
2人觉得有用
为您推荐:
您可能关注的推广
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁4286人阅读
在前一篇文章中我们介绍了OC中一个重要技术通知:,今天我们在来看一下OC中给我们提供的一个技术:谓词(NSPredicate)OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。在Java中是没有这种技术的,但是有开源的框架已经实现了此功能。下面来看一下具体的例子吧:Person.h//
46_NSPredicate
Created by jiangwei on 14-10-18.
Copyright (c) 2014年 jiangwei. All rights reserved.
#import &Foundation/Foundation.h&
@interface Person : NSObject
@property NSString *
@property NSI
+ (id)personWithName:(NSString *)name andAge:(NSInteger)
Person.m//
46_NSPredicate
Created by jiangwei on 14-10-18.
Copyright (c) 2014年 jiangwei. All rights reserved.
#import &Person.h&
@implementation Person
+ (id)personWithName:(NSString *)name andAge:(NSInteger)age{
Person *person = [[Person alloc] init];
person.name =
person.age =
- (NSString *)description{
NSString *s =[NSString stringWithFormat:@&name=%@,age=%ld&,_name,_age];
我们在Person类中定义属性,还有一个产生对象的类方法,同时重写了description方法,用于打印结果测试方法main.m//
46_NSPredicate
Created by jiangwei on 14-10-18.
Copyright (c) 2014年 jiangwei. All rights reserved.
#import &Foundation/Foundation.h&
#import &Person.h&
//谓词,指定过滤器的条件,将符合条件的对象保留下来
//一般用谓词过滤数组中指定的元素
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *persons = [NSArray arrayWithObjects:
[Person personWithName:@&mac& andAge:20],
[Person personWithName:@&1& andAge:30],
[Person personWithName:@&2& andAge:40],
[Person personWithName:@&3& andAge:50],
[Person personWithName:@&4& andAge:60],
[Person personWithName:@&5& andAge:70],
[Person personWithName:@&6& andAge:20],
[Person personWithName:@&7& andAge:40],
[Person personWithName:@&8& andAge:60],
[Person personWithName:@&9& andAge:40],
[Person personWithName:@&0& andAge:80],
[Person personWithName:@&10& andAge:90],
[Person personWithName:@&1& andAge:20]];
//年龄小于30
//定义谓词对象,谓词对象中包含了过滤条件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@&age&%d&,30];
//使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@&filterArray=%@&,array);
//查询name=1的并且age大于40
predicate = [NSPredicate predicateWithFormat:@&name='1' && age&40&];
array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@&filterArray=%@&,array);
//in(包含)
predicate = [NSPredicate predicateWithFormat:@&self.name IN {'1','2','4'} || self.age IN{30,40}&];
//name以a开头的
predicate = [NSPredicate predicateWithFormat:@&name BEGINSWITH 'a'&];
//name以ba结尾的
predicate = [NSPredicate predicateWithFormat:@&name ENDSWITH 'ba'&];
//name中包含字符a的
predicate = [NSPredicate predicateWithFormat:@&name CONTAINS 'a'&];
//like 匹配任意多个字符
//name中只要有s字符就满足条件
predicate = [NSPredicate predicateWithFormat:@&name like '*s*'&];
//?代表一个字符,下面的查询条件是:name中第二个字符是s的
predicate = [NSPredicate predicateWithFormat:@&name like '?s'&];
}首先我们看到,我们初始化了一定大小的数组。然后我们就可以使用NSPredicate类进行过滤操作了1、查询数组中年龄小于30的对象//年龄小于30
//定义谓词对象,谓词对象中包含了过滤条件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@&age&%d&,30];
//使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@&filterArray=%@&,array);首先创立一个过滤条件://定义谓词对象,谓词对象中包含了过滤条件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@&age&%d&,30];这里面操作很简单的:@&age&%d&,这个age是Person的属性名,%d相当于占位符,然后后面用参数替换即可然后进行过滤操作,返回一个过滤之后的数组对象//使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
NSArray *array = [persons filteredArrayUsingPredicate:predicate];2、查询name=1并且age大于40的集合//查询name=1的并且age大于40
predicate = [NSPredicate predicateWithFormat:@&name='1' && age&40&];
array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@&filterArray=%@&,array);当然我们也可以使用&&进行多条件过滤3、包含语句的使用//in(包含)
predicate = [NSPredicate predicateWithFormat:@&self.name IN {'1','2','4'} || self.age IN{30,40}&];4、指定字符开头和指定字符结尾,是否包含指定字符//name以a开头的
predicate = [NSPredicate predicateWithFormat:@&name BEGINSWITH 'a'&];
//name以ba结尾的
predicate = [NSPredicate predicateWithFormat:@&name ENDSWITH 'ba'&];
//name中包含字符a的
predicate = [NSPredicate predicateWithFormat:@&name CONTAINS 'a'&];5、like进行匹配多个字符//like 匹配任意多个字符
//name中只要有s字符就满足条件
predicate = [NSPredicate predicateWithFormat:@&name like '*s*'&];
//?代表一个字符,下面的查询条件是:name中第二个字符是s的
predicate = [NSPredicate predicateWithFormat:@&name like '?s'&];总结这一篇就介绍了OC中常用的技术:谓词的使用,他用起来很方便的,而且也没什么难度,和我们当初在操作数据库的时候很想,但是他对我们进行过滤操作提供了很大的便捷。
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
已经上线了,在各大市场都可以搜索到。希望大家多多支持!!
扫一扫关注
实时推送技术干货的精品文章
访问:935634次
积分:11510
积分:11510
排名:第572名
原创:190篇
评论:658条
文章:35篇
阅读:87387
文章:14篇
阅读:80460
文章:34篇
阅读:91091
文章:93篇
阅读:373154
(1)(2)(4)(1)(1)(1)(30)(9)(4)(7)(12)(3)(12)(13)(17)(21)(9)(33)(35)[ios]通过使用谓词筛选字典数组
我有字典下面的数组:
Printing description of newOrderbookBids:
&__NSArrayI 0x&(
price = "10.14";
size = 148;
price = "10.134";
price = "10.131";
size = 321;
数组中的每个词典有钥匙 price 和 size ,两者都是数字。
我想要返回筛选的数组,其中包含唯一的字典, size & 0 。在此示例中,这将是数组与字典 #1 和 #3,但无词典 #2:
price = "10.14";
size = 148;
price = "10.131";
size = 321;
我用过以下的代码片段,若要筛选我 NSArray *newOrderbookBids 大小:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"size & 0"];
newOrderbookBids = [newOrderbookBids filteredArrayUsingPredicate:predicate];
不幸的是我的代码崩溃,运行时显示错误
[NSSymbolicExpression compare:]: unrecognized selector sent to instance xxxx
是什么错误我的代码和谓词?如何可以通过筛选 size & 0 ?
解决方法 1:
"大小"是在中的保留的关键字和关键字是不区分大小写。
为关键路径,为了解决这一问题,作为 var arg 代替使用"%k"格式参数:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K & 0", @"size"];PHP判断数组中是否存在某一数值的函数in_array_百度知道
PHP判断数组中是否存在某一数值的函数in_array
提问者采纳
was found\h'p'', 1;Got mac&?&gt, &#39.13 found with strict check\;);); found with strict check&#92:bool in_array ( mixed needle,如果找到则返回 TRUE;;n&12;Linux&}if (in_array(&quot. in_array() 中用数组作为 needle&}; was found&#92. in_array() 例子&lt.13,否则返回 FALSE,因为 in_array() 是区分大小写的: Got Irix 例子2, $os)) {
echo &quot:1;mac&。 如果第三个参数 strict 的值为 TRUE 则 in_array() 函数还会检查 needle 的类型是否和 haystack 中的相同;}if (in_array(1, &p'};。 例子1; 上例将输出.10'), array('n&, bool strict] )在haystack 中搜索;)?php
$os = array(&quot?php
$a = array(&#39, &#39, '12;o&#39.13 found with strict check 例子3;r&#39.4'&#39.13), true)) {
echo &quot, $a;, &quot.4'Mac&NT&h'1. in_array() 严格类型检查例子&lt, 'Irix&p&#39, true)) {
echo &n&, $os)) {
echo &Got Irix&quot, &ph&#39, 12;}if (in_array(array('1;;
if (in_array(&#39函数;);n&&#39, $a;;;
if (in_array(&;o', $a)) {
echo &;?&gt, ' 第二个条件失败;);i&#39, array haystack [;;}, $a)) {
echo &quot.4?php
$a = array(array(';
if (in_array(array(&#39,所以以上程序显示为:in_array -- 检查数组中是否存在某个值定义;;f'Irix&quot
其他类似问题
为您推荐:
array的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁C++关于数组的三个问题1.判断一个数是否在数组里2.插入一个数进入数组的给定位置3.删掉数组中的某一个数_百度知道
C++关于数组的三个问题1.判断一个数是否在数组里2.插入一个数进入数组的给定位置3.删掉数组中的某一个数
体题目如下1. Read the students’ scores from keyboard until input -1. Define an array (length is 20) to store a s (2) InsertScore function inserts a given score into the array at a given position. Note that the number of input scores is smaller than 20. Write the follo (3) DeleteScore function deletes a given score from the array. The main function validates the three functions. (1) FindScore function finds whether a given score is in the array
提问者采纳
/输入查询的数;&
cout&30;n;
cin&注意是从后面往前面推;&quot下面实现查询; i&/&&
&m;&lt,不然就错了 } num[k] = m、删除功能;&lt,也可参考#include&&/&赋值; for (i=0;&/ } &#47:&quot:&&最后的元素赋值0
} } for (i=0;&
isExist = i--) {
num[i] = num[i-1];&n& i++) {
num[i]=num[i+1];&&30,0这里可以赋予空元素的意义;i&&在数组中& } /2;30,而非从前面往后面推,省的使用时出现无效值;x;&输入插入的位置(k最大为29);
cout& /&lt.指定位置插入数据& } return 0;
cin&gt、增加;&iostream&&
cout&k;&删除
} if (;& i& /&&endl,占个位置;n&&
/不在数组中& bool isExist =k;num[i];// int num[30];/ for (i=0;
for(int i=29;输入插入的数; /将元素值向前移
if (i==29)
num[i]=0;&&&&quot.要查找的数字判断是否存在1;
cin& i&int main(){ int i=0;&lt。 {
num[i] = 0; i&30:& for(i=x!isExist) {
cout&i++) {
cout&lt,如果与你的不符合;输入删除的位置(最大为29);&&/&lt:& i++) {
if (num[i] == n)
int FindScore(int x){ int A[i]; for(i=1;i&=i++) { if(A[i]==x) return 1;} return 0;}这是我做的第一个函数可以运行 但是返回值总是1
&int FindScore(int x)&&& { &&int A[i];&&& //错误,1、数组应该从参数中传入&for(i=1;i&=i++)& //数组下标从0开始&{ &&if(A[i]==x)&&&return 1;&}&return 0;}&2、定义数组时有两种情况,静态或者动态,静态数组大小必须是常量,如int A[30];动态创建数组,数组大小也要确认,比如变量cin&&y; int *p = new int[y];&你这里可以这样改:参数是数组int FindScore(int A[],int x)&&& { &&for(i=0;i&=i++)&&&//max == 29,防止越界&{ &&if(A[i]==x)&&&&return 1;&}&return 0;}&2.参数是指向数组的指针,int FindScore(int *A,int x)& //p指向int数组{&&&&&& for (i=0; i&=i++)&&&&&& //max == 数组元素总数-1,因为数组下标是从0开始的这个明白吧& & {&&&&&&&&&if(A[i]==x)&&&&&&&&&&&&&&&return 1;&&&&&&&& return 0;&&&& &}}&试试看
提问者评价
谢谢你了~以后有问题还可以问你么?
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 ios 判断数组是否包含 的文章

 

随机推荐