User Tools

Site Tools


ios-labs-s14:intermediate-scope

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ios-labs-s14:intermediate-scope [2014/02/25 10:42]
mbarboi created
ios-labs-s14:intermediate-scope [2014/02/26 10:42]
mbarboi
Line 1: Line 1:
 =====Variable Declaration and Access===== =====Variable Declaration and Access=====
 The location of a variable declaration affects its scope, its lifetime, and the way in which you access it.  The location of a variable declaration affects its scope, its lifetime, and the way in which you access it. 
- 
-UNDER CONSTRUCTION 
  
 Common declaration types Common declaration types
Line 11: Line 9:
 Note that other types of declarations exist, these are the most common. ​ Note that other types of declarations exist, these are the most common. ​
  
-The following are descriptions of each type. Note that each example will use a variable of type NSString ​named "myString."+The following are descriptions of each type. Note that each example will use a variable of type NSArray ​named "arr" ​in a class named MyClass, a subclass of NSObject. ​
  
 ====Local Variables==== ====Local Variables====
Line 18: Line 16:
 <​code>​ <​code>​
 - (void) myMythodName { - (void) myMythodName {
 +     ​NSArray*arr = [[NSArray alloc] init]; 
 +     ​[arr.addObject:​SOMEOBJECT];​
 } }
 </​code>​ </​code>​
  
 ====[Private] Instance Variables==== ====[Private] Instance Variables====
 +Scope: the class in which the variable is declared. Not publically visible
 +Accessing: use just the name of the variable
 +Declaration:​ must be in curly braces in an //​@implementation//​ section
 +
 +Declaration (in a .m file):
 +<​code>​
 +@interface MyClass() {
 +     ​NSArray *arr;
 +}
 +
 +@end
 +</​code>​
 +
 +In a header file:
 +<​code>​
 +@interface MyClass : NSObject {
 +     ​NSArray *arr;
 +}
 +
 +@end
 +</​code>​
 +
 +Why are there two examples above? You can use //​@interface//​ in the header file or the implementation file. The format is different for each (parenthesis in the .m file, the colon/​superclass in the .h). The variable declared is the same. 
 +
 +Usage:
 +<​code>​
 +[arr addObject:​SOMEOBJECT];​
 +</​code>​
  
 ====Property==== ====Property====
 +Scope: any class that uses the class, the class itself
 +Accessing: Dot operator. Use self.NAME if accessing within class, OBJECT.NAME if using from another class
 +Declaration:​ must be in header file outside of curly braces
 +
 +Declaration (in a .h file):
 +<​code>​
 +@interface MyClass : NSObject {
 +
 +}
 +
 +@property (strong, nonatomic) NSArray *arr;
 +
 +@end
 +</​code>​
 +
 +Usage:
 +<​code>​
 +[self.arr addObject:​SOMEOBJECT];​
 +</​code>​
  
 ===Behind the Scenes-- Properties=== ===Behind the Scenes-- Properties===
 +What do properties do? They seem magical, but its actually quite simple. There aren't actually any public variables, variables declared with //​@property//​ are replaced at compile time.
 +
 +For example, ​
 +
 +  @property (strong, nonatomic) NSArray *arr;
 +  ​
 +is replaced with:
 +  -An private instance variable named _arr
 +  -Accessors, public methods:
 +
 +<​code>​
 +  - (NSarray*) getArr;
 +  - (void) arr;
 +</​code>​
 +
 +To recap: properties are a shorthand way of creating a private instance variable that has the same name as the property with an underscore and getter/​setter methods. You should **not** refer to the underscored variable in code, although you can.
 +
 +If you want to use the actual name of the variable instead of //​self.arr//,​ include the following line after //​@implementation//​ in the source code file:
 +
 +  @synthesize arr;
 + 
 +This allows you to use 
 +  [arr addObject:​SOMEOBJECT];​
 +  ​
 +instead of 
 +
 +  [self.arr addObject:​SOMEOBJECT];​
ios-labs-s14/intermediate-scope.txt · Last modified: 2014/02/26 10:42 by mbarboi