Artificial Intelligence + NLP + deep learning‎ > ‎OS‎ > ‎iOS‎ > ‎OLD‎ > ‎

Objective-C



.h  header file
.m Objective-c or c code
.mm c++ code


The specification of a class in Objective-C requires two distinct pieces:
the interface (the class declaration and defines the public interface of the class)
As with C code, you define header files and source files to separate public declarations from the implementation details of your code. (You can put other declarations in your implementation file if they are part of the programmatic interfaces but are meant to be private.)
the implementation.


a class can have only one parent.
declaring a class called MyClass, which inherits from the base (or root) class, NSObject.


@interface MyClass : NSObject
int count;
id data;       //dynamically typed
NSString* name;

- (id) initWithString : (NSString)* aName;

+ (MyClass*) createMyClassWithString  :(NSString*) aName;
@end

If the class has custom functions, constants, or data types associated with its public interface, put their declarations outside the @interface . . . @end block.
 #import "MyClass.h"
@implementation MyClass
- (id) initWithString : (NSString)* aName{

}

+ (MyClass*) createMyClassWithString  :(NSString*) aName{

}
@end



Types:
Statically typed variables include the class name in the variable type declaration. Dynamically typed variables use the type id for the object instead.

Pointers

In Objective-C, object references must always be pointers.
You just have to remember to put an asterisk in front of the variable names for statically typed object declarations. The id type implies a pointer.


Methods

 Method Dispatch a message to the object that implements the method(method call): message expression
 - (void) insertObject: (id) atIndex:(int); [myArray  insertObject: anObject    atIndex: 0];

To dispatch a message, the runtime requires a message expression. A message expression encloses with brackets ([ and ]) the message itself (along with any required parameters) and, just inside the leftmost bracket, the object receiving the message.


                                          
- instance method
+ class method
The colon characters declare the presence of a parameter. If a method has no parameters, you omit the colon after the first (and only) signature keyword. A method’s actual name (insertObject:atIndex:) is a concatenation of all of the signature keywords, including colon characters.

When you want to call a method, you do so by sending a message to the object that implements the method. (Although the phrase "sending a message” is commonly used as a synonym for “calling a method,” the Objective-C runtime does the actual sending.) A message is the method name along with the parameter information the method needs (properly conforming to type). All messages you send to an object are dispatched dynamically, thus facilitating the polymorphic behavior of Objective-C classes. (Polymorphism refers to the ability of different types of objects to respond to the same message.) Sometimes the method invoked is implemented by a superclass of the class of the object receiving the message.

nested message dispatching
[[myAppObject theArray] insertObject:[myAppObject objectToInsert] atIndex:0];
Accessor methods (Getters/Setters) can be specified by dot
[myAppObject.theArray insertObject:myAppObject.objectToInsert atIndex:0];
dot notation for assignment
myAppObject.theArray = aNewArray;     ===     [myAppObject setTheArray:aNewArray];

Declared Properties and Accessor Methods

declared properties are a notational convenience for the declaration and implementation of accessor methods.

@property (nonatomic, copy) NSString *userName;
You declare public properties in the class header files; you declare private properties in a class extension in the source file. Properties of controller objects such as delegates and view controllers should typically be private.

You can also configure some options in parenthesis


@property (copy) MyModelObject *theObject;  // Copy the object during assignment.
@property (readonly) NSView *rootView;      // Declare only a getter method.
@property (weak) id delegate;               // Declare delegate as a weak reference

The instance variable has the same name as the property but with an underscore prefix (_). Your app should directly access an instance variable (instead of its property) only in methods for object initialization and deallocation.
 
If you want a different name for an instance variable, you can bypass autosynthesis and explicitly synthesize a property. Use the @synthesize compiler directive in the class implementation to ask the compiler to generate the accessor methods along with the specially named instance variable. For example:
@synthesize enabled = _isEnabled;
Incidentally, when you declare a property you can specify custom names for the accessor methods, typically to make the getter methods of Boolean properties follow a conventional form, as shown here:
@property (assign, getter=isEnabled) BOOL enabled; // Assign new value, change name of getter method


Blocks

anonymous functions that one can pass in as parameters of methods and functions or that can be returned from methods and functions. Blocks themselves have a typed parameter list and may have an inferred or a declared return type. assign a block to a variable and then call it just as you would a function.

int result = myBlock(4); // result is 28


A block shares data in the local lexical scope. This characteristic of blocks is useful because if you implement a method and that method defines a block, the block has access to the local variables and parameters of the method (including stack variables) as well as to functions and global variables, including instance variables. This access is read-only, but if a variable is declared with the __block modifier, its value can be changed within the block. Even after the method or function enclosing a block has returned and its local scope has been destroyed, the local variables persist as part of the block object as long as there is a reference to the block.

...........

Protocols

the UIApplication class delivers those notifications by calling specific methods of its assigned delegate object.
An object that implements the methods of the UIApplicationDelegate protocol can receive those notifications and provide an appropriate response.
You specify in the interface block that your class conforms to, or adopts, a protocol by putting the name of the protocol in angle brackets (<...>) after the name of the class from which it inherits.
@interface HelloWorldViewController : UIViewController <UITextFieldDelegate> {
The declaration of a protocol looks similar to that of a class interface, with the exceptions that protocols do not have a parent class and they do not define instance variables (although they can declare properties).
@protocol MyProtocol
- (void)myProtocolMethod;
@end
and protocols can specify both required and optional methods.

Categories

enclosing the name of the category in parentheses. extend the interface of a class without having to subclass it.
use categories as a means for grouping related method declarations within a header file.
Use an anonymous category aka class extension to declare private properties and private methods in the implementation (.m) file. A class extension is empty parentheses.
@interface MyAppDelegate () 
    @property (strong) MyDataObject *data; 
@end


self is equivalent to this.    super == super





































Comments