Articles Tagged 'Object-Oriented'

Objective-C: expose properties in a class

I would like to show and discuss some examples on how to add and manipulate properties in Objective-C class. : A classic example, precisely, is the following, in the definition of our interface class defines two properties nome and cognome :

1
2
3
4
5
6
7
8
9
10
11
/ / MyClass.h
# Import <Foundation/Foundation.h>

NSObject { @ Interface MyClass: NSObject {
nome; NSString * name;
cognome; NSString * name;
}

retain ) NSString * nome; @ Property (retain) NSString * name;
retain ) NSString * cognome; @ Property (retain) NSString * name;
@ End

e setter usati rispettivamente per leggere ed impostare le nostre due proprietà: In the implementation file the statement insert @synthesize so that Xcode will produce for us the methods getter and setter , respectively, used to read and set our two properties:

1
2
3
4
5
6
7
8
/ / MyClass.m
# Import "MyClass.h"

@ Implementation MyClass

@ Synthesize name, surname;

@ End

, possiamo scrive: When you're going to use our class MyClass , that is when istanziaremo an object of type MyClass , we can write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/ / Any other class, as AppDelegate
/ / In the file. H
# Import <UIKit/UIKit.h>
# Import "MyClass.h"

@ Class TestViewController;

NSObject <UIApplicationDelegate> { @ Interface TesAppDelegate: NSObject {<UIApplicationDelegate>
UIWindow * window;
* TestViewController viewController;

MyClass * myClass;
}

/ / In the file. M
MyClass alloc ] ; myClass = [MyClass alloc];
"Giovambattista" ; miaClasse.nome @ = "Giovambattista";
"miaClasse.nome = %@" , miaClasse.nome ) ; NSLog (@ "miaClasse.nome =% @", miaClasse.nome);

Or, which is equivalent to:

1
2
3
/ / Always in the file. M
@ "Undolog" ] ; [SetNome myClass: @ "Undolog"];
"miaClasse.nome = %@" , [ miaClasse nome ] ) ; NSLog (@ "miaClasse.nome =% @", [myClass name]);

So far so good. However, it could mislead the equivalence of the "variable" internal (ivar) with the name of the property itself. To understand the difference, propose again the same as doing without, this time, the @synthesize . . Now, therefore, we should take care to write the methods getter and setter . To further emphasize the differences, will rename the internal variables by inserting an underscore before the name. But we see the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Import <Foundation/Foundation.h>

NSObject { @ Interface MyClass: NSObject {
_nome; NSString * _Name;
_cognome; NSString * _cognome;
}

NSString * ) nome; // get - ( NSString *) name; / / get
NSString * ) cognome; // get - ( NSString *) name; / / get

void ) setNome : ( NSString * ) stringaIngresso; // set - (Void) setNome: ( NSString *) stringaIngresso; / / set
void ) setCognome : ( NSString * ) stringaIngresso; // set - (Void) setCognome: ( NSString *) stringaIngresso; / / set

@ End

. Unlike the previous pointers to internal variables (incapsultate) have become _nome and _cognome . . @property è scomparso, in quanto non serve più. In addition there are four definitions of methods that represent our get and set . @property has disappeared, as no longer needed.
We see the implementation file MyClass.m :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Import "MyClass.h"

@ Implementation MyClass

/ / Get a "name"
NSString * ) nome { - ( NSString *) name {
_Name return;
}
/ / Set to "name"
void ) setNome : ( NSString * ) stringaIngresso { - (Void) setNome: ( NSString *) {stringaIngresso
_Name = stringaIngresso;
}

/ / Get for "surname"
NSString * ) cognome { - ( NSString *) name {
_cognome return;
}
/ / Set for "surname"
void ) setCognome : ( NSString * ) stringaIngresso { - (Void) setCognome: ( NSString *) {stringaIngresso
_cognome = stringaIngresso;
}

@ End

Written as a class can be used exactly like the previous one, namely:

1
2
3
4
5
6
7
8
MyClass alloc ] ; myClass = [MyClass alloc];
"Giovambattista" ; miaClasse.nome @ = "Giovambattista";
"miaClasse.nome = %@" , miaClasse.nome ) ; NSLog (@ "miaClasse.nome =% @", miaClasse.nome);

/ / Or, which is equivalent to:

@ "Undolog" ] ; [SetNome myClass: @ "Undolog"];
"miaClasse.nome = %@" , [ miaClasse nome ] ) ; NSLog (@ "miaClasse.nome =% @", [myClass name]);

e set , evidenziando – anche con l'aggiunta dell'underscore – le differenze tra il nome della proprietà e la sua ivar interna _nome . At the level of educational neglect @synthesize forced us to write "their own" methods get and set , highlighting - even with the addition dell'underscore - the differences between the property name and its internal ivar _nome .
permette un reale controllo del dato prima della sua impostazione (o prima della sua lettura) e quindi un reale incapsulamento per proteggere la variabile interna. At the functional level the personal use of the methods get and set allows real control of the data before its setting (or prior to its reading) and then a real encapsulation to protect the internal variable.
For example it would be possible to prevent the passage of empty strings to the property nome :

1
2
3
4
void ) setNome : ( NSString * ) stringaIngresso { - (Void) setNome: ( NSString *) {stringaIngresso
stringaIngresso == @ "" ) stringaIngresso = @ "senza nome" ; if (@ stringaIngresso == "") @ stringaIngresso = "no name";
_Name = stringaIngresso;
}

Further variation

If you want to use the internal variables with the underscore in front (who rpoviene Adobe Actionscript could be used as well) is not necessary to abandon the use of the directive @synthesize . Xcode makes it possible to "merge" the methods mentioned above:

1
2
_nome; @ Synthesize name = _Name;
_cognome; @ Synthesize name = _cognome;

. In doing so we could use the pointer to the internally _nome , "summed up" - to the outside - as a property nome . e setter , è vero anche che lo fa solo se non li trova, quindi se desiderate “implementare” un vostro metodo di getter e/o setter potete farlo anche se avete usato la direttiva @synthesize . Moreover, it is true that the use of @synthesize produces automatic generation methods (messages) of getter and setter , it is also true that if he does not find them, so if you want to "implement" a method for your getter and / or setter you can do this even if you used the directive @synthesize .

Memory allocations

In the examples above I have omitted some important details for a real implementation. First of all, I have not shown any method init() , useful for object initialization and default values. Moreover, there is the addition of a method dealloc() :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/ / File MyClass.m
# Import "MyClass.h"

@ Implementation MyClass

id ) init { - (Id) init {
self = [ super init ] ) { if (self = [super init]) {
"Nome preimpostato" ; _Name @ = "Preset Name";
"Cognome preimpostato" ; _cognome @ = "Preset Name";
}
return self;
}

void ) dealloc { - (Void) dealloc {
; [_Name Release];
; [_cognome Release];
; [Super dealloc];
}

NSString * ) nome { - ( NSString *) name {
_Name return;
}
void ) setNome : ( NSString * ) stringaIngresso { - (Void) setNome: ( NSString *) {stringaIngresso
stringaIngresso == @ "" ) stringaIngresso = @ "senza nome" ; if (@ stringaIngresso == "") @ stringaIngresso = "no name";
_Name = stringaIngresso;
}

NSString * ) cognome { - ( NSString *) name {
_cognome return;
}
void ) setCognome : ( NSString * ) stringaIngresso { - (Void) setCognome: ( NSString *) {stringaIngresso
_cognome = stringaIngresso;
}

@ End

, etc… In the future we will then see the details on the properties readonly , retain , etc ... :)

Continued ...

From Actionscript to Objective-C

I thought it might be useful to those who have recently approached the development of applications for Apple iPhone, compare Adobe ActionScript - the language used in Adobe Flash and Adobe Flex, more common among the neo-programmers - and Objective-C language used by Apple to develop its applications. Objective-C is in effect an object-oriented language in the pure sense, not that it is not actionscript, but Objective-C is definitely a plus because it is an extension of ANSI C and its syntax is a mix between C / C + + and Smalltalk, is a true OO (Object-Oriented Language).

Continued ...

Overloading

Overloading is a very useful feature of some programming languages ​​to objects. However tuti OO languages ​​do not support it, and some of the "implementing" or limited to or different. In general, functions or methods, overloading allows you to create two or more functions / methods that have the same name but accept different parameters, for example:

Continued ...

Development languages

I "revived" this article of mine wrote a little 'of years ago. I slightly revised, updating a little bit here and there, but I think it is still relevant and interesting.

INTRODUCTION

What is a development language? A computer, aka PC (Personal Computer), has a personal language. This language is called machine code, to mean that every machine, so any computer (PC compatible, Apple, Unix, etc ...), has a unique and proprietary. The programs that we see "run" on our PCs are mainly carried out by the mysterious object called a microprocessor. This is the heart, the intelligent module, each computer. In fact, only one application is not running but is supported by the microprocessor, so to speak, to what is called an operating system: a software layer supplied by the manufacturer of the machine (see, for example, the Apple Macintosh).

Continued ...

Classes, Objects and Instances

I noticed often confusion when it comes to classes, objects and instances. Those who are not very educated on the object-oriented programming often confuses the true meaning of these terms. I knew, however, that there are two schools of thought regarding the definition of classes and objects. I like the "school" that indicates the class definition as a possible subject, and therefore, as an instance of the object.

It seems trivial, but I've - talking with others - to be in "conflict" (so to speak) and then fall into confusion, when using these terms, starting from the assumption that if anything the "other" just as we intend them .

I see it in this way, a class is a definition! It is precisely defines a class of possible objects. The class is the set of methods and properties (if you want we can also add events - what else ... not only that special methods) that will own the object.

For example, when we write in Actionscript, or any other object-oriented language:

1
2
3
4
class MyClass {
MiaClass function () {}
function myMethod () {}
}

We have defined a class and not an object. In the limit we have "defined" a "possible" object. We could even argue, and rightly so, that the object exists at runtime while the Class not (in truth there are dynamic classes that can be defined - and then used to create objects - even at runtime). Exclude static classes, of course, that - eventually - are nothing more than sub-instances (or instances hidden) and then real objects.

But when we have:

1
MiaClasse = new MiaClasse ( ) ; var MyObject: MyClass = new MyClass ();

! Here mioOggetto is an instance of MiaClasse() ! . That mioOggetto is a subject - in fact - of type MiaClasse() .

. As a result, their philosophy to objects, objects of type MiaClasse() I can have as many as I want, which can not be - the very definition - of MiaClasse() . For example, if the report is true and it makes sense:

1
2
3
4
5
MiaClasse = new MiaClasse ( ) ; var mioOggetto_1: MyClass = new MyClass ();
MiaClasse = new MiaClasse ( ) ; var mioOggetto_2: MyClass = new MyClass ();
MiaClasse = new MiaClasse ( ) ; var mioOggetto_3: MyClass = new MyClass ();
...
MiaClasse = new MiaClasse ( ) ; var mioOggetto_n: MyClass = new MyClass ();

It makes no sense:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class MyClass {
MiaClass function () {}
MioMetodo_2 function () {}
}

class MyClass {
MiaClass function () {}
MioMetodo_2 function () {}
}

class MyClass {
MiaClass function () {}
MioMetodo_3 function () {}
}

Object Instance and, therefore, coincide and are used alternately for the same meaning in different contexts.

Probably not much anyone cares ... the question of completeness of requirements ... :)

Continued ...

Write good OO code in Adobe Flash

Here are some tips on how to write good code Object Oriented (OO) in Adobe Flash, especially for those still using version MX waiting to go to CS3.

Organize classes folders

First of all, the organization of classes makes the job significantly easier code maintenance. You can also create a real library you can reuse in other projects. Flash uses a classification related to the filesystem, and then organizing them into folders will also be reflected on the import of classes. For example, if we create the sequence of folders "mylibrary / graphics / plot" and insert our own ActionScript class "PlotClass.as", when we should use to import the class:

1
grafica . plot . PlotClass ; mylibrary imports. graphics. plot. PlotClass;

If the library (folder) "MyLibrary" is not in your movie folder or project, use the Flash publish settings to select the path:

Setting percoroso libraries

Continued ...