Objective-C Language

Topics related to Objective-C Language:

Getting started with Objective-C Language

Protocols

Blocks

Blocks are specified by the Language Specification for Blocks for C, Objective-C, C++ and Objective-C++.

Additionally, the Blocks ABI is defined by the Block Implementation Specification.

Categories

To avoid method name clashes, it is recommended to use prefixes (like xyz_ in the example). If methods with the same name exist, it is undefined which one will be used in the runtime.

Key Value Coding / Key Value Observing

Logging

NSArray

Classes and Objects

NSString

NSDictionary

NSDictionary

The NSDictionary class declares the programmatic interface to objects that manage immutable associations of keys and values. Use this class or its subclass NSMutableDictionary when you need a convenient and efficient way to retrieve data associated with an arbitrary key. NSDictionary creates static dictionaries, and NSMutableDictionary creates dynamic dictionaries. (For convenience, the term dictionary refers to any instance of one of these classes without specifying its exact class membership.)

A key-value pair within a dictionary is called an entry. Each entry consists of one object that represents the key and a second object that is that key’s value. Within a dictionary, the keys are unique. That is, no two keys in a single dictionary are equal (as determined by isEqual:). In general, a key can be any object (provided that it conforms to the NSCopying protocol—see below), but note that when using key-value coding the key must be a string (see Key-Value Coding Fundamentals). Neither a key nor a value can be nil; if you need to represent a null value in a dictionary, you should use NSNull.

NSDictionary is “toll-free bridged” with its Core Foundation counterpart, CFDictionaryRef. See Toll-Free Bridging for more information on toll-free bridging.

Low-level Runtime Environment

In order to use the Objective-C runtime, you need to import it.

#import <objc/objc.h>

NSArray

NSURL

Methods

Error Handling

Enums

NSData

Random Integer

Properties

NSDate

NSDate is a very simple value type, representing one exact moment in universal time. It does not contain information about time zones, daylight saving time, calendars, or locale.

NSDate is really only an immutable wrapper around an NSTimeInterval which is a double. There is no mutable subclass, as with some other value types in Foundation.

NSPredicate

NSMutableArray

NSRegularExpression

NSJSONSerialization

NSJSONSerialization is Available in iOS 5.0 and later An object that may be converted to JSON must have the following properties:

  • The top level object is an NSArray or NSDictionary.

  • All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.

  • All dictionary keys are instances of NSString.

  • Numbers are not NaN or infinity.

Memory Management

Singletons

NSCalendar

NSMutableDictionary

Multi-Threading

NSAttributedString

NSTimer

Structs

In Objective C, you should almost always use an object instead of a struct. However, there are still cases where using a struct is better, such as:

  • When you're going to be creating and destroying a lot of values of the (struct) type, so you need good performance and small memory usage
  • Structs are faster to create and use because when calling a method on an object, the method has to be determined at runtime
  • Structs take up less size because objects have an extra property isa, which holds their class

Subscripting

Basic Data Types

Unit testing using Xcode

Dependencies:

  • If application uses third party libraries or cocoa pods, then those libraries or pods are needed to be install for test as well.
  • Test class (Test Suit) extends XCTestCase.

Get brushed up before starting:

  • All test classes have two methods in common setUp & tearDown.

  • setUp runs before every testcase & tearDown after every testcase.

  • Test cases runs alphabetically.

  • In Test Driven Development, it is good to create dummy test data first.

  • Test case methods starts with "test" keyword.

  • Test methods accept no parameters & return no value.

Appendix:

There are several other methods for comparing the expected result & actual result out of an operation. Some of those methods are listed below:

  • XCTAssertNil(expression, comment) generates a failure if expression != nil.
  • XCTAssertNotNil(expression, comment) generates a failure if expression = nil.
  • XCTAssert(expression, comment) generates a failure if expression == false.
  • XCTAssertTrue(expression, comment) generates a failure if expression == false.
  • XCTAssertFalse(expression, comment) generates a failure if expression != false.
  • XCTAssertEqualObjects(expression1, expression2, comment) generates a failure if expression1 is not equal to expression2.
  • XCTAssertEqualObjects(expression1, expression2, comment) generates a failure if expression1 is equal to expression2.
  • XCTAssertNotEqual(expression1, expression2, comment) generates a failure if expression1 == expression2.
  • XCTAssertEqual(expression1, expression2, comment) generates a failure if expression1 != expression2.
  • XCTAssertGreaterThanOrEqual(expression1, expression2, comment) generates a failure when ( expression1 < expression2).

Fast Enumeration

NSSortDescriptor

Inheritance

NSTextAttachment

NSTextAttachment objects are used by the NSAttributedString class cluster as the values for attachment attributes. The objects you create with this class are referred to as text attachment objects, or when no confusion will result, as text attachments or merely attachments.

NSUrl send a post request

BOOL / bool / Boolean / NSCFBoolean

Protocols and Delegates

Protocols and Delegates are two related but different concept:

A Protocol is a interface a class can conforms to, meaning that class implements the listed methods.

A Delegate is typically an anonymous object that conforms to a protocol.

The application of Delegate called Delegation is a design pattern.

At one end we have the concept of Inheritance which creates a tight coupling between the subclass and its superclass whereas Delegation design pattern provides an alternative to avoid this tight coupling using which we can create a much looser relationship based on anonymous Delegate objects.

XML parsing

Declare class method and instance method

Predefined Macros

NSCache

Grand Central Dispatch

Continue and Break!

Format-Specifiers

Due to the nature of format-specifiers, if you wish to include the percentage symbol (%) in your string, you must escape it using a second percentage symbol.

Example:

int progress = 45;//percent
NSString *progressString = [NSString stringWithFormat:@"Progress: %i%%", (int)progress];

NSLog(progressString);//logs "Progress: 45%"

No Format Specifier for BOOL-type exists.

Common-use solutions include:

BOOL myBool = YES;
NSString *boolState = [NSString stringWithFormat:@"BOOL state: %@", myBool?@"true":@"false"];

NSLog(boolState);//logs "true"

Which utilizes a ternary operator for casting a string-equivalent.

BOOL myBool = YES;
NSString *boolState = [NSString stringWithFormat:@"BOOL state: %i", myBool];

NSLog(boolState);//logs "1" (binary)

Which utilizes an (int) cast for implanting a binary-equivalent.

NSObject

Modern Objective-C

NSUserDefaults