-1

I have a chunk of code written in C that is pulling data from a device, that code can be viewed Here

I want this code which contains a function called getData to be run as method (called getData) of an Objective-C class rather than just having it run from inside the main() C function as it does now while I test it out. My goal is for this method to populate a public global variable variable or even just an class property with a base64 encoded string and return a status.

Here Is how I'm currently setting this up, but this is also my first time writing both C and Objective-C so to be honest I'm not sure if my approach is correct. First I create an interface(protocol) called GDDriver.h

//GDDriver.h
typedef enum Status : NSInteger {
    Success,
    Cancelled,
    DeviceNotFound,
    DeviceError,
    UnkownModel,
} Status;

@protocol GWDriver <NSObject>

-(enum Status)getData;
-(void)cancel;

@end

I then have a class which lets call it DriverOne which I'm setting up like this

DriverOne.h

// DriverOne.h
#import <Foundation/Foundation.h>
#import "GWDriver.h"

@interface DriverOne : NSObject <GWDriver>

@end

DriverOne.m

// DriverOne.m
#import "DriverOne.h"

@implementation DriverOne

enum Status getData(char* encodedBuffer, int user)
{
    // Copy C code which I showed in the link earlier
    // into this method. I will want it to return a status
    // and populate a global variable with the data.
}


void cancel()
{
    // Cancels and closes driver
    // is called from with in getData()
}

@end

I'm aware that the methods are currently written in C syntax here, I'm not sure if that is bad practice or not though at this point. Here is how I intend to call the method.

DriverOne *driver = [[DriverOne alloc] init];
driver.getData();  

Am I completely off base here or is this approach correct in what I'm trying to achieve? Thanks for any advice or suggestions.

Stavros_S
  • 2,145
  • 7
  • 31
  • 75

1 Answers1

1

Best practice dictates that you generally don't use C style functions in your Objective C classes. char pointers are also frowned upon, generally. I would change your functions to something like this:

- (enum Status)getDataWithBuffer:(NSString *)buffer userId:(NSInteger)userId
{
    char * encodedBuffer = [buffer UTF8String];

    // Copy C code which I showed in the link earlier
    // into this method. I will want it to return a status
    // and populate a global variable with the data.
}


- (void)cancel
{
    // Cancels and closes driver
    // is called from with in getData()
}

and then change your call to this

DriverOne *driver = [[DriverOne alloc] init];
[driver getData:@"your data" userId:12345]; 
Lance
  • 8,872
  • 2
  • 36
  • 47
  • If I set a property on the DriverOne class would I be able to populate it with the data that getData receives even if getData returns void? It actually is getting and converting a byte array buffer to a string then to a Base64 string. – Stavros_S Mar 11 '14 at 20:08
  • Yup, you could totally do that. NSData might be what you would store that in. Depends on what you're doing with it I suppose. – Lance Mar 11 '14 at 20:09
  • Good to know! the driver method is actually going to be called from a button event and then the data that gets stored in the property will then be sent out to a parser. – Stavros_S Mar 11 '14 at 20:11