Attributes Operators Definition, Objective-C is the primary programming language you use when writing software for OS X and iOS. It’s a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime. Attributes Operators Definition in Objective-C inherits the syntax, primitive types, and flow control statements of C and adds syntax for defining classes and methods.
Rather than creating an entirely new class to provide minor additional capabilities over an existing class, it’s possible to define a category to add custom behavior to an existing class. You can use a category to add methods to any class, including classes for which you don’t have the original implementation source code.
Attributes Operators Definition, The majority of work in an Objective-C app occurs as a result of objects sending messages to each other. Often, these messages are defined by the methods declared explicitly in a class interface.
Integer types
// Char (1 byte for both 32-bit and 64-bit)
unsigned char anUnsignedChar = 255;
NSLog(@"char size: %zu", sizeof(char));
// Short (2 bytes for both 32-bit and 64-bit)
short aShort = -32768;
unsigned short anUnsignedShort = 65535;
NSLog(@"short size: %zu", sizeof(short));
// Integer (4 bytes for both 32-bit and 64-bit)
int anInt = -2147483648;
unsigned int anUnsignedInt = 4294967295;
NSLog(@"int size: %zu", sizeof(int));
// Long (4 bytes for 32-bit, 8 bytes for 64-bit)
long aLong = -9223372036854775808; // 64-bit
unsigned long anUnsignedLong = 18446744073709551615; // 64-bit
NSLog(@"long size: %zu", sizeof(long));
// Long Long (8 bytes for both 32-bit and 64-bit)
long long aLongLong = -9223372036854775808;
unsigned long long anUnsignedLongLong = 18446744073709551615;
NSLog(@"long long size: %zu", sizeof(long long));
// Single precision floating-point (4 bytes for both 32-bit and 64-bit)
float aFloat = 72.0345f;
NSLog(@"float size: %zu", sizeof(float));
// Double precision floating-point (8 bytes for both 32-bit and 64-bit)
double aDouble = -72.0345f;
NSLog(@"double size: %zu", sizeof(double));
// Extended precision floating-point (16 bytes for both 32-bit and 64-bit)
long double aLongDouble = 72.0345e7L;
NSLog(@"long double size: %zu", sizeof(long double));
Preprocessor Directives
Directive
Purpose
#define
Used to define constants or macros that are replaced by the compiler at runtime
#elif
An else if conditional statement
#else
An else conditional statement
#endif
An end if conditional statement
#error
Used to flag an error line in code
#if
An if conditional statement
#ifdef
An if defined conditional statement
#ifndef
An if not defined conditional statement
#import
Imports a header file. This directive is identical to #include, except that it won’t include the same file more than once
#include
Includes a header file
#pragma
Used for commenting code or inhibiting compiler warnings
#undef
Used to undefine and redefine macros
#warning
Used to flag a warning line in code
Classes and Protocols
Directive
Purpose
@class
Declares the names of classes defined elsewhere
@interface
Begins the declaration of a class or category interface
@implementation
Begins the definition of a class or category
@protocol
Begins the declaration of a formal protocol
@required
Declares the methods following the directive as required (default)
@optional
Declares the methods following the directive as optional. Classes implementing this protocol can decide whether to implement an optional method or not and should first check if the method is implemented before sending it a message.
@end
Ends the declaration/definition of a class, category, or protocol
Properties
Directive
Purpose
@property
Declares a property with a backing instance variable
@synthesize
Synthesizes a property and allows the compiler to generate setters/getters for the backing instance variable
@dynamic
Tells the compiler that the setter/getter methods are not implemented by the class itself but somewhere else, like the superclass
Errors
Directive
Purpose
@throw
Throws an exception
@try
Specifies a block of code to attempt
@catch
Specifies what to do if an exception was thrown in the @try block
@finally
Specifies code that runs whether an exception occurred or not
Visibility of Instance Variables
Directive
Purpose
@private
Limits the scope of instance variables specified below it to the class that declares it
@protected
Limits the scope of instance variables specified below it to declaring and inheriting classes
@public
Removes restrictions on the scope of instance variables specified below it
@package
Declares the instance variables following the directive as public inside the framework that defined the class, but private outside the framework (64-bit only)
Others
Directive
Purpose
@selector(method)
Returns the compiled selector that identifies a method
@protocol(name)
Returns the given protocol (an instance of the Protocol class)
@synchronized
Encapsulates code in a mutex lock to ensure that the block of code and the locked object can only be accessed by one thread at a time
@autoreleasepool
Replaces (and is 6 times faster than) the NSAutoreleasePool class
@encode(spec)
Yields a character string that encodes the type structure of spec
@compatibility_alias
Allows you to define an alias name for an existing class.
@defs(classname)
Yields the internal data structure of classname instances
@import
Imports a module and autolinks its framework (currently for Apple frameworks only)
Literals
Syntax
What it does
@”string”
Returns an NSString object
@28, @3.14, @YES
Returns an NSNumber object initialized with an appropriate class constructor, depending on the type
@[]
Returns an NSArray object
@{}
Returns an NSDictionary object
@()
Dynamically evaluates the boxed expression and returns the appropriate object literal based on its value
Arithmetic Operators
Operator
Purpose
+
Addition
–
Subtraction
*
Multiplication
/
Division
%
Modulo
Relational and Equality Operators
Operator
Purpose
==
Equal to
!=
Not equal to
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
Logical Operators
Operator
Purpose
!
NOT
&&
Logical AND
||
Logical OR
Compound Assignment Operators
Operator
Purpose
+=
Addition
-=
Subtraction
*=
Multiplication
/=
Division
%=
Modulo
&=
Bitwise AND
|=
Bitwise Inclusive OR
^=
Exclusive OR
<<=
Shift Left
>>=
Shift Right
Increment and Decrement Operators
Operator
Purpose
++
Addition
—
Subtraction
*=
Multiplication
/=
Division
%=
Modulo
&=
Bitwise AND
|=
Bitwise Inclusive OR
^=
Exclusive OR
<<=
Shift Left
>>=
Shift Right
Bitwise Operators
Operator
Purpose
&
Bitwise AND
|
Bitwise Inclusive OR
^
Exclusive OR
~
Unary complement (bit inversion)
<<
Shift Left
>>
Shift Right
Other operators
Operator
Purpose
()
Cast
? :
Conditional
&
Memory Address
*
Pointer
Property Attributes
Type
What it does
copy
Creates an immutable copy of the object upon assignment and is typically used for creating an immutable version of a mutable object. Use this if you need the value of the object as it is at this moment, and you don’t want that value to reflect any future changes made by other owners of the object.
assign
Generates a setter which assigns the value directly to the instance variable, rather than copying or retaining it. This is typically used for creating properties for primitive types (float, int, BOOL, etc).
weak
Variables that are weak can still point to objects but they do not become owners (or increase the retain count by 1). If the object’s retain count drops to 0, the object will be deallocated from memory and the weak pointer will be set to nil. It’s best practice to create all delegates and IBOutlet’s as weak references since you do not own them.
unsafe_unretained
An unsafe reference is similar to a weak reference in that it doesn’t keep its related object alive, but it won’t be set to nil if the object is deallocated. This can lead to crashes due to accessing that deallocated object and therefore you should use weak unless the OS or class does not support it.
strong
This is the default and is required when the attribute is a pointer to an object. The automatically generated setter will retain (i.e. increment the retain count of) the object and keep the object alive until released.
readonly
This only generates a getter method so it won’t allow the property to be changed via the setter method.
readwrite
This is the default and generates both a setter and a getter for the property. Often times, a readonly property will be publicly defined and then a readwrite for the same property name will be privately redefined to allow mutation of the property value within that class only.
atomic
This is the default and means that any access operation is guaranteed to be uninterrupted by another thread and is typically slower in performance to use.
nonatomic
This is used to provide quicker (but thus interruptable) access operations.
getter=method
Used to specify a different name for the property’s getter method. This is typically done for boolean properties (e.g. getter=isFinished)
setter=method
Used to specify a different name for the property’s setter method. (e.g. setter=setProjectAsFinished)
Extending Classes
Approach
Difficulty
Purpose
Inheritance
Easy
Used if all you want to do is inherit behavior from another class, such as NSObject
Category
Easy
Used if all you need to do is add additional methods to that class. If you also need to add instance variables to an existing class using a category, you can fake this by using associative references.
Delegation
Easy
Used to allow one class to react to changes in or influence behavior of another class while minimizing coupling.
Subclass
Can be difficult
Used if you need to add methods and properties to an existing class or if you want to inherit behavior from an existing class. Some classes are not designed to be subclassed.
Swizzle
Can be difficult
Swizzling allows you to replace a method in an existing class with one of your own making. This approach can lead to a lot of unexpected behavior, so it should be used very sparingly.
Attributes Operators Definition, Happy Coding 🙂
Discover more from mycodetips
Subscribe to get the latest posts sent to your email.