iOS 应用程序中实例变量不返回值

发布于 2024-11-19 23:18:28 字数 3183 浏览 3 评论 0原文

此问题可能与 实例变量未在 iOS 应用程序中保留值有关< /a>.

我的应用程序有一个表视图,应该用 plist 中的数据填充。 plist 问题是根据默认设置确定的。

设置实例变量的内容后,计数显示它包含多条记录。但是,如果我稍后在绘制表视图单元格时尝试计算此实例变量,则它不会显示任何记录。

--

我在类头中声明了一个“providerData”NSDictionary 实例变量:

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController {

    NSDictionary *providerData;

}

@property (nonatomic, retain) NSDictionary *providerData;

- (void)loadProviderDataSource:(NSString *)providerName;

@end

在我的实现中,我使用 viewDidLoad 从 plist 文件中读取字典,检查“provider_preference”的值,然后调用 [self loadProviderDataSource:providerPreference ]

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Retrieve the user's provider preference from defaults:
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    // Set a default value for provider preference if the user didn't explicitly set one:
    NSString *providerPreference = [[NSString alloc] initWithString:@"foo"];

    if ([defaults stringForKey:@"provider_preference"]) {
        providerPreference = [defaults stringForKey:@"provider_preference"];
    }

    // Load provider data from the relevant plist:
    [self loadProviderDataSource:providerPreference];
}

这一切都工作正常(使用 NSLog 我可以看到 plist 文件被正确读取,并且正确的providerPreference 值被传递给 loadProviderDataSource。loadProviderDataSource

的实现如下如下所示:

- (void)loadProviderDataSource:(NSString *)providerName
{
    // Set the filename and path for the data source:
    NSString *dataSourceFileName = [[NSString alloc] initWithFormat:@"data-%@.plist",providerName];
    NSString *dataSourcePath = [[NSBundle mainBundle] bundlePath];
    dataSourcePath = [dataSourcePath stringByAppendingPathComponent:dataSourceFileName];

    // Read the provider data:
    self.providerData = [[NSDictionary alloc] initWithContentsOfFile:dataSourcePath];

    NSLog(@"provider data entry count 1: %d", [self.providerData count]);
}

此时,NSLog 显示providerData 中有 2 个条目。

但是,当我必须为表视图设置单元格时,以下代码中的 NSLog 显示providerData 有 0 个条目:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSLog(@"provider data entry count 2: %d", [self.providerData count]);

    return cell;
}

在 loadProviderDataSource 中,我已经尝试使用:

self.providerData = [[NSDictionary alloc] initWithContentsOfFile:dataSourcePath];

和:

[self setProviderData:[[NSDictionary alloc] initWithContentsOfFile:dataSourcePath]];

...两者都不起作用,我仍然在我的日志中得到这个:

provider data entry count 1: 2
provider data entry count 2: 0
provider data entry count 2: 0

有什么想法吗?

This problem might be related to Instance variable not retaining value in iOS application.

My application has a table view that's supposed to be populated with data from a plist. The plist is question is determined based on a defaults setting.

After setting the contents of the instance variable, a count on it shows that it contains multiple records. However, if I then later try to count this instance variable when drawing the table view cells, it shows no records.

--

I declared a "providerData" NSDictionary instance variable in my class header:

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController {

    NSDictionary *providerData;

}

@property (nonatomic, retain) NSDictionary *providerData;

- (void)loadProviderDataSource:(NSString *)providerName;

@end

In my implementation I use viewDidLoad to read a dictionary from a plist file, check the value of "provider_preference", and then call [self loadProviderDataSource:providerPreference]:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Retrieve the user's provider preference from defaults:
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    // Set a default value for provider preference if the user didn't explicitly set one:
    NSString *providerPreference = [[NSString alloc] initWithString:@"foo"];

    if ([defaults stringForKey:@"provider_preference"]) {
        providerPreference = [defaults stringForKey:@"provider_preference"];
    }

    // Load provider data from the relevant plist:
    [self loadProviderDataSource:providerPreference];
}

This all works fine (using NSLog I can see that the plist file is read correctly and that a correct providerPreference value is passed to loadProviderDataSource.

The implementation for loadProviderDataSource is as follows:

- (void)loadProviderDataSource:(NSString *)providerName
{
    // Set the filename and path for the data source:
    NSString *dataSourceFileName = [[NSString alloc] initWithFormat:@"data-%@.plist",providerName];
    NSString *dataSourcePath = [[NSBundle mainBundle] bundlePath];
    dataSourcePath = [dataSourcePath stringByAppendingPathComponent:dataSourceFileName];

    // Read the provider data:
    self.providerData = [[NSDictionary alloc] initWithContentsOfFile:dataSourcePath];

    NSLog(@"provider data entry count 1: %d", [self.providerData count]);
}

At this point the NSLog shows me that there are 2 entries in providerData.

However, when I have to set the cells for my table view, the NSLog in the following code shows me that providerData has 0 entries:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSLog(@"provider data entry count 2: %d", [self.providerData count]);

    return cell;
}

In loadProviderDataSource I've tried using:

self.providerData = [[NSDictionary alloc] initWithContentsOfFile:dataSourcePath];

and:

[self setProviderData:[[NSDictionary alloc] initWithContentsOfFile:dataSourcePath]];

... neither work and I still end up with this in my log:

provider data entry count 1: 2
provider data entry count 2: 0
provider data entry count 2: 0

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2024-11-26 23:18:29

基于 NSLog(@"%@", self); 在显示如下的方法中

loadProviderDataSource

self: <ViewController: 0x83739b0> 

tableView:cellForRowAtIndexPath

self: <ViewController: 0x8373170>

它看起来像您正在以某种方式创建 ViewController 的第二个实例。

据我所知,其中之一是使用 XIB 创建的。验证您是否正在以编程方式创建另一个。

Based on NSLog(@"%@", self); in both the method which shows up as follows

loadProviderDataSource

self: <ViewController: 0x83739b0> 

tableView:cellForRowAtIndexPath

self: <ViewController: 0x8373170>

It looks like you are creating a second instance of ViewController somehow.

I understand one of them is created using XIB. Verify whether you are creating another one programmatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文