0

So I am quite new on OC programming, I come from Front-end background (i.e. HTML/CSS/JavaScript ...), so I understand basic concepts of programming :)

Basically I created a console application, with a simple FooClass.

FooClass.h

#import <Foundation/Foundation.h>

@interface FooClass : NSObject

@property (strong, nonatomic) NSString *username;

- (NSString *) username;
- (void) setUsername:(NSString *)username;

@end

FooClass.m

#import "FooClass.h"

@implementation FooClass

@synthesize username = _username;

- (instancetype) init
{
    self = [super init];

    if (self) {

    }

    return self;
}

- (NSString *) username
{
    return _username;
}

- (void) setUsername:(NSString *)username
{
    _username = username;
}

@end

And in the main.m file, where the app bootstraps.

#import <Foundation/Foundation.h>
#include "FooClass.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        // insert code here...
        NSLog(@"Hello, World!");

        FooClass *foo = [[FooClass alloc] init];
        foo.username = @"a";
    }
    return 0;
}

XCode tells me that it cannot find property username on object of type FooClass. And I don't really have idea about it. Any one could help?

John Wu
  • 967
  • 1
  • 9
  • 18

1 Answers1

1

I am a bit late in posting the answer. Here are few things that you should consider.

  1. Since you have a property username. You are not required to create methods for setters and getters. The compiler will create them for you. Simply remove the two statements.

  2. No need to synthesize in .m as well.

  3. Instead of #include use #import. import takes only one copy even if you try to add the file(s) directly or indirectly from other files as compared to include.

Anoop Vaidya
  • 45,475
  • 15
  • 105
  • 134