Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

mixing objective-c and c++

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

Posted on Nov 9, 2008 5:53 AM

Reply
25 replies

Nov 9, 2008 1:54 PM in response to orangekay

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.

Nov 9, 2008 5:27 PM in response to Etek

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.

Nov 10, 2008 8:23 AM in response to Etek

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

Nov 10, 2008 8:54 AM in response to orangekay

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

Nov 10, 2008 1:18 PM in response to etresoft

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];

mixing objective-c and c++

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.