简体   繁体   English

数据未持久存在于同一对象的多个实例中(iOS)

[英]Data is not persisting in multiple instances of the same object (iOS)

I'm having issues with data persisting inside of multiple instances of objects I'm creating. 我在创建的多个对象实例中持久保存数据时遇到问题。

I have a class "IconViewController" that extends UIViewController that I pass information to, such as the name of the image it should be using: 我有一个“ IconViewController”类,该类扩展了我将信息传递到的UIViewController,例如它应该使用的图像名称:


//IconViewController.h

@interface AppIconViewController : UIViewController
{
    NSString *imageName;
}

@property (nonatomic, retain) NSString *imageName;

- (void) doSomething;


//IconViewController.m

@implementation AppIconViewController

@synthesize imageName;

NSNumber *iconWidth;

- (void)loadView
{
    [super loadView];

    UIImageView *iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    iconWidth = [NSNumber numberWithFloat:iconImage.bounds.size.width];
    [iconImage release];

    NSLog(@"iconWidth: %f", [iconWidth floatValue]);
}

- (void) doSomething
{
    NSLog(@"iconWidth: %f", [iconWidth floatValue]);
}

In another view controller, I'm instantiating several instances of these IconViewControllers and passing different sized images to them: 在另一个视图控制器中,我实例化了这些IconViewControllers的几个实例,并将不同大小的图像传递给它们:


AppIconViewController *appIcon1 = [[AppIconViewController alloc] initWithNibName:nil bundle:nil];
appIcon1.imageName = @"Image65PXWide.png";
[self.view addSubview:appIcon1.view];

AppIconViewController *appIcon2 = [[AppIconViewController alloc] initWithNibName:nil bundle:nil];
appIcon2.imageName = @"Image105PXWide.png";
[self.view addSubview:appIcon2.view];

Okay, the weirdness is that when I'm creating these, I'm getting logs back that are accurate...appIcon1 logs "iconWidth: 65.0" and appIcon2 logs "iconWidth: 105.0". 好的,很奇怪,当我创建它们时,我得到的日志是准确的... appIcon1记录“ iconWidth:65.0”,而appIcon2记录“ iconWidth:105.0”。 But when I call: 但是当我打电话:

[appIcon1 doSomething];

...my log is "iconWidth:105.0". ...我的日志是“ iconWidth:105.0”。

Why is the data in the first instance reflecting the data in the second instance? 为什么第一个实例中的数据反映了第二个实例中的数据? What am I missing? 我想念什么?

EDIT: 编辑:
I know that if I declare iconWidth in the header and synthesize it as a property, that it will work. 我知道,如果我在标头中声明iconWidth并将其综合为一个属性,它将可以工作。 So what I'm wondering is how to make a private version of it persist. 因此,我想知道的是如何保留其私有版本。 Because I tried retaining the NSNumber with: 因为我尝试通过以下方式保留NSNumber:

iconWidth = [[NSNumber numberWithFloat:iconImage.bounds.size.width] retain]; iconWidth = [[NSNumber numberWithFloat:iconImage.bounds.size.width]保留];

...and it still doesn't work. ...而且仍然无法正常工作。 Does it have to be synthesized and public? 它必须被合成并公开吗?

EDIT #2: 编辑#2:
Okay, so I figured out that once I declare iconWidth in my header, it works just fine, and I don't have to synthesize it so it keeps it private. 好的,所以我发现,一旦在标头中声明了iconWidth,它就可以正常工作,而且我不需要对其进行合成,因此可以将其保持私有状态。 Not sure why exactly it won't work if declared in the implementation file - does anyone have any insight into why and if there's any purpose in declaring variables at the top of an implementation but not in the header? 不知道如果在实现文件中声明了它到底为什么将不起作用-是否有人了解为什么以及是否有在声明实现的顶部而不是在标头中声明变量的目的? Just curious now more than anything. 现在只是好奇比什么都重要。

In your method doSomething, you are assuming iconWidth is set up properly. 在您的doSomething方法中,假设iconWidth设置正确。

However, iconWidth is created as an autorelease object in loadView method, so when loadView finishes, the main loop will release iconWidth and you are getting random values. 但是,iconWidth在loadView方法中创建为自动释放对象,因此,当loadView完成时,主循环将释放iconWidth,并且您将获得随机值。

To fix this you have to retain iconWidth so you can use it in other methods 要解决此问题,您必须保留iconWidth,以便可以在其他方法中使用它

iconWidth = [[NSNumber numberWithFloat:iconImage.bounds.size.width] retain];

As a general rule, method that doesn't start with init will return autorelease object, so you have to be careful how you instantiate an object and whether you need to call retain on it. 通常,不以init开头的方法将返回autorelease对象,因此必须小心如何实例化对象以及是否需要在其上调用keep。

Synthesizing doesn't make anything private. 合成不会使任何事情变得私有。 It just generates getter/setter methods according to declared properties. 它只是根据声明的属性生成getter / setter方法。

By placing iconWidth in the implementation, outside of any methods, it's essentially a class-level variable. 通过将iconWidth放置在实现中的任何方法之外,实际上是一个类级变量。 So it gets overwritten by the last thing that writes to it. 因此,它会被最后写入的内容覆盖。 You already have imageName declared in the interface, so why not just put iconWidth there as well (instance variables are private by default)? 您已经在接口中声明了imageName ,那么为什么不也将iconWidth放在那里(默认情况下实例变量是私有的)呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM