NSString property: copy or retain?

前端 未结 10 1203
予麋鹿
予麋鹿 2020-11-22 02:53

Let\'s say I have a class called SomeClass with a string property name:

@interface SomeClass : NSObject
{
    NSString* name;
}

@p         


        
10条回答
  •  死守一世寂寞
    2020-11-22 03:10

    I try to follow this simple rule:

    • Do I want to hold on to the value of the object at the point in time when I am assigning it to my property? Use copy.

    • Do I want to hold on to the object and I don't care what its internal values currently are or will be in the future? Use strong (retain).

    To illustrate: Do I want to hold on to the name "Lisa Miller" (copy) or to I want to hold on to the person Lisa Miller (strong)? Her name might later change to "Lisa Smith", but she will still be the same person.

提交回复
热议问题