Porting Application from iOS to WP7C#. Some toughts migration of Objective-C to C#

29. April 2011 12:07 by Mrojas in   //  Tags: , , , , , , , , , , , , ,   //   Comments (0)

Windows Phone 7 (WP7) is out! and it’s a great platform for developing new Apps. After being involved with Silverlight for a while
I am glad to have now the option to deploy apps to Windows Phone 7.

But we have to recognize that there are tons of great Apps for iPhone already. You might even have some of them.
So it’s porting time. Here at Artinsoft we love to upgrade/port application to all platforms.
So I will provide here some basic thoughts to help you upgrade your code.

For this post let’s discuss a little about applications written for XCode.

XCode applications are written in Objetive-C.Let’s map some examples:

In Objective-C your class will be usually

#import <Foundation/NSObject.h>
#import <Foundation/NSObject.h>

@interface Fraction: NSObject {
    int numerator;
    int denominator;
}

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
How you should understand that? Well this is just the list of methods in your class something like:
using System;
public class Fraction {    
    int _numerator;    
    int _denominator;    
    public void print() { /* TODO*/}     
    public int numerator   { get { /* TODO */ } set { /*TODO*/} }    
    public int denominator { get { /* TODO */ } set { /*TODO*/} }
}
The code for these methods will be in the .m file.
#import "Fraction.h"
#import <stdio.h>

@implementation Fraction
-(void) print {
    printf( "%i/%i", numerator, denominator );
}

-(void) setNumerator: (int) n {
    numerator = n;
}

-(void) setDenominator: (int) d {
    denominator = d;
}

-(int) denominator {
    return denominator;
}

-(int) numerator {
    return numerator;
}
@end
So that will complement your class implementation for something like:
using System;
public class Fraction 
{    
    int _numerator;    
    int _denominator;    
    public void print() 
    { 
        Console.Write("{0}/{1}" ,numerator,denominator);
    }     
    public int numerator     
    {         
        get { return _numerator; }        
        set { _numerator = value; }    
    }    
    public int denominator     
    {        
        get { return _denominator; }       
        set { _denominator = value; }    
    }
}
Ok An now let’s look at the Objective-C main.m:
#import <stdio.h>
#import "Fraction.h"

int main( int argc, const char *argv[] ) {
    // create a new instance
    Fraction *frac = [[Fraction alloc] init];
    Fraction *frac2 = [[Fraction alloc] init];

    // set the values
    [frac setNumerator: 1];
    [frac setDenominator: 3];

    // combined set
    [frac2 setNumerator: 1 andDenominator: 5];

    // print it
    printf( "The fraction is: " );
    [frac print];
    printf( "\n" );

    // print it
    printf( "Fraction 2 is: " );
    [frac2 print];
    printf( "\n" );

    // free memory
    [frac release];
    [frac2 release];

    return 0;
}
 
Which can be rewritten in C# as:
 
using System;
static class ProgramMain{static int Main(string[] argv) {
    // create a new instance
    Fraction frac = new Fraction();
    // set the values frac.Numerator = 1; frac.Denominator = 3;
    // print it
    Console.Write( "The fraction is: " );
    frac.print();    Console.Write( "\n" );
    // free memory frac = null;
    return 0;
}}
Well this is just for warming up. In following posts we will be looking 
at more Objective-C \ XCode concept mapping to C#.
I recommend looking at the site http://wp7mapping.interoperabilitybridges.com/ 
that provides a lot about mapping iOS API
 

Categories