objectc

首页objectc
25
Sep
0

新手学oc十二:内存管理之自动释放池

1.self.dog = [[Dog alloc]init]问题

[[Dog alloc]init] 引用计数器为1

self.dog 属性设置器会再次为计数器+1

所以应该修改为

1._dog = [[Dog alloc]init];

2.self.dog = [[[Dog alloc]init]autorelease];自动释放池再次release;

2.类回收

-(void)dealloc

{

[_dog release];

[supper dealloc];

}

3.自动释放池 用于释放对象后面加autorelease的对象 其实就是单纯让引用计数-1

@autoreleasepool{

}

new copy alloc创建的对象不会自动入池 release 释放 通过autorelease才能入池

new 相当于同时alloc init

4.自动释放池可以嵌套使用

Foundation框架必须要有一个自动释放池

今天内容有点少 哈哈哈

oc程序语言方面的内容就学完了 下一步就要学ios的课程或是框架的学习 如文件操作类之类的

再次鼓励一下自己吧 确实很难坚持啊 不过每天要进步一点点 加油

22
Sep
5

新手学oc十一:内存管理之合成存取器

1.常量引用计数是没有的 它返回-1

2.内存释放检测使用Analyze按钮Snip20130922_1

Snip20130922_2

 

运行结果:

[c]

2013-09-22 20:49:45.310 MemoryManager[4108:303] eat

2013-09-22 20:49:45.312 MemoryManager[4108:303] retainCount = 1

2013-09-22 20:49:45.313 MemoryManager[4108:303] eat

2013-09-22 20:49:45.314 MemoryManager[4108:303] eat

2013-09-22 20:49:45.314 MemoryManager[4108:303] retainCount = 1

[/c]

3.合成存取器中对内存的使用方法

[c]

-(void)setMyStr:(NSString *)_myStr

{

myStr = _myStr;

[_myStr retain];

}

[/c]

多次调用后 引用计数就错误了 不能正确释放了

修改后:

[c]

-(void)setMyStr:(NSString *)_myStr

{

if (myStr != _myStr) {

myStr = _myStr;

[_myStr retain];

}

}

[/c]

这时引用给了另一个对象MYSTR 又会发生原对象引用计数不变 实际它应该减少才对 因为当前已经不引用它了 这时又不能正确释放

[c]

-(void)setMyStr:(NSString *)_myStr

{

if (myStr != _myStr) {

[myStr release];

myStr = _myStr;

[_myStr retain];

}

}

[/c]

如果这时不希望外部的MYSTR改变而影响内部MYSTR又怎么办呢

[c]

-(void)setMyStr:(NSString *)_myStr

{

if (myStr != _myStr) {

[myStr release];

myStr = _myStr;

[_myStr copy];//copy method

}

}

[/c]

或者

@property(copy) NSString *youStr;

22
Sep
0

新手学OC十:内存管理之引用计数和自动释放机制

1.类方法和实例方法 为什么不同

2.NSNull

表示什么也没有 只有一个方法[NSNull null]

3.所有权

凡是以alloc(dealloc) new 或名字中包含copy的方法创建的对象

使用retain获得所有权的对象

[object retain];

[object release];

取得对象的计数 返回NSUInteger

[object retainCount];

[c]

Car *car;

car = [[Car alloc]init];

NSLog(@"%ld",[car retainCount]);

[car retain];

NSLog(@"%ld",[car retainCount]);

[car release];

NSLog(@"%ld",[car retainCount]);

[car release];

//=============================

2013-09-22 19:48:09.032 ocTest[2235:303] 1

2013-09-22 19:48:09.034 ocTest[2235:303] 2

2013-09-22 19:48:09.034 ocTest[2235:303] 1

2013-09-22 19:48:09.035 ocTest[2235:303] car retain count = 0 ,car is dealloc!

[/c]

3.特殊的NSString

[c]

//只有这种方法RETAINCOUNT才为1

NSString *str = [[NSString alloc]initWithCString:"acb" encoding:NSUTF8StringEncoding];

NSLog(@"str retainCount = %ld",[str retainCount]);

NSString *st = @"china";

NSLog(@"st = %@",st);

NSLog(@"st retainCount = %ld",[st retainCount]);

NSString *s = [NSString stringWithFormat:@"i love"];

NSLog(@"s retainCount = %ld",[s retainCount]);

//=============================

2013-09-22 20:19:01.377 ocTest[3335:303] str retainCount = 1

2013-09-22 20:19:01.380 ocTest[3335:303] st = china

2013-09-22 20:19:01.384 ocTest[3335:303] st retainCount = -1

2013-09-22 20:19:01.387 ocTest[3335:303] s retainCount = 1

[/c]

4.retainCount在计数等于0之后就会变得不正确,因为对象已释放的原因

22
Sep
6

新手学oc九Foundation框架下:数值封装和词典

1.NSValue封装值

NSNumber是NSValue的子类

[c]

int nNum = 10;

NSMutableArray *intArray = [[NSMutableArray alloc]initWithCapacity:10];

//对象地址? ? ? ? 对象类型

NSValue *intVal = [NSValue valueWithBytes:&nNum objCType:@encode(int)];

[intArray addObject:intVal];

int k;

NSValue * intVal2;

intVal2 = [intArray objectAtIndex:0];

[intVal2 getValue:&k];

NSLog(@"intArray1 %d",k);

//封装DOUBLE

double ndouble = 3.14;

intVal = [NSValue valueWithBytes:&ndouble objCType:@encode(double)];

[intArray addObject:intVal];

double ndouble2;

intVal2 = [intArray objectAtIndex:1];

[intVal2 getValue:&ndouble2];

NSLog(@"intarray2 %f",ndouble2);

//封装结构

struct Student

{

char name[100];

double score;

char sex;

}stu = {"lilei",90.5,'M'};

intVal = [NSValue valueWithBytes:&stu objCType:@encode(struct Student)];

[intArray addObject:intVal];

struct Student myStu;

intVal2 = [intArray objectAtIndex:2];

[intVal2 getValue:&myStu];

NSLog(@"myStu %s,%f,%c",myStu.name,myStu.score,myStu.sex);

//=====================================

2013-09-18 07:15:59.000 ocTest[615:303] intArray1 10

2013-09-18 07:15:59.002 ocTest[615:303] intarray2 3.140000

2013-09-18 07:15:59.003 ocTest[615:303] myStu lilei,90.500000,M

[/c]

2.词典

[c]

NSDictionary *dictionary = [[NSDictionary alloc]initWithObjectsAndKeys:pointVal,@"point",doubleVal,@"double",

rangeVal,@"range", nil];

NSValue *value;

value = [dictionary objectForKey:@"point"];

NSPoint pointValue;

pointValue = [[dictionary objectForKey:@"point"]pointValue];? //用哪种方法都行

//? ? ? ? value = [dictionary objectForKey:@"point"];

//? ? ? ? [value getValue:&pointValue];

NSLog(@"point x = %f, y = %f",pointValue.x,pointValue.y);

[/c]

09
Sep
0

新手学oc八Foundation框架中:数组和枚举器

1.数组,它可以不是同一数据类型,它是任何对象,但它不能是C中的基本数据类型 int float enum struct或是nil或NULL

2.可变数组

[c]

TestCopying *test = [[TestCopying alloc]init];

test.myString = @"myFirstString";

NSArray *array = [[NSArray alloc]initWithObjects:@"first",test, nil];

NSLog([array objectAtIndex:0]);

//访问方法一

TestCopying *testCopying = (TestCopying*)[array objectAtIndex:1];

NSLog(testCopying.myString);

NSLog(@"array count = %d",[array count]);

//? ? ? ? [array release];

[test release];

[testCopying release];

 

NSString *a = @"wo ";

NSString *b = @"shi ";

NSString *c = @"shui";

array = [[NSArray alloc]initWithObjects:a,b,c, nil];

//访问方法二

for (int i = 0; i < [array count]; i++) {

NSLog(@"array's %@",array[i]);

}

//访问方法三 枚举器

NSEnumerator *enumerator;

enumerator = [array objectEnumerator];//顺序枚举

id obj;

while (obj = [enumerator nextObject]) {

NSLog(@"NSEnumerator? %@",obj);

}

 

enumerator = [array reverseObjectEnumerator];//反向枚举

while (obj = [enumerator nextObject]) {

NSLog(@"reverseNSEnumerator %@",obj);

}

//快速枚举

for (NSString *obj in array) {

NSLog(@"for in array is %@",obj);

}

 

NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithCapacity:10];//

[mutableArray addObject:a];

[mutableArray addObject:b];

[mutableArray addObject:c];

for (NSString *obj in mutableArray) {

NSLog(@"NSMutableArray %@",obj);

}

[mutableArray removeObject:a];

[mutableArray removeObjectAtIndex:1];

for (NSString *obj in mutableArray) {

NSLog(@"NSMutableArray remove %@",obj);

}

[mutableArray replaceObjectAtIndex:0 withObject:a];

for (NSString *obj in mutableArray) {

NSLog(@"NSMutableArray replace %@",obj);

}

//=======================================

2013-09-09 22:34:10.433 ocTest[2528:303] first

2013-09-09 22:34:10.435 ocTest[2528:303] myFirstString

2013-09-09 22:34:10.436 ocTest[2528:303] array count = 2

2013-09-09 22:34:10.436 ocTest[2528:303] array's wo?

2013-09-09 22:34:10.437 ocTest[2528:303] array's shi?

2013-09-09 22:34:10.437 ocTest[2528:303] array's shui

2013-09-09 22:34:10.442 ocTest[2528:303] NSEnumerator? wo?

2013-09-09 22:34:10.442 ocTest[2528:303] NSEnumerator? shi?

2013-09-09 22:34:10.443 ocTest[2528:303] NSEnumerator? shui

2013-09-09 22:34:10.443 ocTest[2528:303] reverseNSEnumerator shui

2013-09-09 22:34:10.444 ocTest[2528:303] reverseNSEnumerator shi?

2013-09-09 22:34:10.444 ocTest[2528:303] reverseNSEnumerator wo?

2013-09-09 22:34:10.445 ocTest[2528:303] for in array is wo?

2013-09-09 22:34:10.445 ocTest[2528:303] for in array is shi?

2013-09-09 22:34:10.446 ocTest[2528:303] for in array is shui

2013-09-09 22:34:10.446 ocTest[2528:303] NSMutableArray wo?

2013-09-09 22:34:10.447 ocTest[2528:303] NSMutableArray shi?

2013-09-09 22:34:10.449 ocTest[2528:303] NSMutableArray shui

2013-09-09 22:34:10.450 ocTest[2528:303] NSMutableArray remove shi?

2013-09-09 22:34:10.450 ocTest[2528:303] NSMutableArray replace wo?

[/c]