Why Can't I use Assign with an NSString?

Discussion in 'Mac Programming' started by BadWolf13, May 21, 2010.

  1. BadWolf13 macrumors 6502

    Joined:
    Dec 17, 2009
    #1
    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.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    from http://developer.apple.com/mac/libr...ceptual/objectivec/articles/ocProperties.html

    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
     
  3. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #3
    NSStrings are objects. You have to retain objects.
     
  4. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #4
    EDIT: Note to self: don't answer questions after 2AM.
     
  5. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #5
    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.
     
  6. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #6
    Yes, of course that is correct. Brain fart on my part.
     
  7. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #7
    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?
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX

Share This Page