Outlets

An outlet is a property through which we talk to an element in the view. It is a pointer to an object. A strong pointer means the object it points to will stick around until we are done with it. A weak pointer means the object will only remain if someone else has a strong pointer to it. As soon as no more strong pointers exist pointing to an object or an object we have a weak pointer to, then that object will be released and no longer exist.

Delegates

Having spent the last 10 years bashing my head against a brick wall when it came to programming with cocoa, a lot of things are now beginning to make sense, in no small part thanks to these guys Ray Wenderlich and Matthijas Hollemans. Check out their sites for some great tutorials.

Delegates are simply an object responding to another objects method calls on it’s behalf. For example a UIViewController might be appointed as the delegate for a UITableView. Then the UITableView’s methods can be called from within the UIViewController. If it’s not set up as a delegate then you cannot do this and you’ll find it very difficult to control the UITableView, which hinders you slightly in achieving virtually anything in programming with cocoa.

Learning Objective C Part 3

Declared Properties

Declared properties are a convenience notation that are used to replace the declaration, and optionally the implementation of accessor methods.

Property declarations are included with method declarations in the class interface. The basic definition uses the @property compiler directive, followed by type information and property name. The property can also be configured with custom options to help define how the accessor methods behave. Here are a few simple property declarations.

1
2
3
@property BOOL flag;
@property (copy) NSString *nameObject; //copies the object during assignment.
@property (readonly) UIView *rootView; // Declare only a getter method.

Each readable property specifies a method with the same name as the property. Each writable property specifies an additional method of the form setPropertyName: where the first letter of the property name is capitalised.

In your class implementation, you can use the @synthesize compiler directive to ask the compiler to generate the methods according to the specification in the declaration.

1
2
3
@synthesize flag;
@synthesize nameObject;
@synthesize rootView;

You can also combine the above into a single line if required.

1
@synthesize flag, nameObject, rootView;

In simple terms properties reduce the amount of code you have to write. Because the majority of accessor methods are written in similar ways, properties reduce the need to write setter and getter methods for each property exposed in a class. Instead you specify the behavior you want using the property declaration and then synthesize getter and setter methods based on the declaration at compile time.

Strings

ObjC supports the same conventions for specifying strings as C. i.e. single characters are enclosed in single quotes, and multiple characters are enclosed in double quotes. ObjC frameworks do not usually use c style strings, and instead pass them around as NSString objects.

The NSString class provides an object wrapper that has many advantages, and as strings are used commonly objC provides shorthand notation for creating NSString objects from constant values. All you need to do is precede a normal double quoted string with the @ symbol, as shown below.

1
2
3
4
NSString *myString = @"My String";
NSString *anotherString = [NSString stringWithFormat:@"%d %@", 1, @"String"];
//Create an objective C string from a C string
NSString *fromCString = [NSString stringWithCString:"A C string" encoding:NSASCIIStringEncoding];

Protocols

Protocols declare methods that can be implemented by any class. They are not classes themselves. They simply define an interface that other objects are responsible for implementing. When you implement the methods of a protocol in one of your classes, your class is said to conform to that protocol.

Protocols are used frequently to specify the interface for delegate objects. Below is a good way to look at the interplay between protocols, delegates and other objects.

The UIApplication class implements the required behavior of an application. Instead of making you subclass UIApplication to receive notifications about the current state of the application, 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 these notifications and provide an appropriate response. You specify that your class conforms to, or adopts, a protocol by putting in the interface block the name of the protocol in <> after the name of the class from which it inherits. You do not have to declare the protocol methods you implement.

1
2
3
4
@interface MyClass : NSObject
{
}
@end

The declaration of a protocol look similar to that of a class interface, with the exceptions that protocols have no parent calss and they do not define instance variables. Here is a simple protocol declaration with one method.

1
2
3
@protocol MyProtocol
-(void)myProtocolMethod;
@end

In the case of many delegate protocols , adopting a protocol is simply a matter of implememting the methods defined by that protocol. Some protocols require you to state explicitly that you support that protocol, and protocols can specify both required and optional methods.

Learning Objective C Part 2

Methods and Messaging.

A class in objC can declare either instance or class methods. An instance method is a method that works on a particular instance of the class. Therefore before an instance method can be called , an instance of the class must be created to call it on. Class methods do not require an instance. More later.

A method declaration consists of the method type identifier. Which will be a + or -, a return type, signature key words and the parameter type and name information. The following line of code shows the declaration of an instance method for

1
2
insertObject:atIndex
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index

This declaration is preceded by a minus sign which declares this method an instance method. The methods name insertObject:atIndex: is derived from joining all the signature keywords, including the colons, which declare a parameter is required. If a method has no parameters you omit the colon after the first and only signature keyword. In this example two parameters are required.
When you want to call a method you do so by sending a message to an object. A message is the method signature along with the parameters the method requires. All messages are sent dynamically, therefore facilitating the polymorphic behavior of objC classes.
Messages are enclosed by square brackets []. Inside the brackets, the receiving object is on the left and the message along with any parameters required is on the right. To send the insertObject:atIndex: message to an object in myArray you would use the following.

1
[myArray insertObject:anObject atIndex:0];

To avoid using numerous local variables to store temporary results, objC allows you to nest messages. The return value for each nested message is used as a parameter, or as the target, of another message. For example you could replace any of the variables in the previous message with messages to retrieve variables. Therefore if you had an object called myAppObject that had methods for accessing the array object and the object to insert into the array, you could write the preceding example like this;

1
[[myAppObject theArray] insertObject:[myAppObject objectToInsert]atIndex:0];

ObjC also has a dot syntax method for invoking accessor methods. Accessor methods get and set the state of objects. Sometimes know as getters and setters. They typically take the form -(type)propertyName and -(void)setPropertyName:(type). With the dot syntax the previous message could be re-written as;

1
[myAppObject.theArray insertObject:[myAppObject objectToInsert] atIndex:0];

Dot syntax can also be used for assignment.

1
myAppObject.theArray = aNewArray;

Which is simply a different way of writing

1
[myAppObject setTheArray:aNewArray];

The preceding examples were all messages to instance methods. We can also send messages to the class. When messaging a class the method must be defined as a class method, not an instance method.
Class methods are usually factory methods. Used when making instances of the class or for accessing some sort of information associated with the class. The syntax is the same as for instance methods however a + sign is used for the method type identifier.
The following example shows how a class method is used as a factory method. In this case the array method is a class method on the NSArray class, and inherited by NSMutableArray that allocates and initializes a new instance of the class and returns it to your code.

1
2
3
NSMutableArray *myArray = nil;
//create a new array and assign it to myArray variable.
myArray = [NSMutableArray array];

The listing below shows an implementation of MyClass from the previous example. Like the class declaration, the class implementation is identified by two compiler directives; @implementation and @end. These provide the compiler with the information it requires to associate the methods with the class. A methods definition matches its corresponding declaration in the interface, except for the inclusion of a code block.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@implementation MyClass
-(id)initWithString:(NSString *)aName
{
self = [super init];
if (self) {
name = [aName copy];
}
return self;
}
+ (MyClass *)createMyClassWithString: (NSString *)aName
{
return [[[self alloc] initWithString:aName] autorelease];
}
@end

Learning Objective C Part 1

Relationships with C.

Objective C (objC) is a superset of C. Therefore all that is valid in C is valid in objective C. ObjC includes a syntax for defining classes and methods and other constructs for dynamic extension of classes. To learn objC I would fully recommend you have a good grounding in C programming, particularly the use of pointers and data structures, an understanding of object orientated programming (oop) would also be useful. If you do not have these prerequisites then I would strongly recommend you start there first. Many of the concepts of oop are present in objC, i.e. encapsulation, inheritance and polymorphism. There are a few differences which I will attempt to highlight as we go along.

Objc as mentioned is a superset of C, and therefore supports the same basic syntax as C. As in a C program a header files are defined along with source files to separate public declarations from the implementation of your code. ObjC headers use the following extensions.

.h are header files containing class, type, functions and constant declarations.

.m are source (or implementation) files. These can contain both objC and C code.

.mm are also source files but only need to be used if you are using c++ classes or features within your objC code.

To include header files within your source code the #import directive is used. This is like the #include in C except it ensures that the same file is never included more than once.

Classes.

As in most other oop languages, classes are a way of bundling some data, along with the actions that operate on that data. An object is an instance of it’s class which contains a unique set of data held within its instance variables which were declared by the class and also pointers to the methods of the class. An object contains only data and pointers to it’s class’s methods.

To specify a class in objC two things are arequired. The interface of the class and the implementation of the class. The interface contains the class declaration and is where the instance variables and methods are declared. This is the .h file. The implementation contains the method codings of the class. This is the .m file.

The following code shows how a class called UserDefinedClass which inherits from objC’s base class, NSObject. It begins with the @interface directive and ends with the @end directive. Following the user defined class name is a colon and then the name of the parent class. The instance variables (ivars) are declared in the next block of code and surrounded with curly braces { and }. Following on is the method declarations of the class, with each instance variable and method declaration terminated with a semi colon.

1
2
3
4
5
6
7
8
9
@interface USerDefinedClass : NSObject
{
int count;
id data;
NSString* name;
}
- (id)initWithString:(NSString*)aName;
+ (UserDefinedClass*)createMyClassWithString:(NSString*)aName;
@end

N.B. This interface only declares methods. Classes can also declare properties which we will look at later on.

ObjC supports both weak and strong typing for variables containing objects. Strongly typed include the class name in the variable declaration. Weakly typed use the type id instead. Weakly typed are used often for things such as collector classes, where the exact type of object may be unknown. You are probably used to using strongly typed languages, and think that weakly typed variables may cause issues, but they actually allow more flexibility and dynamism in objC programs.

1
2
MyClass *myobject1; //Strong typing
id myObject2; //Weak typing

Notice the ? In objC object references are pointers. The id type also implies a pointer. Though no is required when using id.

Reducing the RAM useage of MySQL

One thing I noticed when setting up the MySQL database was the amount of RAM that it was using on my server. It seems that when you install MySQL it is fully featured and set up for much bigger useage than your average webserver requires. My initial install was using over 100Mb. What we need to do reduce it is to create a configuration file that MySQL looks for when it starts up. In our initial webserver setup we don’t have one, so the MySQL database server starts with defaults.
To create this file from the command line you need to open up a command line text editor. There are a few about, nano, pico, vim etc. My preferred one is vim. Basically to create a file in vim you just need to start vim and state the file path. As MySQL looks for it’s config file called my.cnf in the /etc folder we simply type;

1
vi /etc/my.cnf

Now pressing i to start insert mode on the vi text editor we can type, or copy depending on what system you are using, the following text into our my.cnf document.

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
[cc lang="mysql"][mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
skip-locking
set-variable = key_buffer=16K
set-variable = max_allowed_packet=1M
set-variable = thread_stack=64K
set-variable = table_cache=4
set-variable = sort_buffer=64K
set-variable = net_buffer_length=2K

# For low memory, InnoDB should not be used so keep skip-innodb uncommented unless required
skip-innodb

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[isamchk]
key_buffer = 8M
sort_buffer_size = 8M

[myisamchk]
key_buffer = 8M
sort_buffer_size = 8M

[mysqlhotcopy]
interactive-timeout"[/cc]

Save the file from vi, by pressing Esc, then typing :wq this will write the file to /etc/my.cnf and quit the text editor.
Now we need to restart MySQL by using;

1
/etc/init.d/mysqld restart

Tada! Now your MySQL installation is consuming almost half the amount of RAM as before, whilst still offering way more features than you are ever likely going to need for a wordpress web host.
This could probably be trimmed down even more if needed, though this will do for starters.

^