• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Objective-C questions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Objective-C questions


  • Subject: Re: Objective-C questions
  • From: Alastair Houghton <email@hidden>
  • Date: Thu, 23 Aug 2007 11:20:09 +0100

On 22 Aug 2007, at 17:30, Shawn Erickson wrote:

On 8/22/07, Daniel Child <email@hidden> wrote:

Your list of advice brings up host of questions. Specifically, (in
reference to your points)

#1 (always use accessors)
Would you recommend using accessors even for primitive instance
variables (int, BOOL, etc.), where there is no need to retain, copy,
etc.

The fact that you store them internally as a primitive instance variable currently doesn't mean that in the future you may store them a different way. If you provide accessors as the interface to get at the data then you have more freedom to change your implementation in the future without affecting client code.

Indeed, though it's worth saying that people differ on whether they use accessors or not in implementations, at least for variables that have been defined by the subclass they're implementing.

There is a trade-off between performance and maintainability/ extendability;
sometimes you don't need the latter but need the former and vice-versa.


#5 - #6 (use immutable objects whenever possible) Why the emphasis on
immutable objects? Simply because they are more efficient?

They usually will be more efficient when accessed, etc. but obviously they wont be more efficient if they need to be modified.

They also tend to be thread-safe, whereas mutable objects generally aren't.


The important thing to consider is what you care about when you take
ownership, ask yourself... Do I want the same object state going
forward in time as when I first got the object (if so copy it, if not
retain it).

A good example of this is that you often find you want to -copy NSString arguments e.g. to setters and/or initialiser methods.

#8 (use a class accessor for class globals etc.)
???? I have no idea how to do 8. I expect to need plenty of class-
related constants. For example, my singleton class (a string parser
to parse input into syllables) has a list of values against which
input text is to be checked. I stored those values in an NSSet which
is one of the instance variables. You are recommending something else?

In Objective-C, sometimes people don't bother creating explicit singletons
where they might be more inclined to in some other OO languages.


The reason is that classes themselves are already singleton objects, and
often a singleton is used because it's there to manage a group of objects
of a particular class. Additionally, ObjC allows you to override class
methods, which isn't the case in some other languages (C++, for instance).


In such cases, the variables that would have been instance variables of
the singleton generally become static variables in the ObjC file containing
the implementation of the class. You might then have class method accessors
to change them.


To give a concrete example, take a look at NSBezierPath. It has a load of
accessors for the default winding rule, line cap and join styles, line
width, etcetera, not to mention a number of special-purpose convenience
constructors. As a result, in some other languages, it *might* have been
implemented as a BezierPathFactory singleton with a separate BezierPath
class.


The more conventional singleton pattern is definitely still used though;
e.g. in NSFontManager or NSWorkspace.

Kind regards,

Alastair.

p.s. Please don't send messages without a subject line to the list. This
being a high-volume list, not everyone has time to read and/or reply to
every message; nor is everyone interested in every subject brought up.
Using the subject line helps with that a lot.


--
http://alastairs-place.net


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: (From: Daniel Child <email@hidden>)
 >Re: (From: "Shawn Erickson" <email@hidden>)

  • Prev by Date: Re: Drawing text with alpha
  • Next by Date: Is this good programming?
  • Previous by thread: Re: Re:
  • Next by thread: NSInvocation in a sheet
  • Index(es):
    • Date
    • Thread