[iOS_Dev] @copy @retain @assign

[iOS_Dev]   @copy  @retain  @assign 

//************************************************************************************************************************************************************************************//


干货:


@copy (实测)


/** 码字--开始 **/
        //from - immutable And mutable
               NSString        *str1 = @"str1";
        NSMutableString *mutableStr1 = [NSMutableString stringWithString:@"mutableStr1"];
               NSArray         *arr1 = [       NSArray arrayWithObjects:@"1",@"2",@"3", nil];
        NSMutableArray  *mutableArr1 = [NSMutableArray arrayWithObjects:@"A",@"B",@"C", nil];
        //to - immutable
        NSString        *copyStr1 = [str1        copy];
        NSString *mutableCopyStr1 = [str1 mutableCopy];
        NSString        *copyMutableStr1 = [mutableStr1        copy];
        NSString *mutablecopyMutableStr1 = [mutableStr1 mutableCopy];
        //to - mutable
        NSMutableString        *copyStr1ToM = [str1        copy];
        NSMutableString *mutableCopyStr1ToM = [str1 mutableCopy];
        NSMutableString       *copyMutableStr1ToM = [mutableStr1        copy];
        NSMutableString *mutablecopyMutableStrToM = [mutableStr1 mutableCopy];
       
        //log
        NSLog(@"copyStr1=%p,%@----from---str1=%p,%@",
                copyStr1,copyStr1,       str1,str1);
        NSLog(@"mutableCopyStr1=%p,%@------------from---str1=%p,%@",
                mutableCopyStr1,mutableCopyStr1,        str1,str1);
        NSLog(@"copyMutableStr1=%p,%@------------from---mutableStr1=%p,%@",
                copyMutableStr1,copyMutableStr1,        mutableStr1,mutableStr1);
        NSLog(@"mutablecopyMutableStr1=%p,%@--------------from---mutableStr1=%p,%@",
                mutablecopyMutableStr1,mutablecopyMutableStr1,   mutableStr1,mutableStr1);
        NSLog(@"copyStr1ToM=%p,%@---------from---str1=%p,%@",
                copyStr1ToM,copyStr1ToM,         str1,str1);
        NSLog(@"mutableCopyStr1ToM=%p,%@---------------from---str1=%p,%@",
                mutableCopyStr1ToM,mutableCopyStr1ToM,        str1,str1);
        NSLog(@"copyMutableStr1ToM=%p,%@---------------from---mutableStr1=%p,%@",
                copyMutableStr1ToM,copyMutableStr1ToM,        mutableStr1,mutableStr1);
        NSLog(@"mutablecopyMutableStrToM=%p,%@---------------------from--mutableStr1=%p,%@",
                mutablecopyMutableStrToM,mutablecopyMutableStrToM,       mutableStr1,mutableStr1);
/** 码字--好累 **/


2014-11-27 14:02:20.437 TestCopy[466:6731] <span style="color:#ff0000;">copyStr1</span>=<span style="color:#ff0000;">0x100001088</span>,str1----from---<span style="color:#ff0000;">str1</span>=<span style="color:#ff0000;">0x100001088</span>,str1
2014-11-27 14:02:20.439 TestCopy[466:6731] mutableCopyStr1=0x100206420,str1------------from---str1=0x100001088,str1
2014-11-27 14:02:20.440 TestCopy[466:6731] copyMutableStr1=0x100206500,mutableStr1------------from---mutableStr1=0x100204d80,mutableStr1
2014-11-27 14:02:20.440 TestCopy[466:6731] mutablecopyMutableStr1=0x100206520,mutableStr1--------------from---mutableStr1=0x100204d80,mutableStr1
2014-11-27 14:02:20.441 TestCopy[466:6731] <span style="color:#ff0000;">copyStr1ToM</span>=<span style="color:#ff0000;">0x100001088</span>,str1---------from---<span style="color:#ff0000;">str1</span>=<span style="color:#ff0000;">0x100001088</span>,str1
2014-11-27 14:02:20.441 TestCopy[466:6731] mutableCopyStr1ToM=0x100206580,str1---------------from---str1=0x100001088,str1
2014-11-27 14:02:20.441 TestCopy[466:6731] copyMutableStr1ToM=0x1002065e0,mutableStr1---------------from---mutableStr1=0x100204d80,mutableStr1
2014-11-27 14:02:20.441 TestCopy[466:6731] mutablecopyMutableStrToM=0x100206600,mutableStr1---------------------from--mutableStr1=0x100204d80,mutableStr1
Program ended with exit code: 0


【copy】在OC环境中, 对String进行拷贝(包括不可变的NSString 和 可变的NSMutableString,copy 和mutableCopy),有且只有copy immutable源,才是浅复制,其他都是深复制。注: 已经证明 适用于string; 容器数据类型如字典数组还没去验证。



///************************************************************************************************************************************************************************************//



@retain  @assign (iTunes  Stanford 笔记 --- iOS7 Dev 2013-2014Fall)



ARC.  Awesome !  Much better than garbage collection of Java ! --- garbage collection come along later. 

All pointer Property can either be STRONG or WEAK. ---- to know what to do with the memory in the heap.

Strong, means ---- keep the memory for the thing --- that this pointer points to IN THE HEAP, as long as I or anyone else has a STRONG pointer to it. 
Referencing count ---- keep track of every single STRONG pointer to a object in the heap, as long as at least ONE strong pointer exists, the object LIVE in the heap, AND as soon as NO strong pointer exists, the memory of the object WILL be cleaned(freed) instantly(right away) !

Weak, means ---- .... Keep the memory as long as someone else has a STRONG pointer to it, BUT as soon as no one has a STRONG pointer to it, it gets freed from memory, and THIS pointer(the weak) get set to NIL. Nil means this pointer doesn't point to anything. Nil is the same as ZERO. 

Oc, sending message to nil --- WON'T crash !  For no instance.Var there, executes nothing in code.  Thus returns ZERO(if any).

If you have a strong pointer and points to something, and then you set it to NIL, now the strong pointer doesn't point to that thing, as long as no one else point to it, you can free the memory ---- OC will clean up the memory for you.  Question here!!
Or if you have a pointer point to something in the heap, and you make a point to something else in the heap, then you no longer have strong pointer to that other thing in the heap, as long as no one else does, you can clear up the memory.
--- at this time, STRONG pointer can still point to something; BUT WEAK, NO ! The weak pointer is set to NIL.


Nonatomic, means ---- calling the setter and getter(i.e @synthesis FOO--instance.Var) that goes along with @property, is NOT thread safe --- no locking code. --- canNOT have two threads trying to set the property at the same time. 
But For sake of efficiency --- main thread. 
Multi-threading in iOS, is not but having a single object that multi-threads are setting on, we usually have separate object running in another thread in your MODAL. And then other UI stuff running in the UI thread are talking thread to thread. So we don't need this, and knowing that, what's going to happen here when we set the property is ?*thread is going to creat the getter and setter methods to set the contents automatically for us, and we want then to be simpler. If we don't say nonatomic, there are going to a lots of locking code in there. Multi-threads are accessing the setter and getter here,they need locking code, especially where we implement the setter and getter ourselves, which we are going to sometimes. But the default here is we don't implement the setter and getter, it is automatically in there for us.

@synthesis content = _content;
Weird !?
Specifying the name of the instance variable that we are going to use to store THE STUFF in (i.e _contents).
means --- @synthesis contents, to use instance variable with NAME (_contents).
= here means not ASSIGN, but "to use instance variable with NAME (_contents)".

If NO @synthesis there, NO memory space for instance.Var get created. Let alone any name to be referred to it in your getter and setter.

Once you tap @property, the setter and getter method is there, but they do not show straightforward. 

all object lives in HEAP(where we alloc memory space), and we have pointers to it(the object); strong means retain, but weak means more than assign --- weak is much safer --- because as long as the reference count of the object is bigger than one(i.e. the object is referenced, or say pointed to, by sending message STRONG or RETAIN), the object still lives in the heap. Assign and = both means pointing to the same MEMORY SPACE, i.e SAME POINTER! BUT if the reference count is ZERO, the object is Dealloc Automatically by Objective-C. ARC ! --- reference OR say point to means OWNERSHIP Of the object, which MUST BE responsible for alloc, copy, new AND corresponding RELEASE or autoRELEASE. Property STRONG and RETAIN will release the old object that pointed to, and retain the new object that pointed to,(which is automatically but NOT EXPLICITLY implemented in the implementation .m file,the hidden code of setter method), but weak(weak pointer here) will get set to NIL if the object weak pointer points to get Dealloc due to reference count decreasing to ZERO, but assign won't get set to NIL). While sending message to nil won't crash the app, trying to access the object that has already get Dealloc will crash tha app, SO WEAK IS SAFER THAN ASSIGN. 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值