Attributes Operators Definition in Objective-C

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));

Fixed width integer types

// Exact integer types
int8_t aOneByteInt = 127;
uint8_t aOneByteUnsignedInt = 255;
int16_t aTwoByteInt = 32767;
uint16_t aTwoByteUnsignedInt = 65535;
int32_t aFourByteInt = 2147483647;
uint32_t aFourByteUnsignedInt = 4294967295;
int64_t anEightByteInt = 9223372036854775807;
uint64_t anEightByteUnsignedInt = 18446744073709551615;

// Minimum integer types
int_least8_t aTinyInt = 127;
uint_least8_t aTinyUnsignedInt = 255;
int_least16_t aMediumInt = 32767;
uint_least16_t aMediumUnsignedInt = 65535;
int_least32_t aNormalInt = 2147483647;
uint_least32_t aNormalUnsignedInt = 4294967295;
int_least64_t aBigInt = 9223372036854775807;
uint_least64_t aBigUnsignedInt = 18446744073709551615;

// The largest supported integer type
intmax_t theBiggestInt = 9223372036854775807;
uintmax_t theBiggestUnsignedInt = 18446744073709551615;

Floating Point

// 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

DirectivePurpose
#defineUsed to define constants or macros that are replaced by the compiler at runtime
#elifAn else if conditional statement
#elseAn else conditional statement
#endifAn end if conditional statement
#errorUsed to flag an error line in code
#ifAn if conditional statement
#ifdefAn if defined conditional statement
#ifndefAn if not defined conditional statement
#importImports a header file. This directive is identical to #include, except that it won’t include the same file more than once
#includeIncludes a header file
#pragmaUsed for commenting code or inhibiting compiler warnings
#undefUsed to undefine and redefine macros
#warningUsed to flag a warning line in code

Classes and Protocols

DirectivePurpose
@classDeclares the names of classes defined elsewhere
@interfaceBegins the declaration of a class or category interface
@implementationBegins the definition of a class or category
@protocolBegins the declaration of a formal protocol
@requiredDeclares the methods following the directive as required (default)
@optionalDeclares 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.
@endEnds the declaration/definition of a class, category, or protocol

Properties

DirectivePurpose
@propertyDeclares a property with a backing instance variable
@synthesizeSynthesizes a property and allows the compiler to generate setters/getters for the backing instance variable
@dynamicTells the compiler that the setter/getter methods are not implemented by the class itself but somewhere else, like the superclass

Errors

DirectivePurpose
@throwThrows an exception
@trySpecifies a block of code to attempt
@catchSpecifies what to do if an exception was thrown in the @try block
@finallySpecifies code that runs whether an exception occurred or not

Visibility of Instance Variables

DirectivePurpose
@privateLimits the scope of instance variables specified below it to the class that declares it
@protectedLimits the scope of instance variables specified below it to declaring and inheriting classes
@publicRemoves restrictions on the scope of instance variables specified below it
@packageDeclares the instance variables following the directive as public inside the framework that defined the class, but private outside the framework (64-bit only)

Others

DirectivePurpose
@selector(method)Returns the compiled selector that identifies a method
@protocol(name)Returns the given protocol (an instance of the Protocol class)
@synchronizedEncapsulates 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
@autoreleasepoolReplaces (and is 6 times faster than) the NSAutoreleasePool class
@encode(spec)Yields a character string that encodes the type structure of spec
@compatibility_aliasAllows you to define an alias name for an existing class.
@defs(classname)Yields the internal data structure of classname instances
@importImports a module and autolinks its framework (currently for Apple frameworks only)

Literals

SyntaxWhat it does
@”string”Returns an NSString object
@28, @3.14, @YESReturns 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

OperatorPurpose
 +Addition
 –Subtraction
*Multiplication
/Division
%Modulo

Relational and Equality Operators

OperatorPurpose
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Logical Operators

OperatorPurpose
!NOT
&&Logical AND
||Logical OR

Compound Assignment Operators

OperatorPurpose
+=Addition
-=Subtraction
*=Multiplication
/=Division
%=Modulo
&=Bitwise AND
|=Bitwise Inclusive OR
^=Exclusive OR
<<=Shift Left
>>=Shift Right

Increment and Decrement Operators

OperatorPurpose
++Addition
Subtraction
*=Multiplication
/=Division
%=Modulo
&=Bitwise AND
|=Bitwise Inclusive OR
^=Exclusive OR
<<=Shift Left
>>=Shift Right

Bitwise Operators

OperatorPurpose
&Bitwise AND
|Bitwise Inclusive OR
^Exclusive OR
~Unary complement (bit inversion)
<<Shift Left
>>Shift Right

Other operators

OperatorPurpose
()Cast
? :Conditional
&Memory Address
*Pointer

Property Attributes

TypeWhat it does
copyCreates 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.
assignGenerates 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).
weakVariables 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_unretainedAn 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.
strongThis 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.
readonlyThis only generates a getter method so it won’t allow the property to be changed via the setter method.
readwriteThis 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.
atomicThis 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.
nonatomicThis is used to provide quicker (but thus interruptable) access operations.
getter=methodUsed to specify a different name for the property’s getter method. This is typically done for boolean properties (e.g. getter=isFinished)
setter=methodUsed to specify a different name for the property’s setter method. (e.g. setter=setProjectAsFinished)

Extending Classes

ApproachDifficultyPurpose
InheritanceEasyUsed if all you want to do is inherit behavior from another class, such as NSObject
CategoryEasyUsed 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.
DelegationEasyUsed to allow one class to react to changes in or influence behavior of another class while minimizing coupling.
SubclassCan be difficultUsed 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.
SwizzleCan be difficultSwizzling 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.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top