0

i need to build an application that define an array that should be made of image items. every image iten has an image, a name and a photographer name. i build my image item class and i want you to check if my define is correct and good(i just start to learn objective c). i want you to emphasize on the set's methods.

here is the photoitem.h:

#import <Foundation/Foundation.h>

@interface photoItem : NSObject
 {
     UIImage *imageView;
     NSString *photoNameLabel;
     NSString *photographerNameLabel;
     UIButton *viewPhoto;
 }
 @property(readonly) NSString *name;
 @property(readonly) NSString *nameOfPhotographer;
 @property(readonly) UIImage *imageItem;

 -(id)makePhotoItemWIthPhoto:(UIImage*)image name:(NSString*)photoName photographer:   (NSString*)photographerName;

@end

here is my photoitem.m:

#import "photoItem.h"

@implementation photoItem

@synthesize name;
@synthesize nameOfPhotographer;
@synthesize imageItem;

-(id)makePhotoItemWIthPhoto:(UIImage*)image name:(NSString*)photoName photographer:(NSString*)photographerName
{
    [self setName:photoName];
    [self setNameOfPhotographer:photographerName];
    [self setImageItem:image];
    return self;
}

-(void) setName:(NSString *)name
{
    photoNameLabel = name;
}  

-(void) setNameOfPhotographer:(NSString *)nameOfPhotographer
{
    photographerNameLabel = nameOfPhotographer;
}

-(void)setImageItem:(UIImage *)imageItem
{
    imageView = imageItem;
}
@end

i hope you could fix my errors(if there are some). thanks.

4

1 回答 1

0

想到两个问题:

1)-(id)makePhotoItemWIthPhoto:name:photographer:可能会更好-(id)initWithPhoto:name:photographer:。否则调用者需要先allocinit一个对象,这样才self有效,然后调用你的方法。在这一点上,返回self没有意义。例子:

-(idinitWithPhoto:(UIImage*)image name:(NSString*)photoName photographer:(NSString*)photographerName {
    self = [super init];
    if (self) {
        [self setName:photoName];
        [self setNameOfPhotographer:photographerName];
        [self setImageItem:image];
    }
    return self;
}

2) 这三个只读属性似乎没有任何用途,因为它们与您在makePhotoItemWIthPhoto:方法中初始化的变量没有任何联系。

于 2012-05-29T17:05:08.527 回答