objective C 基础教程

每本书读一次都会有收获,今天再次回顾objective 基础教程,记录你一下里面的基础东西.




#import ""

@class

一般使用一个自己创建的类,都会用来 #import ""引入该类;
如果类A和类B相互引用,则需要 @class来创建一个向前引用,否则会出现编译错误;
如果是类B要继承类A,则需要使用 #import ""引入该类(因为他需要了解超类的信息).


NSNumberNSValue的子类
用法:将一个int型,struct型.......数据类型,封装成对象的形式.
如果是自定义的struct型的类型,需要用来 NSValue进行封装.

    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);




内存管理
以前刚接触的时候,他们说很重要,要处理好(我是从JAVA过来的,所以这东西比较陌生).
现在回顾,其实简单的理解就是自己通过

new

alloc

copy

创建的东西,要自己负责回收它.
表面说起简单,实际要解决得非常好,还需要经验的.(嘿嘿)
在新发布的SDK5中,就可以选择自动由系统处理内存了(我想应该会方便很多)



类别:
1.不能添加新的实例变量
2.名称冲突,类别具有更高的优先级

@interface NSString (other)

- (void)myNSStringTest;

@end


@implementation NSString (other)

- (void)myNSStringTest {

    

   NSLog(@"myNSStringTest");

}

@end


在某些时候,自己需要建立一些私有方法,
而不想被.h文件给暴露出去,如果不写的话(写在引用方法的后面),就可能出现编译警告
就可以通过类别在.m的实现前面加一些私有的方法

@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





对象的复制:
实现   NSCopying 协议,再实现- (id)copyWithZone:(NSZone *)zone;方法

- (id)copyWithZone:(NSZone *)zone {

    TestCopy *test;

    test = [[[selfclass]allocWithZone:zone]init];

    test.name =self.name;

    test.add =self.add;

    return test;

}


如果父类已经实现过   NSCopying 协议
则子类无须再声明,直接重写 - (id)copyWithZone:(NSZone *)zone;方法
然后直接调用 [super copyWithZone:zone];
再写上自己要复制的信息

- (id)copyWithZone:(NSZone *)zone {

   TestCopyChild *test;

    test = [supercopyWithZone:zone];

    test.childName =self.childName;

    return test;

}




文件的保存:
1.属性列表
2.对象编码
3.sqllite数据库

对象的编码:
实现  NSCoding 协议;
实现两方法:

- (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;

}

用   NSKeyedArchiver  将对象归档成  NSData
用  NSKeyedUnarchiver  来解析 NSData

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:test1];

test1 = [NSKeyedUnarchiver unarchiveObjectWithData:data];




键/值编码 KVC
应用:假如一个类中,某属性没有set和get方法,就可以通过键/值编码的方式来访问,修改等等一系列的应用.

[test1setValue:@"myname"forKey:@"name"];

NSString *name = [test1valueForKey:@"name"];




  NSPredicate 类(谓词)
用于指定过滤器的条件.对每个对象通过谓词进行筛选.
不能使用 $VARIABLE作为键路径.因为它只表示值

1.

    NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"student.name == 'pubo'"];

    BOOL isExistence = [predicate evaluateWithObject:testObject];


判断 testObject对象的属性为 student对象的name属性是否为pubo.

2.

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];


查询 allTest数组中所有对象的price属性大于150的对象,然后返回给数组results

3.
%k指定键路径

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"%K == %@",@"name",@"pubo"];


4.
将变量放入字符串中

    NSPredicate *predicateTemp = [NSPredicatepredicateWithFormat:@"student.name == $NAME"];

   NSDictionary *varDict = [NSDictionarydictionaryWithObjectsAndKeys:@"pubo",@"NAME",nil];

    NSPredicate *predicate = [predicateTemp predicateWithSubstitutionVariables:varDict];


5.
运算符

    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];



6.  SELF 的运用

    NSArray *names = [NSArrayarrayWithObjects:@"a1",@"a2",@"a3",nil];

   NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF IN {'a1','b2','c3'}"];

    NSArray *results = [names filteredArrayUsingPredicate:predicate];


7.字符串运算符 

BEGINSWITH 检查某个字符是否以...开头

ENDSWITH   检查某个字符是否以...结尾

CONTAINS   检查某个字符是否包含另外一个字符


修饰符[c]代表:不区分大小写

修饰符[d]代表:不区分发音符

修饰符[cd]代表:不区分大小写和发音符

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"student.name CONTAINS[cd] 'pu'"];


8.LIKE运算符

?:问号表示一个字符

*:表示任意个字符

NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"student.name LIKE[cd] '*pu??'"];

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值