Kochan 2.0: Question about synthesized accessors ( exercise 15.9...mainly)

Discussion in 'Mac Programming' started by mdeh, Feb 8, 2009.

  1. macrumors 6502

    Joined:
    Jan 3, 2009
    #1
    May I ask a few questions relating to this exercise.

    1) How would one store an instance variable like "playtime". As an NSString* ( eg "@1 hr 32 mins"?) or perhaps as a number object?

    2) We have been taught, up to now ( not yet at GC) how to release, for example, an addressCard with instance variables that have alloced memory.

    So, for instance, given a card with a single NSString *name ( for example) the dealloc method would be something like:

    Code:
    -(void) dealloc
    {
    [ theProperty release];
    [super dealloc];
    
    
    and the setter would be *something* like

    Code:
    -(void) setProp: (NSString *) aProp
    {
    [theProperty release];
    the Property = [ [ class alloc] initWithProp: aProp];
    
    etc etc.
    

    Now, using synthesized methods:

    I **think** that the above will do the same as the release and assign ( with new alloc) in the above code. But what about the release during the "dealloc" method. Will it take care of that?
    The reason I ask, is that the documentation says:

    If I have not articulated my exact issue, it is that I do not clearly understand it yet myself. Any insight is appreciated
     
  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Why would you use anything other than a double?
     
  3. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    Or an int/long int if sub-second precision is not needed.

    -Lee
     
  4. thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #4

    Thanks Lee.
    But that was almost a side question to the main issue.
    Thanks again.
     
  5. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #5
    No, it will not take care of deallocation for you. Synthesizing a property really just creates the property and setProperty: methods for you.

    If setter that is created (or that you write yourself) does a copy or retain, then you would need to release the object yourself in - dealloc.
     
  6. thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #6
    Eddie...thank you. So, it's nothing more than getter and setter methods!! ...synthesized!! DUH! :D

    Another quick question. The issue of "nonatomic". Is there a simple explanation - I know it is coming up in a couple of chapters...or is it something that requires an in depth look at. ( For instance, the documentation says:
    It also says some other seemlngly contradictory stuff, but if you think it is too detailed, I will wait.
     
  7. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #7
    So in a nutshell, "atomic" (which, btw is the default) means that the getters and setters that are generated are written so that they are thread safe. Meaning that multiple threads can be getting and setting your property and they will not get incomplete results.

    If you know only one thread will be calling these getters and setters, or if your application has some other mechanism already to deal with multiple threads accessing these particular objects, then you probably don't want the overhead of thread-safe getters and setters. So in those cases you have the ability to declare a property nonatomic.

    And so the getters and setters are smaller, simpler and faster because they don't need to worry about the thread safety issues.

    Hope that makes sense?
     
  8. thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #8

    yes it does...thanks.
     
  9. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #9
    Setter and getter functions are thread safe in the sense that if two threads call the setter function simultaneously, for example, then all your retain counts will be ok afterwards; the two setters will run one after the other and their code will not be mixed together with likely bad results. Still, the value stored by one of the setters will be lost, so you would still have to check your program logic carefully in a multi-threaded system.
     
  10. thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #10

    Thanks gnasher
     

Share This Page