One of the advantages of the Property list files, which are nothing more than text files that follow the XML standard, is that it can be instantly transformed into objects (such as arrays or dictionary) Objective-C. When you create a file Property list:

Continued ...
If you are to develop in an environment where it is impossible to use debugging tools such as FireBug , such as the Apple iPad simulator Xcode, it can become frustrating to identify problems, one all wrong access to the properties of an object. Here the use of the works alert() becomes crucial!
Continued ...
In Objective-C we have two ways widely used for receiving and sending messages between classes: the notifications and delegates. The difference between the two, as well as being at the implementation level, substantially depends on "how much" - objects - can receive a message. First let me show you how does the concept of delegate.
Continued ...
Even in the most simple tutorial you can meet the use of protocols. It will certainly happen to many to use in your view controller a protocol, inputting, next to the interface definition, a statement similar to:
1 2 3
| UIViewController <uiwebviewdelegate> { @ Interface MyViewController: UIViewController {<uiwebviewdelegate> ... } |
Continued ...
I would like to show and discuss some examples on how to add and manipulate a property in Objective-C class. : A classic example, precisely, is as follows, in the definition of our class interface we define two properties nome and cognome :
e setter usati rispettivamente per leggere ed impostare le nostre due proprietà: In the implementation file we insert the declaration @synthesize so that Xcode produce for us the methods getter and setter used to get and set up our two properties:
1 2 3 4 5 6 7 8
| / / MyClass.m # Import "MyClass.h"
@ Implementation MyClass
@ Synthesize name, last name;
@ End |
, possiamo scrive: When you're going to use our class MyClass , ie 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" ] ; [MyClass setNome: @ "Undolog"]; "miaClasse.nome = %@" , [ miaClasse nome ] ) ; NSLog (@ "miaClasse.nome =% @", [myClass name]); |
So far so good. However, it could mislead the equivalence of "variabli" 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 us to write the methods getter and setter . To further underline the differences, rinominerò the internal variables by inserting an underscore in front of the name. But let's 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 |
A class so written can be used exactly as 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" ] ; [MyClass setNome: @ "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 "yourself" methods get and set , highlighting - even with the addition dell'underscore - the difference between the name of the property and its ivar internal _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 a functional level the use of personal methods get and set allows real control of the data before its setting (or before reading it) 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 variant
If you wish to use the internal variables with the underscore in front (who rpoviene from Adobe Actionscript could be used as well) do not need to abandon the use of the directive @synthesize . Xcode makes it possible to "merge" the methods described above:
1 2
| _nome; @ Synthesize name = _Name; _cognome; @ Synthesize name = _cognome; |
. By doing so we could use the pointer to 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 . In addition, if it is true that the use of @synthesize produces the automatic generation methods (messages) of getter and setter , it is also true that it only does so if you do not find them, so if you want to "implement" a method for your getter and / or setter can do it even if you used the directive @synthesize .
Memory allocations
In the examples above I omitted some important details for a real implementation. First of all, I have not presented any method init() , useful for object initialization and default values. Furthermore, it lacks 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 see the details about the property readonly , retain , etc ... 
Continued ...
In Post Extending MovieClip in Adobe Flash MX had shown some techniques to extend a MovieClip. In particular I said that the use of MovieClip.prototype not permit the extension of Properties to but only of methods:
[...] Two important limitations of this technique are:
- Can not be applied to all objects exposed by Flash
- They can be "added" only methods with no property [...]
Indeed, it is possible, with an extra step, dynamically add properties also using MovieClip.prototype . , infatti, Flash permetteva l'aggiunta di proprietà (in lettura/scrittura o solo lettura) tramite il metodo addProperty() . Before the introduction of function get and function set , in fact, Flash allowed the addition of property (read / write or read only) via the addProperty() . In practice, this results in the invocation of the method addProperty() and the definition of two getter and setter functions. The setter can be null so as to create read-only properties. For example if we wanted to extend all the MovieClip a new property _alpha able to add animation, just write the following code:
1 2 3 4 5 6 7
| : Number { _get_alpha function (): Number { this . _alpha ) ; return (this. _alpha); } v : Number ) : Void { _set_alpha function (v: Number ): Void { this , "_alpha" , Strong . easeOut , this . _alpha , v , 1 , true ) ; new Tween (this, "_alpha", Strong. easeOut, this. _alpha, v, 1, true); } prototype . addProperty ( "_alpha_tween" , _get_alpha , _set_alpha ) ; MovieClip . prototype. addProperty ("_alpha_tween" _get_alpha, _set_alpha); |
From this moment on, if we have a symbol "miosimbolo_mc" we can use this new property:
1
| ; miosimbolo_mc. _alpha_tween = 50; |
. What you can do instead is to overwrite the existing properties, which is why I used _alpha_tween instead of _alpha . Here, then, a good reason to use it anyway Classes 2.0 to extend - and result - any MovieClip.
Continued ...
Latest Comments
Giovambattista Fazioli : @ ale: As shown @ Kevin see on GitHub repo: https://github.com/gfazioli/Ch roma-Key
Giovambattista Fazioli : @ Kevin: See https://github.com/gfazioli/Ch roma-Key
Kevin : Very nice example - would like to see the. fla too!
Ludovica : Hello! I'll explain my doubt. When I write a post not add pictures in the article (if so ...
Marco : hello @ Giovan Battista Fazioli, thanks for all the explanations of this excellent guide. I have a question to ...