Benches Uncategorized

17

mixing objective-c and c++

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hello,

I'm trying to write a class in c++ that needs to implement some objective-c methods. I read here, that it's possible:
http://www.mactech.com/articles/mactech/Vol.13/13.03/CandObjectiveCCompared/ (at the end of the document)

However, I get this error: 'error: expected unqualified-id before '-' token' at this line

+- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response+

Here's the url_connection.h

#ifndef
URL_CONNECTIONH

#define
URL_CONNECTIONH

#include "debug.h"

class URL_Connection

{

private:

NSData *receivedData;

NSURL *url;

NSURLRequest *theRequest;

NSURLConnection *theConnection;

public:

URL_Connection();

~URL_Connection();

};

+- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response+

{

// this method is called when the server has determined that it

// has enough information to create the NSURLResponse

// it can be called multiple times, for example in the case of a

// redirect, so each time we reset the data.

// receivedData is declared as a method instance elsewhere

[receivedData setLength:0];

}

#endif

I really have no ideea how to fix this and couldn't find something similar on the web. What am I doing wrong?

Please advice,

Thanks

17 replies

Sign in or join Beachball Help to reply.

  1. 0

    orangekay

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    You cannot define an Objective-C method outside of an Objective-C class.

  2. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Cocoa is only going to talk to Objective-C. There is no way around that. You can create a simple Objective-C class derived from NSObject and make it a delegate of anything. How to assign that delegate depends on the context. If it is a user interface object you can do it through Interface Builder. If not, you'll have to look for some sort of "setDelegate" method.

    Now, you can mix Objective-C and C++ very easily. Just use .mm Objective-C++ files. Your Cocoa-facing interface will always have to be Objective-C++, but those Objective-C++ methods can simply call an instance variable that is a pointer to a C++ object.

  3. 0

    xnav

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    For this forum format your code like this:


    your code

    At any rate it looks as if the line after '~URL_Connection();', the '};' should be '}'.

  4. 0

    Etek

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thanks, but the ; after } for a class is mandatory.

  5. 0

    orangekay

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    You cannot define an Objective-C method outside of an Objective-C class.

  6. 0

    Etek

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    So, best option will be to write a objective-c class to handle this, instead of c++?

  7. 0

    orangekay

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    That would be the only option unless you want to get extremely familiar with the runtime implementation.

  8. 0

    Etek

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Ok, so I painfully written an objective-c class. The only thing remaining is, how do I make this class be a delegate in order to receive messages?

    I hope this is the problem, because none of the methods below is called.


    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection;

    Thank you.

  9. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Cocoa is only going to talk to Objective-C. There is no way around that. You can create a simple Objective-C class derived from NSObject and make it a delegate of anything. How to assign that delegate depends on the context. If it is a user interface object you can do it through Interface Builder. If not, you'll have to look for some sort of "setDelegate" method.

    Now, you can mix Objective-C and C++ very easily. Just use .mm Objective-C++ files. Your Cocoa-facing interface will always have to be Objective-C++, but those Objective-C++ methods can simply call an instance variable that is a pointer to a C++ object.

  10. 0

    Etek

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thank you for your response.

    I could not find any decent documentation and proper examples on the web for how to create a delegate.

    Any good sites or books about objective-c that you can recommend?

    Thank you

  11. 0

    orangekay

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Etek wrote:

    Thank you for your response.

    I could not find any decent documentation and proper examples on the web for how to create a delegate.

    Then you didn't look very hard.

    http://developer.apple.com/qa/qa2007/qa1554.html

  12. 0

    Etek

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    How does that help me?

    There's nothing there about delegates that don't need a nib file. The messages required have nothing to do with a window.

  13. 0

    orangekay

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Etek wrote:

    There's nothing there about delegates that don't need a nib file.

    You'd get a lot farther if you actually read the words. Since you seem to be attempting to use the URL loading system here, the associated documentation very clearly shows you how to specify your delegate object when you create the connection via initWithRequest:

    http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks /UsingNSURLConnection.html#//apple_ref/doc/uid/20001836

    Notice the second parameter is named "delegate."

    http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSUR LConnectionClass/Reference/Reference.html#//appleref/doc/uid/20001697-BAJDDIDG

  14. 0

    Etek

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Let me put it more clearly.

    I already did that. If I take all that and put it in let's say myAppdelegate.m, it works.

    If I put it in my class, they don't work anymore.

    How do I make my class receive those messages? What am I doing wrong?


    -(void) connect
    {
    {
    url = [NSURL URLWithString: @"http://www.google.com" ];
    theRequest=[NSURLRequest requestWithURL : url
    cachePolicy : NSURLRequestReloadIgnoringCacheData
    timeoutInterval : 60.0];
    // create the connection with the request
    // and start loading the data
    theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection)
    {
    // Create the NSMutableData that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    receivedData=[[NSMutableData data] retain];
    isConnected = true;
    }
    else
    {
    // inform the user that the download could not be made
    isConnected = false;
    }
    }
    }

    I tried using setDelegate and stuff, none of it works. Obviously I'm doing something wrong and I haven't found an actual complete example of a working class to go from there, only bits and pieces and I can't put together.

    Message was edited by: Etek

    Message was edited by: Etek

  15. 0

    orangekay

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    You would have to instantiate an object of your class' type and pass it in rather than self.

  16. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Perhaps you should post your entire class, including all the delegate methods. What you are doing looks correct and is identical to Apple's NSURLConnection example. It should work fine.

  17. 0

    Etek

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Ok, here goes:

    url_connection.h


    @interface URL_Connection : NSObject
    {
    NSMutableData *receivedData;
    NSURL *url;
    NSURLRequest *theRequest;
    NSURLConnection *theConnection;
    NSError *theError;

    bool _isConnected;
    bool _isFinished;

    id _delegate;
    }
    - (id) delegate;
    - (void) setDelegate:(id)newDelegate;
    - (void) connect;
    - (bool) isConnected;
    - (bool) isFinished;
    - (NSError*) getError;
    - (int) getDataSize;
    - (unsigned char*) getData;
    //I understood these do not necesarily have to be here, but I put them to avoid confusion in the future
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection;
    @end

    url_connection.m


    #import "URL_Connection.h"
    @implementation URL_Connection
    - (id)init
    {
    if (self = [super init])
    {
    _isConnected = false;
    _isFinished = false;
    }
    return self;
    }
    - (id)delegate
    {
    return _delegate;
    }
    - (void)setDelegate:(id)newDelegate
    {
    _delegate = newDelegate;
    }

    -(void) connect
    {
    {
    url = [NSURL URLWithString: @"http://www.google.com" ];

    theRequest=[NSURLRequest requestWithURL : url
    cachePolicy : NSURLRequestReloadIgnoringCacheData
    timeoutInterval : 60.0];

    // create the connection with the request
    // and start loading the data
    theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection)
    {
    // Create the NSMutableData that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    receivedData=[[NSMutableData data] retain];
    _isConnected = true;
    }
    else
    {
    // inform the user that the download could not be made
    _isConnected = false;
    }
    }
    }
    - (bool) isConnected
    {
    return _isConnected;
    }
    - (NSError*) getError
    {
    return theError;
    }
    - (bool) isFinished
    {
    return _isFinished;
    }
    - (unsigned char*) getData
    {
    int urlLength = [receivedData length];
    unsigned char *downloadBuffer;

    downloadBuffer = (unsigned char*) malloc (urlLength);

    [receivedData getBytes: (unsigned char*)downloadBuffer];

    return downloadBuffer;
    }
    - (int) getDataSize
    {
    return [receivedData length];
    }

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
    // this method is called when the server has determined that it
    // has enough information to create the NSURLResponse
    // it can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.
    // receivedData is declared as a method instance elsewhere
    [receivedData setLength:0];
    }


    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
    // append the new data to the receivedData
    // receivedData is declared as a method instance elsewhere
    [receivedData appendData:data];
    }


    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [receivedData release];
    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
    [error localizedDescription],
    [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
    // release the connection, and the data object

    _isFinished = true;

    }
    - (void) dealloc
    {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    if (_delegate)
    [nc removeObserver:_delegate name:nil object:self];

    [super dealloc];
    //there's more to be released here
    }
    @end

    url_connection object initialization


    URL_Connection *connection = [[URL_Connection alloc] init];