黑马程序员——oc之self的使用

 -------android培训java培训iOS培训.Net培训、期待与您交流! ----------

首先我们创建一个学生类:Student类

这个学生类里有学生的id和学生的姓名name

 
 
  1. #import  
  2.  
  3.  
  4. @interface  
  5. Student : NSObject{  
  6.  
  7. //idname  
  8.  
  9. NSString *id;  
  10.  
  11. NSString *name;  
  12. }  
  13.  
  14. @property  
  15. (nonatomic,strong) NSString *id;  
  16. @property  
  17. (nonatomic,strong) NSString *name;  
  18.  
  19. @end  
  20.  
  21. 学生类的实现文件  
  22.  
  23. #import  
  24. "Student.h"  
  25.  
  26. @implementation  
  27. Student   
  28.  
  29. @synthesize  
  30. id,name;  
  31.  
  32. @end  

如果使用上面的方法来定义学生类的属性的get、set方法的时候,那么其他类访问的时候就是:

获取student的名字通过student.name来获取,给名字赋值则使用[student

setName:@“eve”]; 其中student是Student类的对象,如果在Student类内部访问其成员属性使用[self 
setName:@”evo”], 访问使用self.name;

上面的方法只是一种,但是很难解释self该不该使用。请看下面:

我们改写Student类

 
 
  1. #import  
  2.  
  3.  
  4. @interface  
  5. Student : NSObject{  
  6.  
  7. //idname  
  8.  
  9. NSString *_id;  
  10.  
  11. NSString *_name;  
  12. }  
  13.  
  14. @property  
  15. (nonatomic,strong) NSString *id;  
  16. @property  
  17. (nonatomic,strong) NSString *name;  
  18.  
  19. @end  
  20.  
  21. .m文件  
  22.  
  23. #import  
  24. "Student.h"  
  25.  
  26. @implementation  
  27. Student   
  28.  
  29. @synthesize  
  30. id = _id;  
  31. @synthesize  
  32. name = _name;  
  33.  
  34. @end  

可见这样的写法我们增加了_id和_name,其中@synthesize也有一定的变化。

如何这个时候使用self.name编译器就会报错,这样就说明了我们通常使用self.name实际使用的是student类name的get方法,同理name的set方法亦是如此。

另外网络上也有人从内存管理方面来说明的,我将其剪切出来以供学习:

ViewController.h文件,使用Student类,代码如下:

 
 
  1. #import  
  2. @  
  3. class Student;  
  4.  
  5. @  
  6. interface ViewController : UIViewController{  
  7.  
  8. Student *_student;  
  9. }  
  10.  
  11. @property  
  12. (nonatomic, retain) Student *student;  
  13.  
  14. @end  
  15.  
  16. ViewController.m文件,代码:  
  17.  
  18. #import  
  19. "ViewController.h"  
  20. #import  
  21. "Student.h"  
  22.  
  23. @implementation  
  24. ViewController  
  25. @synthesize  
  26. student = _student;  
  27.  
  28. -  
  29. (void)didReceiveMemoryWarning  
  30. {  
  31.  
  32. [super didReceiveMemoryWarning];  
  33. }  
  34.  
  35. #pragma  
  36. mark - View lifecycle  
  37.  
  38. -  
  39. (void)viewDidLoad  
  40. {  
  41.  
  42. [super viewDidLoad];  
  43. }  
  44.  
  45. -  
  46. (void) dealloc  
  47. {  
  48.  
  49. [_student release];  
  50.  
  51. _student = nil;  
  52.  
  53. [super dealloc];  
  54. }  
  55. 其它的方法没有使用到,所以这里就不在显示了。  
  56.  
  57. 在ViewController.m的viewDidLoad方法中创建一个Student类的对象  
  58.  
  59. Student  
  60. *mystudent = [[Student alloc] init];  
  61. self.student  
  62. = mystudent;  
  63. [mystudent  
  64. release];  

接下来就需要从内存角度来分析它们之间的区别了:

1、加self的方式:

 
 
  1. Student  
  2. *mystudent = [[Student alloc] init];  //mystudent 对象  
  3. retainCount = 1;  
  4. self.student  
  5. = mystudent; //student 对象 retainCount = 2;  
  6. [mystudent  
  7. release];//student 对象 retainCount = 1;  
  8. retainCount指对象引用计数,student的property  
  9. 是retain 默认使用self.student引用计数+1。  

2、不加self的方式

 
 
  1. Student  
  2. *mystudent = [[Student alloc] init];  //mystudent 对象  
  3. retainCount = 1;  
  4. student  
  5. = mystudent; //student 对象 retainCount = 1;  
  6. [mystudent  
  7. release]; //student 对象内存已释放,如果调用,会有异常  

3、加self直接赋值方式

self.student = [[Student alloc] init];//student 对象 retainCount =

2;容易造成内存泄露

由于objective-c内存管理是根据引用计数处理的,当一个对象的引用计数为零时,gcc才会释放该内存

个人总结:只需要在属性初始化的时候使用self.属性,其他时候直接使用属性名就行;使用self.是 使retaincount+1,为了确保当前类对此属性具有拥有权

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
精简不需要的Iphone手机自带软件 1.删除OTA升级: System/Library/LaunchDaemons  把3个进程删除 com.apple***le.softwareupdated.plist com.apple.OTATaskingAgent.plist com.apple.softwareupdateservicesd.plist 修改Applications/Preferences.app/ General.plist 删除下面:                <dict> <key>cell</key> <string>PSLinkCell</string> <key>cellClass</key> <string>PSBadgedTableCell</string> <key>detail</key> <string>SoftwareUpdatePrefController</string> <key>id</key> <string>SOFTWARE_UPDATE_LINK</string> <key>label</key> <string>SOFTWARE_UPDATE</string> </dict> 2.删掉系统壁纸 /Library/Wallpaper/iPhone 3.**菜单修改 路径/Applications/Preferences.app/Privacy.plist 4.访问限制修改 路径/Applications/Preferences.app/Restrictions.plist 5.设置修改 路径/Applications/Preferences.app/Settings.plist 以下很多删除项目在删除原文件后都需要在这个文件中修改。具体是:找到程序名称,如下面的“天气”,就找Weather,将两个<dict>之间的十多行,也可能只有几行,记不清了。全部删除。下同。记得要做好备份。 备份方法:点Settings.plist,导出到电脑。然后复制一份,在复制出的上面修改。保存,然后将手机中的原文件删除,将修改后的粘贴进去。注销ok。 6.删除天气 /Applications/Weather.app /System/Library/WeeAppPlugins/AttributionWeeApp.bundle /System/Library/AccessibilityBundles/Weather.axbundle /System/Library/AccessibilityBundles/WeatherAssistantUI-Assistant.axbundle /System/Library/AccessibilityBundles/WeatherFramework.axbundle /System/Library/AccessibilityBundles/WeatherNotifications.axbundle /System/Library/Assistant/UIPlugins/WeatherAssistantUI.siriUIBundle /System/Library/BulletinBoardPlugins/WeatherNotifications.bundle /System/Library/PrivateFrameworks/Weather.framework /System/Library/LocationBundles/Weather.framework 7.删除股票 /Applications/Stocks.app /System/Library/WeeAppPlugins/StocksWeeApp.bundle /System/Library/AccessibilityBundles/Stocks-Assistant.axbundle /System/Library/AccessibilityBundles/Stocks.axbundle /System/Library/AccessibilityBundles/StocksFramework.axbundle /System/Library/AccessibilityBundles/StocksWeeApp.axbundle /System/Library/Assistant/Plugins/Stocks.assistantBundle /System/Library/Assistant/UIPlugins/Stocks.siriUIBundle /System/Library/PrivateFrameworks/Stocks.framework 8.删除语音备忘录 /Applications/VoiceMemos.app /System/Library/AccessibilityBundles/VoiceMemos.axbundle /System/Library/AccessibilityBundles/VoiceMemosFramework.axbundle /System/Library/PrivateFrameworks/VoiceMemos.framework /System/Library/SearchBundles/VoiceMemos.searchBundle /System/Library/SpringBoardPlugins/VoiceMemosLockScreen.lockbundle /System/Library/SyncBundles/VoiceMemos.syncBundle /System/Library/UsageBundles/VoiceMemosUsageBundle.bundle 9.删除指南针 /Applications/Compass.app /Applications/CompassCalibrationViewService.app /System/Library/AccessibilityBundles/Compass.axbundle /System/Library/AccessibilityBundles/CompassViewCalibrationService.axbundle /System/Library/PreferenceBundles/CompassSettings.bundle /System/Library/PrivateFrameworks/CompassUI.framework 10.删除通知中心 日历日视图 /System/Library/WeeAppPlugins/CalendarWidget.bundle 11.删除提醒事项 /Applications/Reminders.app /System/Library/WeeAppPlugins/RemindersWidget.bundle /System/Library/AccessibilityBundles/Reminders-Assistant.axbundle /System/Library/AccessibilityBundles/Reminders.axbundle /System/Library/Assistant/Plugins/Reminders.assistantBundle /System/Library/Assistant/UIPlugins/Reminders.siriUIBundle /System/Library/PreferenceBundles/RemindersSettings.bundle /System/Library/SearchBundles/Reminders.searchBundle 通知中心图标位置: /System/Library/BulletinBoardPlugins/CalendarProvider.bundle  打开Info.plist 删掉第8行: <string>EKBBReminderProvider</string> ,保存 12.删除passbook /Applications/Passbook.app /Applications/PassbookUIService.app /System/Library/AccessibilityBundles/PassKitFramework.axbundle /System/Library/AccessibilityBundles/PassesLockScreenPlugin.axbundle /System/Library/BulletinBoardPlugins/PassesBulletinBoardPlugin.bundle  ”通知中心“的图标 /System/Library/UsageBundles/PassbookUsageBundle.bundle   “用量”的图标 /System/Library/Frameworks/PassKit.framework /System/Library/LaunchDaemons/com.apple.passd.plist /System/Library/SpringBoardPlugins/PassesLockScreenPlugin.lockbundle 13.删除 Nike /Applications/Nike.app /System/Library/AccessibilityBundles/Nike.axbundle /System/Library/AccessibilityBundles/VictoriaSettings.axbundle /System/Library/PreferenceBundles/VictoriaSettings.bundle /System/Library/SpringBoardPlugins/NikeLockScreen.bundle 14.禁用spotlight搜索【鉴于大家反映删除后仍然可以拉出,但是功能失效的情况,这个就暂时删除了,大家可以用插件禁止】 15.删除Twitter: 删除 /System/Library/Frameworks//Twitter.framework /System/Library/AccessibilityBundles//TwitterFramework.axbundle /System/Library/AccessibilityBundles//TwitterSettings.axbundle 16.删除facebook 删除 /Applications//FacebookAccountMigrationDialog.app /System/Library/AccessibilityBundles//FacebookSettings.axbundle /System/Library/PreferenceBundles//FacebookSettings.bundle 打开/Applications/Preferences.app/Settings.plist 找到Facebook删除dict之间的代码 17.删除微博: 删除/System/Library/PreferenceBundles//WeiboSettings.bundle 打开/Applications/Preferences.app /Applications/Settings 找到WeiboSettings删除dict之间的删 腾讯微薄和这个一样。主要是名字有变化,前面加了Tencent就是腾讯的那几个字母。TencentWeibo 19.删除邮件 Applications/Mobilemail.app /Library/Application Support/Mail /private/var/root/Library/Mail /var/root/Library/Mail /System/Library/AccessibilityBundles/MobileMail.axbundle /System/Library/AccessibilityBundles/MobileMailSettings.axbundle /System/Library/BulletinBoardPlugins/MailDataProvider.bundle 20.删除地图 /Applications/Maps.app /System/Library/AccessibilityBundles//MapKitFramework.axbundle /System/Library/AccessibilityBundles//Maps-Assistant.axbundle /System/Library/AccessibilityBundles//Maps.axbundle /System/Library/PreferenceBundles//MapsSettings.bundle /System/Library/Frameworks//MapKit.framework 21.删除视频 /Applications/Videos.app /System/Library/AccessibilityBundles//Videos.axbundle /System/Library/PreferenceBundles//VideoSettings.bundle /System/Library/Frameworks//VideoToolbox.framework 这个全部删除后在用量中有个空白图标,找不到原因,尚未解决。 22.删除 Game Center /Applications/GameCenterUIService.app /Applications/Game Center~iphone.app /System/Library/BulletinBoardPlugins/GameKitDataProvider.bundle 23. 删除备忘录 /Applications/Notes.app /System/Library/AccessibilityBundles//Notes-Assistant.axbundle /System/Library/PreferenceBundles//NotesSettings.bundle
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值