Chinaunix首页 | 论坛 | 博客
  • 博客访问: 50582
  • 博文数量: 34
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 297
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-09 10:52
文章分类

全部博文(34)

文章存档

2015年(23)

2014年(11)

我的朋友

分类: iOS平台

2015-04-15 23:18:12

一.NSObjec和反射

反射:根据字符串来实例化一个对象
看先代码:


点击(此处)折叠或打开

  1. //
  2. // Person.h
  3. // NSobject
  4. //
  5. // Created by 金海洋 on 15-4-15.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>

  9. @interface Person : NSObject

  10. -(void)test;

  11. @end


点击(此处)折叠或打开

  1. //
  2. // Person.m
  3. // NSobject
  4. //
  5. // Created by 金海洋 on 15-4-15.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import "Person.h"

  9. @implementation Person

  10. -(void)test{


  11.     NSLog(@"test");

  12. }


  13. @end


点击(此处)折叠或打开

  1. //
  2. // Student.h
  3. // NSobject
  4. //
  5. // Created by 金海洋 on 15-4-15.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import "Person.h"

  9. @interface Student : Person

  10. -(void)fun;
  11. -(void)fun1:(NSString *)a;

  12. @end


点击(此处)折叠或打开

  1. //
  2. // Student.m
  3. // NSobject
  4. //
  5. // Created by 金海洋 on 15-4-15.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import "Student.h"

  9. @implementation Student

  10. -(void)fun{

  11.         NSLog(@"fun");

  12. }

  13. -(void)fun1:(NSString*)a{


  14.     NSLog(@"fun1111---%@",a);

  15. }

  16. @end


点击(此处)折叠或打开

  1. //
  2. // main.m
  3. // NSobject
  4. //
  5. // Created by 金海洋 on 15-4-15.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>
  9. #import "Student.h"
  10. #import "Person.h"

  11. void fun(){
  12. //反射
  13.     //类名的反射
  14.     NSString *s= @"Student";
  15.     
  16.     Class class = NSClassFromString(s);
  17.     
  18.     Person *p = [[class alloc]init ];
  19.     
  20.     NSLog(@"%@",p );

  21.     [p release];

  22.     NSString *s1 = NSStringFromClass([Person class]) ;
  23.     //方法的反射
  24.     NSString *s2 = @"test";
  25.     
  26.     SEL selector = NSSelectorFromString(s2);
  27.     [p performSelector:selector];
  28.     
  29.     NSString* s3 = NSStringFromSelector(selector);
  30.     
  31.     
  32. }

  33. int main(int argc, const char * argv[]) {
  34.     @autoreleasepool {
  35.         
  36.         Student *s=[[[Student alloc]init]autorelease];
  37.         
  38.         //判断对象是不是他的子类或者是类
  39.         //[Student class] 返回类名
  40.         if ([s isKindOfClass:[Person class]] ){
  41.         
  42.             NSLog(@"OK");
  43.         
  44.         }
  45.         
  46.         //判断对象是不是他的类
  47.         if ([s isMemberOfClass:[Person class]]) {
  48.             NSLog(@"OK11");
  49.         }
  50.         
  51.         
  52.         //直接调用
  53.         //[s fun];
  54.         //间接调用
  55.         [s performSelector:@selector(fun)];
  56.         [s performSelector:@selector(fun1:) withObject:@"adf"];
  57.         //延迟调用
  58.         [s performSelector:@selector(fun1) withObject:@"123" afterDelay:2];
  59.         
  60.         fun();
  61.         
  62.     }
  63.     return 0;
  64. }

二.COPY

要实现copy,需要先实现NSCoppying协议,创建的是不可变副本
要实现mutableCopy,需要实现NSMutableArray协议,创建的时可变的副本
深拷贝,对象的拷贝,mutableCopy
浅拷贝,指针的拷贝,copy
看下面代码:

点击(此处)折叠或打开

  1. //
  2. // Student.h
  3. // Copy
  4. //
  5. // Created by 金海洋 on 15-4-23.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import <Cocoa/Cocoa.h>

  9. //实现NSCopying协议
  10. @interface Student : NSObject<NSCopying>

  11. //copy代表 set方法里面release就对象,copy新对象
  12. //修改外面的变量,不会影响到内部变量
  13. @property (nonatomic,copy) NSString *name;

  14. +(id)studentWithName:(NSString*)name;

  15. @end


点击(此处)折叠或打开

  1. //
  2. // Student.m
  3. // Copy
  4. //
  5. // Created by 金海洋 on 15-4-23.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import "Student.h"

  9. @implementation Student


  10. -(void)setName:(NSString *)name{

  11.     if(_name != name){
  12.     
  13.         [_name release];
  14.         _name = [name copy];
  15.     
  16.         
  17.     }


  18. }


  19. -(void)dealloc{

  20.     [_name release];
  21.     [super dealloc];


  22. }

  23. +(id)studentWithName:(NSString*)name{
  24.     //最好写成 self class
  25.     Student *s =[[[[self class] alloc]init]autorelease];
  26.     s.name = name;
  27.     return s;

  28. }

  29. //实现copyWithZone方法
  30. - (id)copyWithZone:(NSZone *)zone{

  31.     Student *copy = [[[self class] allocWithZone:zone]init];
  32.     //拷贝名字给副本对象
  33.     copy.name = self.name;
  34.     
  35.     return copy;
  36.     //不需要释放,因为由外部来释放
  37. }

  38. - (NSString *)description
  39. {
  40.     return [NSString stringWithFormat:@"name--%@", _name];
  41. }

  42. @end


点击(此处)折叠或打开

  1. //
  2. // Stu.h
  3. // Copy
  4. //
  5. // Created by 金海洋 on 15-4-23.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>
  9. #import "Student.h"
  10. @interface Stu : Student


  11. @property (nonatomic, assign) int age;
  12. @property (nonatomic, copy) NSString *name;

  13. +(id)stuWithAge:(int)age name:(NSString*)name;

  14. @end


点击(此处)折叠或打开

  1. //
  2. // Stu.m
  3. // Copy
  4. //
  5. // Created by 金海洋 on 15-4-23.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import "Stu.h"

  9. @implementation Stu


  10. +(id)stuWithAge:(int)age name:(NSString *)name{

  11.     Stu *s = [Stu studentWithName:@"sfg"];
  12.     
  13.     s.age = age;
  14.     
  15.     return s;
  16.     
  17. }

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

  19.     //一定要调用父类的方法
  20.     Stu *s =[super copyWithZone:zone];

  21.     s.age = self.age;
  22.     
  23.     return s;
  24. }

  25. //子类直接访问不了父类的变量,必须通过get方法访问
  26. - (NSString *)description
  27. {
  28.     return [NSString stringWithFormat:@"name--%@,age-%d", self.name,_age];
  29. }

  30. @end


点击(此处)折叠或打开

  1. //
  2. // main.m
  3. // Copy
  4. //
  5. // Created by 金海洋 on 15-4-17.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>
  9. #import "Student.h"
  10. #import "Stu.h"

  11. void fun(){
  12. //字符串拷贝
  13.     
  14.     NSString *s = [[NSString alloc]initWithString:@"afa"];
  15.     //新的对象计数器+1
  16.     NSMutableString * s1 = [s mutableCopy];

  17.     NSLog(@"s---%zd",[s retainCount]);
  18.     NSLog(@"s1---%zd",[s1 retainCount]);
  19.     
  20.     //产生的对象不一样
  21.     if (s != s1) {
  22.         NSLog(@"不一样");
  23.     }
  24.     
  25.     [s1 appendString:@"asdfasdf"];
  26.     
  27.     //不会改变原来的值
  28.     NSLog(@"s1---%@",s1);
  29.     NSLog(@"s---%@",s);
  30.     
  31.     [s release];
  32.     [s1 release];

  33. }

  34. void fun1(){

  35.     
  36.     NSString *s = [[NSString alloc]initWithString:@"afa"];
  37.     
  38.     NSLog(@"s---%zd",[s retainCount]);
  39.     
  40.     //为了性能,因为copy产生的是不可变对象,所以产生的是s本身,直接返回原对象本身,源对象计数区加1
  41.     //相当于retain
  42.     //只有不可变对象调用copy的时候,是浅拷贝
  43.     NSString *s1 = [s copy];

  44.     NSLog(@"s---%zd",[s retainCount]);
  45.     
  46.     if (s == s1) {
  47.         NSLog(@"一样");
  48.     }
  49.     
  50.     [s release];
  51.     [s release];

  52. }

  53. void fun2(){


  54.     NSMutableString *s =[NSMutableString stringWithFormat:@"123"];
  55.     
  56.     //这个时候是深拷贝,因为一个是可变的
  57.     //产生了个新对象,计数器+1
  58.     NSMutableString *s1 = [s copy];
  59.     
  60.     if (s != s1) {
  61.         NSLog(@"不一样");
  62.     }

  63.     [s release];
  64.     [s1 release];
  65.     
  66. }


  67. void fun3(){

  68.     Student *s = [[[Student alloc]init]autorelease];
  69.     NSMutableString *s1 = [NSMutableString stringWithFormat:@"123"];
  70.     s.name = s1;
  71.     //因为set方法里用到的是copy,所以不会改变内部变量
  72.     [s1 appendString:@"345"];
  73.     //建议:String用copy,其他变量用retain
  74.     NSLog(@"%@",s.name);

  75. }


  76. void fun4(){

  77.     Student *s =[Student studentWithName:@"123"];
  78.     
  79.     Student *s1 =[s copy];
  80.     
  81.     s1.name = @"444";
  82.     
  83.     NSLog(@"%@",s1);
  84.     NSLog(@"%@",s);


  85. }

  86. void fun5(){


  87.     Stu *s = [Stu stuWithAge:445 name:@"sdf"];
  88.     
  89.     Stu *s1 = [s copy];
  90.     
  91.     s1.name = @"ddd";
  92.     s1.age = 32222;
  93.     
  94.     NSLog(@"%@",s1);
  95.     
  96.     NSLog(@"%@",s);

  97. }

  98. //copy语法的目的,改变副本的时候,不会影响到源对象
  99. int main(int argc, const char * argv[]) {
  100.     @autoreleasepool {
  101.         
  102.         
  103.         //fun();
  104.         //fun1();
  105.         //fun2();
  106.         //fun3();
  107.         //fun4();
  108.         fun5();
  109.         
  110.     }
  111.     return 0;
  112. }



阅读(712) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~