Guest User

Untitled

a guest
Jul 1st, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. NSMutableString as retain/copy
  2. @property (nonatomic, retain) NSMutableString *str1;
  3.  
  4. @property (nonatomic, retain) NSMutableString *str1;`
  5. // or
  6. @property (nonatomic, copy) NSMutableString *str1;`
  7.  
  8. @property (nonatomic, retain) NSMutableString *str1;`
  9.  
  10. @property (nonatomic, copy) NSMutableString *str1;`
  11.  
  12. - (void)setStr1:(NSMutableString *)arg
  13. {
  14. /* locking/observing/undo/etc omitted */
  15. NSMutableString * copy = [arg mutableCopy];
  16. NSMutableString * prev = str1;
  17. str1 = copy;
  18. [prev release];
  19. }
  20.  
  21. - (NSMutableString *)str1
  22. {
  23. /* locking/observing/etc omitted */
  24. /* don't return something the clients thinks they may
  25. possibly modify, and don't return something you may
  26. modify behind their back
  27. */
  28. return [[str1 mutableCopy] autorelease];
  29. // -- or???
  30. return [[str1 retain] autorelease];
  31. }
  32.  
  33. @interface MONThing : NSObject
  34. {
  35. @private
  36. NSMutableString * str1;
  37. }
  38.  
  39. /* note: passes/returns NSString */
  40. @property (nonatomic, copy) NSString * str1;
  41.  
  42. @end
  43.  
  44. @implementation MONThing
  45.  
  46. // no need for clients to create a mutableCopy
  47. - (void)setStr1:(NSString *)arg
  48. {
  49. /* locking/observing/undo/etc omitted */
  50. NSMutableString * copy = [arg mutableCopy];
  51. NSMutableString * prev = str1;
  52. str1 = copy;
  53. [prev release];
  54. }
  55.  
  56. // the result is clearly defined. return a copy for
  57. // thread-safety, expected behavior, and to minimize
  58. // further copies when handling the result.
  59. - (NSString *)str1
  60. {
  61. /* locking/observing/etc omitted */
  62. /* don't return something the clients thinks they
  63. may possibly modify, and don't return something
  64. you may modify behind their back
  65. */
  66. return [[str1 copy] autorelease];
  67. }
  68.  
  69. @end
  70.  
  71. @interface MONThing : NSObject
  72. {
  73. NSString * str1;
  74. }
  75.  
  76. @property (nonatomic, copy) NSString * str1;
  77.  
  78. @end
  79.  
  80. @implementation MONThing
  81.  
  82. @synthesize str1;
  83.  
  84. - (void)updateTimeElapsed:(NSTimeInterval)seconds
  85. {
  86. NSMutableString * s = [NSMutableString stringWithFormat:@"%f seconds", seconds];
  87. /* ...some mutations... */
  88. self.str1 = s;
  89. }
  90.  
  91. @end
Advertisement
Add Comment
Please, Sign In to add comment