Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

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 10, 2008 2:01 PM in response to Etek

First of all, considering that you are having trouble, you need more NSLog() statements to see what is going on and which functions/code blocks are getting called.

I would look at the creation and deallocation of receivedData. I think it should already exist before there is any possibility of anyone accessing it. It should only be released in the object's dealloc method.

Keep trying it with additional NSLog() statements. It should either succeed or fail eventually with an error.

Nov 10, 2008 2:47 PM in response to etresoft

I used an infinite loop after calling -connect and put breakpoints in every method but none is called, except for the -connect and -init of course.

How can I check if any messages reach my object?

Also, if I move these methods in myAppdelegate.m, they work and download data. But that's because myAppdelegate.m has been properly set as a delegate.

Message was edited by: Etek

Nov 11, 2008 9:49 AM in response to Etek

Etek wrote:
I used an infinite loop after calling -connect and put breakpoints in every method but none is called, except for the -connect and -init of course.

How can I check if any messages reach my object?


Sorry, but debugger breakpoints really aren't reliable. Use NSLog() instead.

Also, if I move these methods in myAppdelegate.m, they work and download data. But that's because myAppdelegate.m has been properly set as a delegate.


OK. Now I'm confused. Where else would you put these methods? If they work, then you are done, aren't you?

Nov 11, 2008 12:04 PM in response to etresoft

i think what you suggested before and orange underlined is true. The recipient to get the data has to be set before the init message. I'll try when i get home.

About having these in a class, it's just for reusability. If i write this class correctly, i'll just use it instead of having to implement these methods again. With all the error messaging and functionlity already tested.

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.