Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BadWolf13

macrumors 6502
Original poster
Dec 17, 2009
271
0
I get an EXC_BAD_ACCESS error if I try to declare a property for an NSString as (readwrite, assign). I don't understand why this is a problem.
 
from http://developer.apple.com/mac/libr...ceptual/objectivec/articles/ocProperties.html

assign
Specifies that the setter uses simple assignment. This is the default.
You typically use this attribute for scalar types such as NSInteger and CGRect, or (in a reference-counted environment) for objects you don’t own such as delegates.
retain and assign are effectively the same in a garbage-collected environment.

so is NSString scalar? Would you just assign the pointer to an NSString if you needed to store the value for a while? How are you using the property? Can you post the code that has the behavior you describe?

-Lee
 
Technically you don't have to retain them; you just have to make sure they are non-nil before you use them.

So, if you use a typical NSString method (e.g., [aString stringByAppendingString:anotherString]), in a reference-counted environment, you will have a non-nil pointer to a string that will eventually be deallocated (autoreleased) - unless you retain it or use +alloc/-init... type creation at some point. This is the kind of thing that results in bad access exceptions - sending a message to nil does not result in an exception.
 
So, if you use a typical NSString method (e.g., [aString stringByAppendingString:anotherString]), in a reference-counted environment, you will have a non-nil pointer to a string that will eventually be deallocated (autoreleased) - unless you retain it or use +alloc/-init... type creation at some point. This is the kind of thing that results in bad access exceptions - sending a message to nil does not result in an exception.

Yes, of course that is correct. Brain fart on my part.
 
Thank you all, however two questions remain. One, I was able to use Assign with an object that I created, and it doesn't give me any error. Second, I'm using Garbage Collector, so I was ignoring all retain, release and autorelease commands. Is it still a good idea to use Retain for the property declaration?

Sorry, actually in another part of this same program, it lets me use Assign for an NSString and an NSMutableArray, as well as my created object. Is it an order of operations thing? Like it's trying to use the object before it has a value, and so gives the EXC_BAD_ACCESS error?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.