O'Reilly logo

iOS Programming: The Big Nerd Ranch Guide by Joe Conway, Aaron Hillegass

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Copying

There is one more change we need to make to our properties – specifically, the two properties that point to instances of NSString.

In general, when you have a property that points to an instance of a class that has a mutable subclass (like NSString or NSArray), it is safer to make a copy of the object to point to rather than pointing to an existing object that could have other owners.

For instance, imagine if a BNRItem was initialized so that its itemName pointed to an instance of NSMutableString.

N​S​M​u​t​a​b​l​e​S​t​r​i​n​g​ ​*​m​u​t​a​b​l​e​S​t​r​i​n​g​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​S​t​r​i​n​g​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​ B​N​R​I​t​e​m​ ​*​i​t​e​m​ ​=​ ​[​[​B​N​R​I​t​e​m​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​I​t​e​m​N​a​m​e​:​m​u​t​a​b​l​e​S​t​r​i​n​g​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required