Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think it is bad design, no, but there are certain circumstances where one may be considered more appropriate than the other. The key questions are:</p> <ul> <li>Does this method belong to a class?</li> <li>Is this method worth adding to a class?</li> </ul> <p>A class is something that is self-contained and reusable. For the method in your example, I would be tempted to answer "Yes, it does/is," because it is something specific to <code>NSString</code> and is a method you (presumably) want to use fairly often. Its parameters are also of type <code>NSString</code>. I would therefore use the message form in a class extension and <code>#import</code> the extension when you need it.</p> <p>There are two situations (off the top of my head) where this is not really appropriate. Firstly is the situation where the method interacts specifically with other entities outside of the 'main class'. Examples of this can be found near the bottom of Apple's <code>NSObjcRuntime.h</code> file. These are all standard C functions. They don't really belong to a specific class.</p> <p>The second situation to use a standard C function is when it will only be used once (or very few times) in a very specific circumstance. <code>UIApplicationMain</code> is the perfect example, and helper methods for a specific <code>UIView</code> subclass's <code>-drawRect:</code> method also come to mind.</p> <p>A final point on efficiency. Yes, selector lookup is fractionally slower standard C calls. However, the runtime (Apple's at least, can't comment on GCC's) does use a caching system so that the most commonly sent messages quickly gravitate to the 'top' of the selector table.</p> <p><em>Disclaimer:</em> This is somewhat a question of a style and the above recommendations are the way I would do it as I think it makes code more organised and readable. I'm sure there are other equally valid ways to structure/interleave C and Objective-C code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload