每本书读一次都会有收获,今天再次回顾objective 基础教程,记录你一下里面的基础东西.
#import ""
@class
struct Test {
int i;
float j;
};
typedefstructTest Test;
Test test1;
test1.i =1;
test1.j =1.1;
NSValue *value;
value = [NSValuevalueWithBytes:&test1objCType:@encode(Test)];
Test test2 ;
[value getValue:&test2];
NSLog(@"%d,%f",test2.i,test2.j);
new
alloc
copy
创建的东西,要自己负责回收它.@interface NSString (other)
- (void)myNSStringTest;
@end
@implementation NSString (other)
- (void)myNSStringTest {
NSLog(@"myNSStringTest");
}
@end
@interface Test :NSObject {
// .
// .
// .
}
@end
@interface Test (Private)
- (void)testMethod;
@end
@implementation Test
// .
// .
// .
- (void)testMethod {
//...
}
@end
respondsToSelector方法
该方法询问对象以确定其是否能够响应某个特定的方法
协议:默认必须实现方法添加 @optional 则可以选择实现
@protocol <#protocol name#>
@optional
<#methods#>
@required
<#methods#>
@end
- (id)copyWithZone:(NSZone *)zone {
TestCopy *test;
test = [[[selfclass]allocWithZone:zone]init];
test.name =self.name;
test.add =self.add;
return test;
}
- (id)copyWithZone:(NSZone *)zone {
TestCopyChild *test;
test = [supercopyWithZone:zone];
test.childName =self.childName;
return test;
}
1.属性列表
2.对象编码
3.sqllite数据库
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
//例子
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeInt:self.testIdforKey:@"testId"];
[aCoder encodeObject:self.nameforKey:@"name"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
//如果父类采用了NSCoder协议,则调用[super initwithCoder:aDecoder]
if (self = [superinit]) {
self.testId = [aDecoder decodeIntForKey:@"testId"];
self.name = [aDecoder decodeObjectForKey:@"name"];
}
returnself;
}
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:test1];
test1 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[test1setValue:@"myname"forKey:@"name"];
NSString *name = [test1valueForKey:@"name"];
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"student.name == 'pubo'"];
BOOL isExistence = [predicate evaluateWithObject:testObject];
Test *test1 = [[Testalloc]init];
Test *test2 = [[Test alloc] init];;
NSArray *allTest = [NSArray arrayWithObjects:test1,test2, nil];
NSArray *results;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"price > 150"];
results = [allTestfilteredArrayUsingPredicate:predicate];
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"%K == %@",@"name",@"pubo"];
NSPredicate *predicateTemp = [NSPredicatepredicateWithFormat:@"student.name == $NAME"];
NSDictionary *varDict = [NSDictionarydictionaryWithObjectsAndKeys:@"pubo",@"NAME",nil];
NSPredicate *predicate = [predicateTemp predicateWithSubstitutionVariables:varDict];
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"price > 50 AND price < 200"];
//第一种方式
NSPredicate *predicate1 = [NSPredicatepredicateWithFormat:@"price BETWEEN {50,200}"];
NSArray *betweens = [NSArrayarrayWithObjects:[NSNumbernumberWithInt:50],[NSNumbernumberWithInt:200],nil];
//第二种方式
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"price BETWEEN %@",betweens];
//第三种方式
NSPredicate *predicateTemp = [NSPredicatepredicateWithFormat:@"price BETWEEN $VALUES"];
NSDictionary *varDict = [NSDictionary dictionaryWithObjectsAndKeys:betweens,@"VALUES",nil];
NSPredicate *predicate3 = [predicateTemp predicateWithSubstitutionVariables:varDict];
NSArray *names = [NSArrayarrayWithObjects:@"a1",@"a2",@"a3",nil];
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF IN {'a1','b2','c3'}"];
NSArray *results = [names filteredArrayUsingPredicate:predicate];
BEGINSWITH 检查某个字符是否以...开头
ENDSWITH 检查某个字符是否以...结尾
CONTAINS 检查某个字符是否包含另外一个字符
修饰符[c]代表:不区分大小写
修饰符[d]代表:不区分发音符
修饰符[cd]代表:不区分大小写和发音符
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"student.name CONTAINS[cd] 'pu'"];
8.LIKE运算符
?:问号表示一个字符
*:表示任意个字符
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"student.name LIKE[cd] '*pu??'"];