iOS中的記憶體管理精講

來源:互聯網
上載者:User

標籤:

main.m

#import <Foundation/Foundation.h>#import "Person.h"#import "Student.h"int main(int argc, const char * argv[]) {    @autoreleasepool {//        NSString *name = [[NSString alloc] initWithFormat:@"張三"];//       // NSLog(@"%lu", [name retainCount]);//        Person *p = [[Person alloc] init];//        [p setName:name];//        [p setName:name];//        //p.name = name;//        [name release];//        //    //        //        //        //        NSString *name1 = [[NSString alloc] initWithFormat:@"張四"];//        [p setName:name1];//        //        //        [p sayHi];        Student *stu = [[Student alloc] initWithName:@"張三" ];        //stu - 1        stu.name = [NSString stringWithFormat:@"習大大"];        //stu.name -2        [stu release];        //stu - 0  stu.name 1        //分析:[NSString stringWithFormat:@"習大大"]計數器為1        //stu.name//        NSLog(@"----%@",stu.name);//        //       NSString * name = stu.name = [NSString stringWithFormat:@"習大大"];//        NSLog(@"%lu",name.retainCount);//         NSLog(@"----%@",stu.name);                                                //        NSString *str = [NSString stringWithFormat:@"習大大"];//        NSLog(@"%lu", str.retainCount);////        [stu setName:str];////        stu.name = str;//        [str retain];//        //這裡的name是堆區 有引用計數//        stu.name = str;//        //        NSLog(@"---%lu", str.retainCount);//        NSLog(@"%lu",  stu.name.retainCount);//        NSLog(@"=====%lu",  stu.name.retainCount);//        [stu release];                        //這裡的string是常量區沒有引用計數//        NSString *string = [[NSString alloc] initWithString:@"小芬"];//        NSLog(@"%lu",[string retainCount]);                         }           return 0;}/*集合的記憶體管理: 1.當向集合中添加元素時,元素的引用計數會被 +1; 2.當元素從集合中移除時,元素的引用計數會被 -1; 3.當集合銷毀時,會將集合中所有元素 -1; */

Person.h

#import <Foundation/Foundation.h>@interface Person : NSObject@property (nonatomic, retain) NSString *name;@property (nonatomic, retain) NSString *genter;@property (nonatomic, assign) NSInteger age;- (void)sayHi;@end

 

Person.m

#import "Person.h"@implementation Person@synthesize name = _name;- (void)setName:(NSString *)name{//    if (_name != name) {  // 解決了野指標問題//        [_name release]; // 解決記憶體泄露問題//        _name = [name retain]; // 解決野指標問題//    }    //為什麼不直接賦值,堆區對象的直接賦值是地址的賦值,在mian.m中    //[name release];    _name = name;}- (NSString *)name{    //把_name的計數器加1 放到自動釋放池    //蘋果的安全處理機制    return [[_name retain] autorelease];}//copy 內部實現//- (void)setName:(NSString *)name{//    if(_name != name)//    {//        [_name release];//        _name = [name copy];//    }//}- (void)sayHi{    NSLog(@"%@,%@,%ld", _name, _genter, _age);}- (void)dealloc{    [_name release]; // 在對象銷毀之前,將執行個體變數的引用計數 -1    [_genter release];    NSLog(@"person沒有了");    [super dealloc]; // 調用父類}@end

 

Student.h

#import <Foundation/Foundation.h>@interface Student : NSObject@property (nonatomic, retain) NSString *name;@property (nonatomic, retain) NSString *gender;@property (nonatomic, assign) NSInteger age;- (id)initWithName:(NSString *)name;+ (id)studentWithName:(NSString *)name;@end

Student.m

#import "Student.h"@implementation Student//copy 內部實現//- (void)setName:(NSString *)name{//    if(_name != name)//    {//        [_name release];//        _name = [name copy];//    }//}@synthesize name = _name;- (void)setName:(NSString *)name{    NSLog(@"%@",_name);    NSLog(@"%lu",_name.retainCount);    if (_name != name) {  // 解決了野指標問題        [_name release]; // 解決記憶體泄露問題        NSLog(@"%lu",_name.retainCount);        _name = [name retain]; // 解決野指標問題         NSLog(@"%lu",_name.retainCount);    }}- (NSString *)name{    //把_name的計數器加1 放到自動釋放池    //蘋果的安全處理機制    return [[_name retain] autorelease];}- (id)initWithName:(NSString *)name{    if (self = [super init]) {        self.name = name;        //_name = name;    }    return self;}+ (id)studentWithName:(NSString *)name{    Student *stu = [[Student alloc] initWithName:name];    return [stu autorelease];}- (void)dealloc{    NSLog(@"Student回收了");    [super dealloc];}@end

 

iOS中的記憶體管理精講

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.