2

I have a textview that user can edit, when I try to insert the text from the textview to a local property from type Nsstring, I got in this property NSMallocBlock. what is the reason? thank you! this is the defintaion of cartItemComment:

@property (nonatomic, assign) NSString * cartItemComment;

and this is the code:

CartItem *cartItem = [[CartItem alloc]init];
cartItem.cartItemComment = itemRequestText.text;

cartitem is a object that have a property cartItemComment, after those lines, I got NSMallocBlock in cartItemComment.

another problem, i can get a weird string like this:

yoc
  • 214
  • 2
  • 13
  • Cant help without code. Please add your code here – Kapil G Aug 10 '17 at 14:41
  • I added more info. does it understand now? – yoc Aug 10 '17 at 14:59
  • No. `itemRequestText.text` is nil? Could you show the definition of `cartItemComment`. Is it a property ? strong and nonatomic? (if you put assign, clearly that could crash). Also, it a NSString property ? Could you show the stack trace of the crash ? The whole error message? – Larme Aug 10 '17 at 15:01
  • this is not a crash, but after, when i try to use this cartItemComment it is failed. – yoc Aug 10 '17 at 15:16
  • the cartItemComment is not nil, it just get a weird type as NSMallocBlock – yoc Aug 10 '17 at 15:17
  • What do you think is weird about the image you linked? The variable is a string pointer (NSString *) and it has an address. – Phillip Mills Aug 10 '17 at 15:41
  • usually you can see the string when you look on it – yoc Aug 10 '17 at 15:43
  • Try changing `assign` to `strong`. – pckill Aug 10 '17 at 16:01
  • Thanx!!! looks good! what is the different? – yoc Aug 10 '17 at 16:14
  • @yoc: I previously said "(if you put assign, clearly that could crash)". Didn't you check it? https://stackoverflow.com/questions/2255861/property-and-retain-assign-copy-nonatomic-in-objective-c assign is generally used for non-object (primitive) properties. – Larme Aug 10 '17 at 18:53

1 Answers1

4

assign property attribute applies to primitive types, it does nothing with the object reference. When an object is assigned to this property, it is deallocated (if it is not retained by some other object), and all you have left is a pointer to the memory where it once was. Never use assign to store objects.

weak is similar to assign, with the only difference that when the object referenced by this property is deallocated (retain count reaches zero), it is set to nil, so you would never have a pointer to garbage memory.

strong retains the object and prevents it from being deallocated.

Your property should be declared like so:

@property (nonatomic, strong) NSString *cartItemComment;
pckill
  • 3,709
  • 36
  • 48