码迷,mamicode.com
首页 > 其他好文 > 详细

视图封状逻辑代码及谓词查找的运用

时间:2016-04-21 01:26:41      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

1:封装一些优化代码

a实例:多处公用视图的相同逻辑代码处理

实例要求它的职责包括:

  1. 通过传入的 URL,加载并展示头像图片
  2. 显示一些附属信息,比如大V的标志
  3. 将用户点击头像的事件传递给外层的 View Controller 跳转到用户信息页面
@interface FDAvatarView : UIView
// 假设 VIPInfo 是某个 Entity
- (void)configureWithAvatarURL:(NSURL *)URL VIPInfo:(id)info tapped:(void (^)(void))block;
@end

再写一个扩展类来处理事件的代码:

@interface FDAvatarView (FDAvatarViewSelfManager)
- (void)selfManagedConfigureWithAvatarURL:(NSURL *)URL VIPInfo:(id)info uid:(NSString *)uid;
@end

@implementation FDAvatarView (FDAvatarViewSelfManager)
// 为后一个页面的创建增加了个 UID 参数
- (void)selfManagedConfigureWithAvatarURL:(NSURL *)URL VIPInfo:(id)info UID:(NSString *)UID {
    [self configureWithAvatarURL:URL VIPInfo:info tapped:^{
        // 假设 App 结构是 Root -> TabBar -> Navigation -> ViewController
        UITabBarController *tabBarControler = (id)[UIApplication.sharedApplication.delegate.window.rootViewController;
        UINavigationController *navigationController = tabBarControler.selectedViewController;
        // 创建用户信息 View Controller
        FDUserProfileViewController *profileViewController = [FDUserProfileViewController viewControllerWithUID:UID];
        [navigationController pushViewController:profileViewController animated:YES];
    }];
}
@end
注意:如果觉得这样的耦合方式不妥的话,也可以封装个全局方法去取到当前顶层的 Navigation Controller。

b实例:点赞的按钮功能上有几个职责:
显示已有的点赞数
点击按钮后执行一个小动画,点赞数 +1,同时发送网络请求。
若已经点赞,点击执行反向操作
若网络请求发送失败,则回退成点击前的状态

@interface FDLikeButton : UIButton
- (void)configureLikeStatus:(BOOL)likeOrNot count:(NSInteger)count animated:(BOOL)animated;
@end
扩展类:
@interface FDLikeButton (FDLikeButtonSelfManager)
- (void)selfManagedConfigureWithLikeStatus:(BOOL)likeOrNot count:(NSInteger)count;
@end

@implementation FDLikeButton (FDLikeButtonSelfManager)
- (void)selfManagedConfigureWithLikeStatus:(BOOL)likeOrNot count:(NSInteger)count {
    [self configureLikeStatus:likeOrNot count:count animated:NO];
    [self addTarget:self action:@selector(likeButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)likeButtonTapped:(id)sender {
    // +1 or -1 with animation
    // Network request ^(NSError *error) {
    //     if (error) {
    //         rollback
    //     }
    // }
}
@end

 2:谓词运用

OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。在Java中是没有这种技术的,但是有开源的框架已经实现了此功能。

下面来看一下具体的例子吧:

Person.h文件

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property NSString *name;
@property NSInteger age;

+ (id)personWithName:(NSString *)name andAge:(NSInteger)age;

@end



Person.m 文件

#import "Person.h"

@implementation Person

+ (id)personWithName:(NSString *)name andAge:(NSInteger)age{
    Person *person = [[Person alloc] init];
    person.name = name;
    person.age = age;
    return person;
}

- (NSString *)description{
    NSString *s =[NSString stringWithFormat:@"name=%@,age=%ld",_name,_age];
    return s;
}

@end

测试方法

#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‘"]; 
    }
    return 0;
}

首先我们看到,我们初始化了一定大小的数组。

然后我们就可以使用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‘"];

 

 

视图封状逻辑代码及谓词查找的运用

标签:

原文地址:http://www.cnblogs.com/wujy/p/5375570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!