日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NSString拼接字符串和NSPredicate详解

發布時間:2023/12/13 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NSString拼接字符串和NSPredicate详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
NSString* string;?// 結果字符串
02 NSString* string1, string2;?//已存在的字符串,需要將string1和string2連接起來
03 ?
04 //方法1.
05 string = [[NSString alloc]initWithFormat:@"%@,%@", string1, string2 ];
06 ?
07 //方法2.
08 string = [string1 stringByAppendingString:string2];
09 ?
10 //方法3 .
11 string = [string stringByAppendingFormat:@"%@,%@",string1, string2];

經常用的是第二種方法。

NSPredicate類:主要用來指定過濾器的條件,該對象可以準確的描述所需條件,對每個對象通過謂詞進行篩選,判斷是否與條件相匹配。謂詞是指在計算機中表示計算真假值的函數。原理和用法都類似于SQL查詢中的where,作用相當于數據庫的過濾取。主要用于從集合中分揀出符合條件的對象,也可以用于字符串的正則匹配


定義(最常用到的方法):

[plain]?view plaincopy
  • NSPredicate?*ca?=?[NSPredicate?predicateWithFormat:(NSString?*),?...];??
  • 例子1:

    (1)對NSArray進行過濾?

    [plain]?view plaincopy
  • NSArray?*array?=?[[NSArray?alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan",?nil];????
  • NSString?*string?=?@"ang";????
  • NSPredicate?*pred?=?[NSPredicate?predicateWithFormat:@"SELF?CONTAINS?%@",string];????
  • NSLog(@"%@",[array?filteredArrayUsingPredicate:pred]);????
  • 例子2:

    [plain]?view plaincopy
  • #import?<Foundation/Foundation.h>??
  • @interface?Person:?NSObject{??
  • ????int?pid;??
  • ????NSString?*name;??
  • ????float?height;??
  • }??
  • -(void)?setPid:?(int)?pid;??
  • -(void)?setName:?(NSString*)?name;??
  • -(void)?setHeight:?(float)?height;??
  • -(int)?pid;??
  • -(NSString*)?name;??
  • -(float)?height;??
  • @end??

  • [plain]?view plaincopy
  • #import?"Person.h"??
  • ??
  • @implementation?Person??
  • -(void)?setPid:?(int)?p{??
  • ????pid=p;??
  • }??
  • -(void)?setName:?(NSString*)?n{??
  • ????[n?retain];??
  • ????[name?release];??
  • ????name=n;??
  • }??
  • -(void)?setHeight:?(float)?h{??
  • ????height=h;??
  • }??
  • -(int)?pid{??
  • ????return?pid;??
  • }??
  • -(NSString*)?name{??
  • ????return?name;??
  • }??
  • -(float)?height{??
  • ????return?height;??
  • }??
  • -(void)?dealloc{??
  • ????[name?release];??
  • ????[super?dealloc];??
  • }??
  • @end??

  • [plain]?view plaincopy
  • int?main(int?argc,?char?*argv[])??
  • {??
  • ????NSAutoreleasePool?*pool?=?[[NSAutoreleasePool?alloc]?init];??
  • ????//實例化三個Person,并放入數組。??
  • ????NSMutableArray?*array=[NSMutableArray?arrayWithCapacity:?5];??
  • ????Person?*person1=[[Person?alloc]?init];??
  • ????[person1?setPid:?1];??
  • ????[person1?setName:?@"Name1"];??
  • ????[person1?setHeight:?168];??
  • ????[array?addObject:?person1];??
  • ????Person?*person2=[[Person?alloc]?init];??
  • ????[person2?setPid:?2];??
  • ????[person2?setName:?@"Name2"];??
  • ????[person2?setHeight:?178];??
  • ????[array?addObject:?person2];??
  • ????Person?*person3=[[Person?alloc]?init];??
  • ????[person3?setPid:?3];??
  • ????[person3?setName:?@"Name3"];??
  • ????[person3?setHeight:?188];??
  • ????[array?addObject:?person3];??
  • ????//創建謂詞,條件是pid>1?并且height<188.0。其實謂詞也是基于KVC?的,也就是如果pid?在person?的成員變量xxx?中,那么此處要寫成xxx.pid>1。??
  • ????NSPredicate?*pre?=?[NSPredicate?predicateWithFormat:??
  • ????????????????????????@"?pid>1?and?height<188.0"];??
  • ????int?i=0;??
  • ????for(;i<[array?count];i++){??
  • ????????Person?*person=[array?objectAtIndex:?i];??
  • ????????//遍歷數組,輸出符合謂詞條件的Person?的name。??
  • ????????if?([pre?evaluateWithObject:?person])?{??
  • ????????????NSLog(@"%@",[person?name]);??
  • ????????}??
  • ????}??
  • ????[person1?release];??
  • ????[person2?release];??
  • ????[person3?release];??
  • ????[pool?release];??
  • ????return?0;??
  • }??

  • 我們看到創建謂詞使用類方法predicateWithFormat: (NSString*) format,

    format 里的東西真的和SQL 的where 條件差不多。

    另外,參數format 與NSLog 的格式化模版差不多,如果1 和188.0 是傳遞過來的參數,你可以寫成如下的形式:

    @"pid>%d and height<%f",1,188.0
    (1)比較運算符>,<,==
    可用于數值及字符串
    例:@"number > 100"
    (1.) 邏輯運算符:AND、OR、NOT
    這幾個運算符計算并、或、非的結果。
    (2.) 范圍運算符:BETWEEN、IN
    例:
    @”pid BETWEEN {1,5}”
    @"name IN {'Name1','Name2'}"
    (3.) 占位符:
    NSPredicate *preTemplate = [NSPredicate predicateWithFormat:@"name==$NAME"];
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:
    @"Name1", @"NAME",nil];
    NSPredicate *pre=[preTemplate predicateWithSubstitutionVariables: dic];
    占位符就是字段對象里的key,因此你可以有多個占位符,只要key 不一樣就可以了。
    (4.) 快速篩選數組:
    前面我們都是使用謂詞逐個判斷數組內的對象是否符合,其實數組本身有更為便捷的方法,
    直接篩選出一個符合謂詞的新數組。
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"pid>1"];
    NSMutableArray *arrayPre=[array filteredArrayUsingPredicate: pre];
    NSLog(@"%@",[[arrayPre objectAtIndex: 0] name]);
    (5.) 字符串運算符:
    BEGINSWITH、ENDSWITH、CONTAINS 分別表示是否以某字符串開頭、結尾、包含。
    他們可以與c、d 連用,表示是否忽略大小寫、是否忽略重音字母(字母上方有聲調標號)。
    例:
    @”name BEGINSWITH[cd] ‘He’”
    判斷name 是否以He 開頭,并且忽略大小寫、忽略重音字母。
    (6.) LIKE 運算符:
    LIKE 使用?表示一個字符,*表示多個字符,也可以與c、d 連用。
    例:
    @”name LIKE ‘???er*’” 與Paper Plane 相匹配。
    (7.) SELF:
    前面的數組中放的都是對象,如果數組放的都是字符串(或者是其他沒有屬性的類型),該
    怎么寫謂詞呢?這里我們使用SELF。
    例:
    NSArray *arrays=[NSArray arrayWithObjects: @"Apple", @"Google", @"MircoSoft", nil];
    NSPredicate *pre2 = [NSPredicate predicateWithFormat:@"SELF=='Apple'"];
    (8.) 正則表達式:
    NSPredicate 使用MATCHES 匹配正則表達式,正則表達式的寫法采用international components
    for Unicode (ICU)的正則語法。
    例:
    NSString *regex = @"^A.+e$";//以A 開頭,以e 結尾的字符。
    NSPredicate *pre= [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if([pre evaluateWithObject: @"Apple"]){
    printf("YES\n");
    }else{
    printf("NO\n");
    }


    例子3:

    一般來說這種情況還是蠻多的,比如你從文件中讀入了一個array1,然后想把程序中的一個array2中符合array1中內容的元素過濾出來。

    正 常傻瓜一點就是兩個for循環,一個一個進行比較,這樣效率不高,而且代碼也不好看。

    其實一個循環或者無需循環就可以搞定了,那就需要用搞 NSPredicate這個類了~膜拜此類~

    1)例子一,一個循環

    NSArray *arrayFilter = [NSArray arrayWithObjects:@"pict", @"blackrain", @"ip", nil];

    NSArray *arrayContents = [NSArray arrayWithObjects:@"I am a picture.", @"I am a guy", @"I am gagaga", @"ipad", @"iphone", nil];

    我想過濾arrayContents的話只要循環 arrayFilter就好了

    int i = 0, count = [arrayFilter count];

    for(i = 0; i < count; i ++)

    {

    NSString *arrayItem = (NSString *)[arrayFilter objectAtIndex:i];

    NSPredicate *filterPredicate = [[NSPredicate predicateWithFormat:@"SELF CONTAINS %@", arrayItem];

    NSLog(@"Filtered array with filter %@, %@", arrayItem, [arrayContents filteredArrayUsingPredicate:filterPredicate]);

    }

    當然以上代碼中arrayContent最好用mutable 的,這樣就可以直接filter了,NSArray是不可修改的。

    2)例子二,無需循環

    NSArray *arrayFilter = [NSArray arrayWithObjects:@"abc1", @"abc2", nil];

    NSArray *arrayContent = [NSArray arrayWithObjects:@"a1", @"abc1", @"abc4", @"abc2", nil];

    NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrayFilter];

    [arrayContent filterUsingPredicate:thePredicate];


    這樣arrayContent過濾出來的就是不包含 arrayFilter中的所有item了。


    3)生成文件路徑下文件集合列表

    NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *defaultPath = [[NSBundle mainBundle] resourcePath]; NSError *error; NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:defaultPath error:&error]


    4)match的用法

    ?? ?1. 簡單比較

    NSString *match = @"imagexyz-999.png"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@", match]; NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate]; 2. match里like的用法(類似Sql中的用法) NSString *match = @"imagexyz*.png"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match]; NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];3. 大小寫比較 [c]表示忽略大小寫,[d]表示忽略重音,可以在一起使用,如下: NSString *match = @"imagexyz*.png"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] %@", match]; NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];4.使用正則 NSString *match = @"imagexyz-\\d{3}\\.png"; //imagexyz-123.png NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match]; NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];

    總結:

    1) 當使用聚合類的操作符時是可以不需要循環的

    2)當使用單個比較類的操作符時可以一個循環來搞定

    PS,例子 一中嘗試使用[@"SELF CONTAINS %@", arrayFilter] 來過濾會掛調,因為CONTAINS時字符串比較操作符,不是集合操作符。







    NSPredicate源代碼:

    [plain]?view plaincopy
  • #import?<Foundation/NSObject.h>??
  • #import?<Foundation/NSArray.h>??
  • #import?<Foundation/NSSet.h>??
  • ??
  • NS_CLASS_AVAILABLE(10_4,?3_0)??
  • @interface?NSPredicate?:?NSObject?<NSCoding,?NSCopying>?{??
  • ????void?*_reserved;??
  • }??
  • +?(NSPredicate?*)predicateWithFormat:(NSString?*)predicateFormat?argumentArray:(NSArray?*)arguments;??
  • +?(NSPredicate?*)predicateWithFormat:(NSString?*)predicateFormat,?...;??
  • +?(NSPredicate?*)predicateWithFormat:(NSString?*)predicateFormat?arguments:(va_list)argList;??
  • ??
  • +?(NSPredicate?*)predicateWithValue:(BOOL)value;?????
  • ??
  • #if?NS_BLOCKS_AVAILABLE??
  • +?(NSPredicate*)predicateWithBlock:(BOOL?(^)(id?evaluatedObject,?NSDictionary?*bindings))block?NS_AVAILABLE(10_6,?4_0);???
  • #endif??
  • ??
  • -?(NSString?*)predicateFormat;??????
  • ??
  • -?(NSPredicate?*)predicateWithSubstitutionVariables:(NSDictionary?*)variables;??????
  • ??
  • -?(BOOL)evaluateWithObject:(id)object;??????
  • ??
  • -?(BOOL)evaluateWithObject:(id)object?substitutionVariables:(NSDictionary?*)bindings?NS_AVAILABLE(10_5,?3_0);???
  • ??
  • @end??
  • ??
  • @interface?NSArray?(NSPredicateSupport)??
  • -?(NSArray?*)filteredArrayUsingPredicate:(NSPredicate?*)predicate;??????
  • @end??
  • ??
  • @interface?NSMutableArray?(NSPredicateSupport)??
  • -?(void)filterUsingPredicate:(NSPredicate?*)predicate;??????
  • @end??
  • ??
  • ??
  • @interface?NSSet?(NSPredicateSupport)??
  • -?(NSSet?*)filteredSetUsingPredicate:(NSPredicate?*)predicate?NS_AVAILABLE(10_5,?3_0);??????
  • @end??
  • ??
  • @interface?NSMutableSet?(NSPredicateSupport)??
  • -?(void)filterUsingPredicate:(NSPredicate?*)predicate?NS_AVAILABLE(10_5,?3_0);??????
  • @end??

  • 總結

    以上是生活随笔為你收集整理的NSString拼接字符串和NSPredicate详解的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。