Roland Adi Nugraha
Roland Adi Nugraha

Reputation: 31

@class and #import on another view controller, what does it means?

I am newbie on Xcode, and trying to figure out more about coding in xcode.

So, I am trying to learn more about models (models operation) on objective C.

I am confused in @Class declaration in PhotoViewController.h and .m Files

as you may see below, I already imported Photo.h on appdelegate.m and also PhotoViewController.m files

the objective from my tutorial is PhotoViewController.m files can recognize self.photo.filename

But, why it has to add @Class and @property in PhotoViewController.h files?

isnt #import command is already enough? what does @Class means and why it has to include @property too?

note : I tried to put a comment (//) on @class , but xcode tell me that photo property not found, and when I put added comment (//) on property

PhotoViewController.m file also messed up with unrecognized photo property.

I dont quite understand, the use of @class and #import at the same time, plus declaring @property photo

here is Photo.m

#import "Photo.h"

@implementation Photo

-(id)init
{
    self = [super init];
    return self;
}

@end

and

Photo.h

#import <Foundation/Foundation.h>

@interface Photo : NSObject
@property (weak, atomic) NSString *title;
@property (strong, nonatomic) NSString *detail;
@property (strong, nonatomic) NSString *filename;
@property (strong, nonatomic) NSString *thumbnail;
@end

Appdelegate.m

#import "AppDelegate.h"
#import "FeedTableViewController.h"
#import "ProfileViewController.h"
#import "FavoritesViewController.h"
#import "Photo.h"


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    Photo *photo= [[Photo alloc]init];
    photo.title = @"Demo Photo";
    photo.detail = @"This is a demo photo";
    photo.filename = @"demo.png";
    photo.thumbnail = @"demo-thumb.png";


    return YES;
}
@end

PhotoViewController.h Files

#import <UIKit/UIKit.h>
@class Photo;
@interface PhotoViewController : UIViewController

@property (weak, nonatomic) NSString *imageFileName;
@property (weak, nonatomic) NSString *imageTitle;
@property (strong, nonatomic) Photo *photo;
@end

PhotoViewController.m Files

#import "PhotoViewController.h"
#import "UIImageView+AFNetworking.h"
#import "Photo.h"

@implementation PhotoViewController

-(void)viewDidLoad {
//    self.title = self.imageTitle;
    UIImageView *imageView = [[UIImageView alloc] init];

    [imageView setImageWithURL:[UIImage imageNamed:self.photo.filename]];
    imageView.frame = CGRectMake(10,10,300,300);

    [self.view addSubview:imageView];

    UILabel *imageTitleLabel = [[UILabel alloc] init];
    imageTitleLabel.text = self.imageTitle;
    imageTitleLabel.frame = CGRectMake(11,320,300,40);

    [self.view addSubview:imageTitleLabel];
}

@end

Upvotes: 0

Views: 720

Answers (2)

PR Singh
PR Singh

Reputation: 653

@property

It is for replacement of getter method, whenever you want to get the value you have to declare that variable as property, so that need not to write getter method separately,

you should implement @synthesize also in Photo.m, @synthesize will work as setter method.

Upvotes: 0

Dobroćudni Tapir
Dobroćudni Tapir

Reputation: 3102

@class Photo defines the existence of class Photo to PhotoViewController.h allowing you to declare photo property. Photo property is later used in PhotoViewController.m to to access the instance variable photo like this self.photo or [self photo]

You could have put #import "Photo.h" in your PhotoViewController.h but it is cleaner this way :)

Upvotes: 1

Related Questions