What I’m trying to do is quite simple but I probably have the syntax wrong.
I have an Objective-C Interface with a Note class parameter. Note.h is a C++ class that basically looks like this:
#include <string>
using namespace std;
class Note {
public:
string name;
Note(string name){
this->name = name; // ERROR: Cannot find interface declaration for 'Note'
}
};
This is my controller where is using Note. I changed the file extension to .mm
@class Note;
@interface InstrumentGridViewController : UIViewController {
@public
Note* note;
}
@property (nonatomic, retain) Note* note;
And this is how I’m using it:
@implementation InstrumentGridViewController
@synthesize note;
- (void)buttonPressed:(id)sender {
note = Note("fa"); // ERROR: Cannot convert 'Note' to 'Note*' in assignment
NSLog(@"naam van de noot is %s", note->name); // ERROR: Cannot find interface declaration for 'Note'
}
I’m getting these three errors though (I’ve added them as comments on the right lines)
Any idea how what I’m doing wrong?
You need to allocate the
Noteobject usingnew:However it doesn’t seem right to be doing that in the button press action method…
Also don’t forget to
deleteit in your object’sdeallocmethod:Lastly your
@propertyattributeretainis wrong as it’s not an Objective-C object; instead useassignand better still make itreadonly.A better way to initialise most objects is by using a
constreference to them rather than a copy: