OC-簡單的介紹謂詞和使用方法

 /*NSPredicate 指定條件過濾數據*/
    
    
    Person *person1 = [[Person alloc] initPerson:@"張三" age:12];
    Person *person2 = [[Person alloc] initPerson:@"李四" age:21];
    Person *person3 = [[Person alloc] initPerson:@"王五" age:33];
    Person *person4 = [[Person alloc] initPerson:@"趙六" age:15];
    
    NSArray *storage = [NSArray arrayWithObjects:person1,person2,person3,person4, nil];
    
    //賽選滿足謂詞的條件返回是數組 滿足年齡大於15
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"age>=%d",15]]);
    //滿足姓名
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name=%@",@"張三"]]);
    //或預算符
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name=%@ or age=%d",@"張三",15]]);
    //in運算符
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.name in {'張三','李四','趙六'}"]]);
    //某個字符開頭
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.name beginswith '張'"]]);
    //某個字符結尾
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.name endswith '三'"]]);
    //包含字符
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.name contains '三'"]]);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章