Classes and Constructors in iOS, Android and Windows Store apps


iOS, Windows and Andoid all include class constructors. Places where setup can be performed when a class is first initialised. So let's say we have a class called Multiplier. It is initialized with a number that later calls to its method multiplyBy are, as you would expect, multiplied by.

iOS (Objective-C)

After we've gone through the process of File>New to create a new file, and provided the name of our class to Xcode and the class it is to be a subclass of (in this case NSObject), we're ready to create a method -(id)initWithMultiplier:(int)multiplier; to initialize the class.

The init prefix to a method signifies a constructor in Xcode, and so our Multiplier.h file would look like this:

#import <Foundation/Foundation.h>
@interface Multiplier : NSObject
-(id)initWithMultiplier:(int)multiplier;
-(int)multiplyBy:(int)number;
@end
and the Multiplier.m file like this:

#import "Multiplier.h"
@implementation Multiplier
int numberToMultiplyBy;
-(id)initWithMultiplier:(int)multiplier {
self = [super init];
if (self) {
numberToMultiplyBy = multiplier;
}
return self;
}
-(int)multiplyBy:(int)number {
int multipliedNumber = numberToMultiplyBy*number;
return multipliedNumber;
}
@end

As you'll see I've also included the multiplyBy method, and to make use of all this in the initial view controller .m file we would first import the class using:

#import "Multiplier.h"

then write:

- (void)viewDidLoad
{
[super viewDidLoad];
Multiplier *multiplier = [[Multiplier alloc] initWithMultiplier:6];
NSLog(@"%i",[multiplier multiplyBy:3]);
}

Android (Eclipse)

In Android we'd again go File>New and this time create a class, ticking the constructor box for convenience. And this is how we'd translate our Multiplier.h and Multiplier.m files into the single Android class file.

public class Multiplier {
private int numberToMultiplyBy;
public Multiplier(int multiplier) {
// TODO Auto-generated constructor stub
this.numberToMultiplyBy = multiplier;
}
public int multiplyBy(int number) {
int multipliedNumber = numberToMultiplyBy*number;
return multipliedNumber;
}
}

As you'll see the Auto generated code for the constructor is simply written public Multiplier () {} and this is how we signify a constructor in Android, i.e. by repeating the class name as a method.

In the MainActivity.java file we'd then add the code needed to instatiate our object and to call it:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instatiate and call Multiplier class
Multiplier multiplier = new Multiplier(6);
Log.i("Result=",String.valueOf(multiplier.multiplyBy(3)));
}

As long as we've saved the class file to the same package/folder as the MainActivity.java file, no code should be necessary to import the class before creating our instance.

Windows (Visual Studio)

Using Shift+Alt+C or by right clicking on our App in the Solution Explorer and selecting Add>New>Class we can create a generic class file and simply copy and paste the same class code as we used in Android:


namespace App
{
class Multiplier {
private int numberToMultiplyBy;
public Multiplier(int multiplier)
{
this.numberToMultiplyBy = multiplier;
}
public int multiplyBy(int number)
{
int multipliedNumber = numberToMultiplyBy * number;
return multipliedNumber;
}
}
}

This is because the constructor is declared in the same way as in Android, and conveniently so are the other elements handled here.

We then add the instantiation to the NavigatedTo method of the MainPage:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
Multiplier multiplier = new Multiplier(6);
System.Diagnostics.Debug.WriteLine(multiplier.multiplyBy(3));
}

And aside from the Debug method, this is straight cut and paste from the Android app as well.

Updated 25th July 2014: iOS (Swift)

In Swift the same method would look like this:

class Multiplier {
    private var numberToMultiplyBy:Int
    init(multiplier:Int)
    {
    self.numberToMultiplyBy = multiplier
    }
    public func multiplyBy(number:Int) -> Int
    {     var multipliedNumber = numberToMultiplyBy * number     return multipliedNumber;     } }

And we'd call it in this way.

let multiplier = Multiplier(multiplier:6)
println(multiplier.multiplyBy(5))
Endorse on Coderwall

Comments