KVC键值编码

// 通过键值编码方式@private,@ptorected修饰的属性也能访问;
// 按照关键字查找,如果没有,会自动加上下划线'_';
// 子类通过键值编码也拥有了父类的私有属性;
- (void)setValue:(nullable id)value forKey:(NSString *)key;

谓词

//创建谓词
+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat;
//谓词条件
//1.使 逻辑运算符 && ||
s = [NSString stringWithFormat:@"self.age>%d && self.name='小白'",30]; 
predicate = [NSPredicate predicateWithFormat:s];
//2.IN (包含)
s = [NSString stringWithFormat:@"name IN {'小白  ','xx','%@'}",@"rose"];
predicate = [NSPredicate predicateWithFormat:s];
//3....开头
s = [NSString stringWithFormat:@"name BEGINSWITH 'r'"]; 
predicate = [NSPredicate predicateWithFormat:s];
//4....结尾         @"name ENDSWITH 'e'" 
//5. 包含 ... 字符  @"name CONTAINS 's'"
//6. like*:任意多个字符 ?:表示任意单个字符
/*
name like *a    以a结尾
name like *a*   字符中包含a的字符 
name like ?a*   第2个字符为a的字符串
*/
//[c]不区分大小写
s = [NSString stringWithFormat:@"self like [c] '%@', ];
predicate = [NSPredicate predicateWithFormat:s];

单例设计模式

设计原理

设计原理:始终返回一个实例,即一个类始终只有一个实例

实现

#import "Husband.h"
@implementation Husband

// 定义一个静态的Husband实例
static Husband *instance = nil;

+(Husband *)defaultHusband {
    // 同步,为了多线程下访问安全,保证任何时候只有一个丈夫被初始化
    @synchronized(self) {
        // 确保只被初始化一次,如果实例已经被创建,那就不再alloc init;
        if(instance == nil){
            instance = [[self alloc] init];
        }
    }
    return instance;
}

// 覆写父类的方法,保证Husband调用alloc时只申请一次内存
// alloc方法会调用allocWithZone
// zone是管理内存碎片化的工具,使内存达到最优使用率
+(instancetype)allocWithZone:(struct _NSZone *)zone {
    @synchronized(self) {
        if (instance == nil) {
            instance = [super allocWithZone:zone];
        }
    }
    return instance;
//  return nil;
}
// 保证copy产生的对象就是自己
-(id)copy{
    return self;
}
@end
posted on 2016-05-01 18:10  Apel  阅读(86)  评论(0编辑  收藏  举报