mirror of
https://github.com/wnagrodzki/CocoaProgrammingGuidelines.git
synced 2025-07-01 22:34:56 +02:00
Removes section "Enumeration types contain invalid value"
This commit is contained in:
parent
a213c7f192
commit
0a36e99e68
2 changed files with 2 additions and 40 deletions
Binary file not shown.
|
@ -573,46 +573,6 @@ Forcing a subclass to provide an implementation of a method is accomplished by r
|
||||||
\end{codelisting}
|
\end{codelisting}
|
||||||
|
|
||||||
|
|
||||||
\subsection{Enumeration types contain invalid value}
|
|
||||||
|
|
||||||
The enumeration type equal to zero is considered as invalid. It protects instance variables from being initialized with a meaningful value when not intended.
|
|
||||||
|
|
||||||
\begin{codelisting}
|
|
||||||
typedef NS_ENUM(NSInteger, Enumeration) {
|
|
||||||
EnumerationInvalid,
|
|
||||||
EnumerationA,
|
|
||||||
EnumerationB,
|
|
||||||
EnumerationC,
|
|
||||||
};
|
|
||||||
\end{codelisting}
|
|
||||||
|
|
||||||
It also shields from false positives, when comparing against values returned by methods sent to nil pointer. Let's assume \inlinecode{EnumerationInvalid} does not exist.
|
|
||||||
|
|
||||||
\begin{codelisting}
|
|
||||||
@interface MyObject : NSObject
|
|
||||||
|
|
||||||
@property (assign, nonatomic) Enumeration enumeration;
|
|
||||||
|
|
||||||
@end
|
|
||||||
\end{codelisting}
|
|
||||||
|
|
||||||
If \inlinecode{myObject} pointer is \inlinecode{nil}, \inlinecode{performActionA} method is called, and it may not be expected.
|
|
||||||
|
|
||||||
\begin{codelisting}
|
|
||||||
MyObject * myObject = ...
|
|
||||||
|
|
||||||
if (myObject.enumeration == EnumerationA)
|
|
||||||
[self performActionA];
|
|
||||||
else if (myObject.enumeration == EnumerationB)
|
|
||||||
[self performActionB];
|
|
||||||
else
|
|
||||||
[self performActionC];
|
|
||||||
\end{codelisting}
|
|
||||||
|
|
||||||
\begin{importantlisting}
|
|
||||||
NS\_OPTIONS macro is used for declaring enumerations which are to be utilised as bit masks. Such enumerations do not contain the invalid value.
|
|
||||||
\end{importantlisting}
|
|
||||||
|
|
||||||
\subsection{The highest level of abstraction is used by default}
|
\subsection{The highest level of abstraction is used by default}
|
||||||
|
|
||||||
Lower levels are used only when more control is required. For example, instead using GCD:
|
Lower levels are used only when more control is required. For example, instead using GCD:
|
||||||
|
@ -747,7 +707,9 @@ static void * const navigationItemKey = (void *)&navigationItemKey;
|
||||||
navigationItem,
|
navigationItem,
|
||||||
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||||
}
|
}
|
||||||
|
\end{codelisting}
|
||||||
|
|
||||||
|
\begin{codelisting}
|
||||||
- (UINavigationItem *)navigationItem
|
- (UINavigationItem *)navigationItem
|
||||||
{
|
{
|
||||||
UINavigationItem * navigationItem = objc_getAssociatedObject(self, navigationItemKey);
|
UINavigationItem * navigationItem = objc_getAssociatedObject(self, navigationItemKey);
|
||||||
|
|
Loading…
Add table
Reference in a new issue