iOS积累-类方法能否访问自己的属性, self调用问题

类方法不能直接访问自己的属性  即

[objc]  view plain  copy
  1. self.iconImageArr;不能用self访问。  

在类方法中要访问自己的属性,必须要在类中实例化一个对象,然后再调用;

如下方代码:

[objc]  view plain  copy
  1. //  
  2. //  FirstTableView1.m  
  3. //  xxtnm_iOS_con  
  4. //  
  5. //  Created by xxt-imac on 16/1/21.  
  6. //  Copyright © 2016年 内蒙校信通. All rights reserved.  
  7. //  
  8.   
  9. #import "FirstTableView1.h"  
  10.   
  11. @interface FirstTableView1()<UITableViewDataSource,UITableViewDelegate>  
  12.   
  13. @property(strong,nonatomic)NSArray *iconImageArr;  
  14.   
  15. @end  
  16. @implementation FirstTableView1  
  17.   
  18.   
  19. +(instancetype)initWithFirstTableView:(UITableView *)tableView andViewController:(UIViewController *)viewController andIconArray:(NSArray *)iconArray andTitleArray:(NSArray *)titleArray  
  20. {  
  21.     FirstTableView1 *tab = [[FirstTableView1 alloc]init];  
  22.     tab.iconImageArr;  
  23.      
  24.     return self;  
  25. }  
  26. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  27. {  
  28.     return 10;  
  29. }  
  30. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  31. {  
  32.     NSString *cellID = @"cell";  
  33.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];  
  34.     if (cell == nil) {  
  35.         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];  
  36.     }  
  37.     return cell;  
  38. }  
  39. @end  


下方这个就是我所说的,不过这样调用并没有什么卵用,要想调用自己的属性,最好还是搞一个实例方法吧。
[objc]  view plain  copy
  1. FirstTableView1 *tab = [[FirstTableView1 alloc]init];  
  2.  tab.iconImageArr;  
下方实例方法调用

[objc]  view plain  copy
  1. -(instancetype)initWithFirstTableView:(UITableView *)tableView andViewController:(UIViewController *)viewController andIconArray:(NSArray *)iconArray andTitleArray:(NSArray *)titleArray  
  2. {  
  3. //    FirstTableView1 *tab = [[FirstTableView1 alloc]init];  
  4. //    tab.iconImageArr;  
  5.     self.iconImageArr;  
  6.      
  7.     return self;  
  8. }  

Objective-C里面既有实例方法也类方法。类方法(Class Method) 有时被称为工厂方法(Factory Method)或者方便方法(Convenience method)。工厂方法的称谓明显和一般意义上的工厂方法不同,从本质上来说,类方法可以独立于对象而执行,所以在其他的语言里面类方法有的时候被称为静态方法。
注意点一:类方法
1,类方法可以调用类方法。
2,类方法不可以调用实例方法,但是类方法可以通过创建对象来访问实例方法。
3,类方法不可以使用实例变量。类方法可以使用self,因为self不是实例变量。
4,类方法作为消息,可以被发送到类或者对象里面去(实际上,就是可以通过类或者对象调用类方法的意思)。
注意点二:self的规则
大家需要记住下面的规则:
1,实例方法里面的self,是对象的首地址。
2,类方法里面的self,是Class.
尽管在同一个类里面的使用self,但是self却有着不同的解读。在类方法里面的self,可以翻译成class self;在实例方法里面的self,应该被翻译成为object self。在类方法里面的self和实例方法里面的self有着本质上的不同,尽管他们的名字都叫self。

类方法创造的对象要不要用release释放?
答 不需要 这个对象被放到自动释放池中

Object-C中的私有方法和私有成员变量

成员变量默认对内是共有的,对外是私有的。

[objc]  view plain  copy
  1. @interface Controller : NSObject  
  2. {  
  3.   
  4. @private: NSString *something;  
  5. }  
  6. + (void)thisIsAStaticMethod;  
  7. - (void)thisIsAnInstanceMethod;  
  8. @end  
  9.   
  10. @interface Controller (Private)  
  11. - (void)thisIsAPrivateMethod;   
  12. @end  
  13.   
  14. 下面的代码就是怎样获取私有变量(记得加头文件#import ):  
  15. NSString *str;  
  16. Mobj *obj = [[Mobj alloc] init];  
  17. object_getInstanceVariable(obj, "mt_", (voidvoid *)&str);  
  18. NSLog(@"%@",str);  
  19. [obj release];  
[objc]  view plain  copy
  1. IOS实例方法和类方法的区别  
  2.   
  3. 类方法和实例方法    
  4. 1:实例方法是— 类开头是+ 实例方法是用实例对象访问,类方法的对象是类而不是实例,通常创建对象或者工具类。  
  5. 在实例方法里,根据继承原理发送消息给selfsuper其实都是发送给self  
  6. 在类方法里面self是其他的类的类方法,在类方法中给self发送消息只能发类方法self是类super也是  
  7. 什么时候用类方法,要创建一个实例时候获取一个共享实例,或者获取关于类的一些共有信息  
  8.   
  9. 2:  
  10. 类方法(class method)和实例方法(instance method)。类方法被限定在类范围内,不能被类的实例调用(即脱离实例运行)。alloc就是一种类方法。实例方法限定在对象实例的范围内(即实例化之前不能运行)。init就是一种实例方法,被alloc方法返回的对象实例调用。  
  11.   
  12.   
  13.   
  14. NSObject * object1 = [[NSObject alloc] init];  
  15.   
  16. instance method  以减号 "-" 开头   
  17. class method  以加号 “+” 开头,相当于static方法   
  18. 3:see see 更健康  
  19. Objective-C  
  20.   
  21. 1.OC是一门基于C的面向对象语言,是C语言的一个超集,同时具有C语言的特征  
  22.   
  23.   
  24.   
  25. 2.OC对类的定义和实现以及初始化  
  26.   
  27.   
  28.   
  29. //声明类接口,继承NSObject对象(该对象是OC中所有类的顶级父类,所有类都继承于它)  
  30.   
  31. @interface ClassName :NSObject   
  32.   
  33. //成员属性和成员函数的声明  
  34.   
  35. +(void)function;//类方法,不需要实例化对象就可以调用的方法  
  36.   
  37. - (void)function2 :(NSString *)arg;//成员方法,必须通过实例化的对象调用  
  38.   
  39. @end  
  40.   
  41.   
  42.   
  43. //实现类  
  44.   
  45. @imlementation ClassName  
  46.   
  47. //成员属性初始化和方法的定义  
  48.   
  49. @end  
  50.   
  51.   
  52.   
  53. 对象的初始化:ClassName *obj = [[ClassName alloc] init]  
  54.   
  55. OC中以消息机制传递信息,发送alloc消息给类分配内存空间,发送init消息生成对象,指针指向对象本身。  
  56.   
  57.   
  58.   
  59. 3.类方法的调用  
  60.   
  61. [obj function];  
  62.   
  63. NSString *str = [NSString stringWithString:@"hello"];  
  64.   
  65. [obj function2 : str];  
  66.   
  67.   
  68.   
  69. 4.输出函数  
  70.   
  71. 根据不同的输出格式输出不同的值 (%d :整形 ,%@:对象<发送description消息>,%s:字符串)  
  72.   
  73. NSlog(@“The result is %d”,intNum);   
  74.   
  75. CF代表Core Foundation (Cocoa)  
  76.   
  77. CFShow发送description给它显示的对象,CFShow打印的信息不会显示时间戳,NSLog会显示,同时CFShow不需要格式字符 串,它只能用于对象  
  78.   
  79. CFShow(obj);  
  80.   
  81.   
  82.   
  83. 5.属性  
  84.   
  85. 支持点表示法:myTableViewCell.textLabel.text = @"hello" 等价于 [[myTableViewCell textLabel] setText:@"hello"];  
  86.   
  87.   
  88.   
  89. 使用属性生成器 property  
  90.   
  91. 在h文件中声明: @property int year  
  92.   
  93. 在m文件中合成生成器:@synthesize year  
  94.   
  95. 使用 obj.year = 1999 相当于调用了 [obj setYear:1999];  
  96.   
  97.   
  98.   
  99. 可以自定义取值方法和赋值方法(getter and setter)  
  100.   
  101. -(int)year  
  102.   
  103. {  
  104.   
  105. return year;  
  106.   
  107. }  
  108.   
  109.   
  110.   
  111. - (void) setYear : (int) newYear  
  112.   
  113. {  
  114.   
  115. //此处添加了一些基本的内存管理方法,保留新的值,释放以前的值  
  116.   
  117. if(newYear != year)  
  118.   
  119. {  
  120.   
  121. [year release];  
  122.   
  123. year = [newYear retain];  
  124.   
  125. }  
  126.   
  127. }  
  128.   
  129.   
  130.   
  131. 也可以绕过oc的命名约定,自己指定gettersetter方法的名称;  
  132.   
  133. @property(getter = isExist,setter = setExist:) BOOL exist;  
  134.   
  135. @synthesize exist;  
  136.   
  137. 使用过程中既可以使用新定义的方法名,也可以使用以前的方法(点表示法)  
  138.   
  139.   
  140.   
  141. 属性的特性:readwrite readonly assign retain copy nonatomic   
  142.   
  143. assign:默认行为,使用@property int year就使用了assign行为,就是给实例变量赋了一个值  
  144.   
  145. retain:实现了两个功能,一个是保留了赋值时传递的对象,其次是赋值前释放了以前值,使用retain可以实现上面讨论的内存管理的优点,使用时加上 @property (retainint year;  
  146.   
  147. copy:发送一条复制的消息给被传递的对象,保留它,并释放任何以前的值;  
  148.   
  149. nonactomic:非原子访问器,加上后可以提升访问速度,但当两个线程同时修改同一个属性时就会出现问题,原子属性可以保证属性在被一个线程使用时不被另一个线程访问,不存在atomic关键字,默认情况下,所有方法都是自动合成的。(类似与java中的线程锁机制synchronized)  
  150.   
  151. readwrite:可读


  关于self的问题:
  
self不仅可以表示当前对象还可以表示类本身, 也就是说它既可以用在静态方法中又可以用在动态方法中
+( void )printInfo{
    NSLog ( @"Hello,World!" );
}
+( void )showMessage{
    [ self printInfo ];
}
上面的是可以调用的
+( void )printInfo{
    NSLog ( @"Hello,World!" );
}
-( void )showMessage{
    [ self printInfo ];
}
下面的是不可以调用的,会报错
结论: 在+方法里面self就是类, 在-方法里面就是实例.  -方法里面的self是不能调用+方法的.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值