我是Xcode的新手,并试图找出有关xcode编码的更多信息。
因此,我试图了解有关目标C的模型(模型操作)的更多信息。
我对PhotoViewController.h和.m文件中的@Class声明感到困惑
如下所示,我已经在appdelegate.m和PhotoViewController.m文件上导入了Photo.h
我的教程中的目标是PhotoViewController.m文件可以识别self.photo.filename
但是,为什么必须在PhotoViewController.h文件中添加@Class和@property?
isnt #import命令已经足够了吗? @Class是什么意思,为什么它也必须包含@property?
注意:我试图在@class上添加注释(//),但是xcode告诉我找不到photo属性,当我在属性上添加注释(//)时
PhotoViewController.m文件还混淆了无法识别的照片属性。
我不太明白,同时使用@class和#import,并声明@property照片
这是Photo.m
#import "Photo.h"
@implementation Photo
-(id)init
{
self = [super init];
return self;
}
@end
和
照片.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文件
#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文件
#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
最佳答案
@class Photo
将类Photo
的存在定义为PhotoViewController.h
,允许您声明照片属性。
照片属性随后在PhotoViewController.m
中用于访问实例变量照片,例如self.photo
或[self photo]
您可以在#import "Photo.h"
中放入PhotoViewController.h
,但是这样更干净:)