【问题标题】:access to array object访问数组对象
【发布时间】:2012-06-02 18:32:46
【问题描述】:

我创建一个数组并用对象对其进行初始化。我试图访问数组对象,但我得到一个(null)。我做错了什么?

    photoArray = [[NSMutableArray alloc] init];
    PhotoItem *photo1 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"1.jpg"] name:@"roy rest"  photographer:@"roy"];
    PhotoItem *photo2 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"2.jpg"] name:@"roy's hand" photographer:@"roy"];
    PhotoItem *photo3 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"3.jpg"] name:@"sapir first" photographer:@"sapir"];
    PhotoItem *photo4 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"4.jpg"] name:@"sapir second" photographer:@"sapir"];
    [photoArray addObject:photo1];
    [photoArray addObject:photo2];
    [photoArray addObject:photo3];
    [photoArray addObject:photo4];

我尝试通过这行代码访问其中一个对象(返回(null)):

photoName.text = [NSString stringWithFormat:@"%@", [[photoArray objectAtIndex:2] nameOfPhotographer]]

更新:照片项目代码:

-(id)initWithPhoto:(UIImage*)image name:(NSString*)photoName photographer:(NSString*)photographerName
{
    self = [super init];
    if(self)
    {
    imageItem = image;
    name = photoName;
    nameOfPhotographer = photographerName;


    //[self setName:photoName];
    //[self setNameOfPhotographer:photographerName];
    //[self setImageItem:image];
}
return self;
}

有什么问题?

谢谢!!

【问题讨论】:

  • 摄影师你叫什么名字?
  • 可能你需要输入你的数组结果。
  • 当 nameOfPhotographer 可能返回一个字符串时,为什么要使用 [NSString stringWithFormat ...]?

标签: objective-c xcode arrays object nsmutablearray


【解决方案1】:

你应该这样做:

@property(strong, nonatomic) NSString* nameOfPhotographer;

在initWith....

self.nameOfPhotographer = 摄影师姓名;

【讨论】:

    【解决方案2】:

    首先,确保您的 PhotoItem Interface 文件与此类似:

    @interface PhotoItem : NSObject
    {
      UIImage *imageItem;
      NSString *name;
      NSString *photographer;
    }
    
    @property (nonatomic, retain) UIImage *imageItem;
    @property (nonatomic, retain) NSString *name;
    @property (nonatomic, retain) NSString *photographer;
    
    
    @end
    

    其次确保你的实现部分是这样的:

    @implementation PhotoItem
    @synthesize imageItem, name, photographer;
    
    -(id)initWithPhoto:(UIImage*)image name:(NSString*)photoName photographer:(NSString*)photographerName
    {
      self = [super init];
    
      if(self)
      {
        self.imageItem = image;
        self.name = photoName;
        self.photographer = photographerName;
      }
    
      return self;
    }
    
    - (NSString *)description
    {
      return [NSString stringWithFormat:@"name:%@\nphotographer:%@", self.name, self.photographer];
    }
    
    @end
    

    现在,由于 description 是您记录对象时对对象的默认调用,您现在可以真正轻松地诊断您的问题。

    我添加了类文件来向您展示属性是多么方便。

    现在,当您想访问时,说出摄影师的姓名,只需:

    photo1.photographer
    

    现在在您的代码中,不要立即将所有内容都放入数组中,而是这样做以确保一切正常:

    PhotoItem *photo1 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"1.jpg"] name:@"roy rest"  photographer:@"roy"];
    
    NSLog(@"%@", [photo1 description]);
    

    实际上应该是,我给你的类代码应该可以正常工作。现在根据需要将它们全部放入一个数组中,并确保数组中的所有内容都符合要求,请执行以下操作:

    NSLog(@"%@", photoArray); //Description is the default when logging an object
    //An array will call the description method on each of it's containing objects
    

    希望这会有所帮助!

    【讨论】:

    • 根据此处的讨论,stackoverflow.com/questions/192721/…,您不应在类的 init 和 dealloc 方法中使用属性访问器。
    • @timthetoolman 这实际上不是真的。由于访问器的定义,使用 self.*something* =passed_variable 会导致保留此变量。由于这些变量将被自动释放,可能在此 init 方法之后立即释放,如果您刚刚做了某事 =passed_variable,则指针引用将在某个时候导致 BAD_ACCESS。现在,如果你做了一些事情 = [passed_variable retain],那很好,只要你稍后发布它。 APRULE 从未说过他/她使用的是什么环境:这种方法安全可靠,非常有意义。
    • 我倾向于倾听像 mmalc 这样为 Apple 工作并真正了解他们的东西的人。如果他们说不要在 init 和 dealloc 中使用属性访问器,我认为这是福音。如果需要保留,除非您使用 ARC,否则您可以使用 name =[photoName retain];等等。如果您使用的是 ARC,则无需担心。 Mike Ash 也在这里:mikeash.com/pyblog/…。这些家伙是真正的专业人士。忽略它们,后果自负。有龙。
    • 感谢@timthetoolman 的提示。值得庆幸的是,这是一个简单的情况,看起来并不重要。在阅读完所有内容后,我现在将在 inits 和 deallocs 中改变我的风格 =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2019-03-22
    • 2012-09-22
    • 2018-05-27
    • 1970-01-01
    • 2021-11-07
    • 2018-11-22
    相关资源
    最近更新 更多