Saturday, July 2, 2011

Basic Objects in Objective-C

Source: Programming in Objective-C 2.0 (3rd Edition) by Stephen G. Kochan


Definition: A distinct occurrence of a class is an instance, and the actions that are performed on the instance are called methods.


A method can be applied to an instance of a class or to the class itself. For example, washing your car object applies to an instance, and finding out how many types of cars a manufacturer makes would apply to a class, so it would be a class method.



Applying methods to classes and instances:

Syntax:



When you want a person object to do something for you, you have to send them a message. This is analogous to when you want a class or an instance to perform some action, you send it a message; and the recipient of the message is called the receiver.


Definition: A message is the method that you would like to invoke on a particular class or instance.


Definition: A receiver is the recipient a message.





- The @interface section describes the class, its fields, and its methods.
- The @implementation section contains the code that implements the methods.


Definition: The data that objects in a class will contain are called instance variables.


When defining a new class:


1. You have to name its parent class
2. You have to specify what type of data is to be stored in the objects of this class
3. You have to define the types of methods than can be used when using a particular object from a class.


Syntax: A leading minus sign (-) in front of a method tell the Objective-C compiler that the method is an instance method. A leading plus sign (+) indicates a class method.


Syntax: When a method takes an argument you append a colon (:) to the method name.


Syntax: Methods
(method_type) (return_type) method_name (method_takes_argument/or not) (argument_type) argument_name;


Terminology: You declare methods in the @interface section and you define them in the @implementation section.


Syntax:


Good Programming Practice: When you create a new object, you have to allocate memory for it. When you are done with it, you have to release the memory it uses. When you start developing advanced applications, you can end up working with lots of objects that consume a lot of memory, and if not properly deallocated it will slow your program's execution.


Definition: Data encapsulation is the idea that you do not allow class methods to directly access instance variables, to protect the class from being manipulated by someone other than the class developer.
Definition: Data encapsulation is the brick wall between the programmer and the class developer.
Definition: Methods that set the values of instance variables are called setters.
Definition: Methods that retrieve the value of instance variables are called getters.
Definition: Methods that have access to the instance variables of a class are called accessor methods.


Big Idea: You cannot directly set or get the value of an instance variable from methods that were not written for the class. You have to write setter and getter methods to do so.


Resulting output:

1 comment :

good days said...


تعد خدمات الاول في المدينة المنورة من افضل الخدمات في مجال التنظيف ومكافحة الحشرات لانها تتميز بجودة المنظفات المستخدمة وامهر عماله تعمل على انهاء مهامها على اكمل وجه وبنظافه ترضى العميل بالاضافة الى خدمة مكافحة الحشرات ورش المبيدات المتميزة لدينا
مستودعات تخزين اثاث بالرياض
شركة غسيل شقق بالمدينة المنورة
شركة تنظيف كنب بالمدينة المنورة