【问题标题】:Access Data from a Array of NSDictionaies in a Singleton Class and Displaying that data in a TableView (XCode))从单例类中的 NSDictionaies 数组访问数据并在 TableView (XCode) 中显示该数据)
【发布时间】:2012-07-20 12:38:48
【问题描述】:

我有一个单例类,其中包含一个 NSDictionary 数组(其中 5 个)。我需要在不同的 ViewController 中显示这些数据(我正在使用 Storyboard)。

如果我从数组中提取数据,我可以访问数据,但现在我需要从 NSDictionary 数组中提取数据。我想出了这一行:

cell.textLabel.text =  [[[[Singleton sharedInstance] movies]  objectAtIndex:indexPath.row] objectForKey:@"title"];

但这似乎不起作用。

有什么想法吗?我将把我的 Singleton 类和我的 TableView 类:

Singleton.h

#import <Foundation/Foundation.h>
#import "AppDelegate.h"

@interface Singleton : NSObject {

    NSDictionary *batman;
    NSDictionary *superman;

    NSArray *superheoes;

}

@property(nonatomic,retain) NSDictionary *batman;
@property(nonatomic,retain) NSDictionary *superman;

@property(nonatomic,retain) NSArray *superheroes;

+ (Singleton *) sharedInstance;
+ (AppDelegate *) sharedAppDelegate;

@end

Singleton.m

#import "Singleton.h"

@implementation Singleton

@synthesize batman, superman;

+ (Singleton *) sharedInstance {

    //The instance of this class is store here
    static Singleton *myInstance = nil;

    // We check to see if an instance already exists
    if (nil == myInstance) {
        myInstance = [[[self class] alloc] init];
    }

    return myInstance;

}

-(id) init {

    if (self=[super init]) {

        batman = [[NSDictionary alloc] initWithObjectsAndKeys: 
                             @"name", @"Back to the Future", 
                             @"year", @"1985", nil];

        superman = [[NSDictionary alloc]initWithObjectsAndKeys:
                    @"name", @"Superman", 
                    @"year", @"1990", nil];

        superheroes = [NSArray arrayWithObjects:batman,superman, nil]; 
    }
    return self;
}

#pragma mark access to app delegate etc.
+ (AppDelegate *) sharedAppDelegate; {
    return (AppDelegate*)[[UIApplication sharedApplication] delegate];
}



@end

SuperheroesDatabase.m

#import "SuperheroesDatabase.h"
#import "Singleton.h"

@interface SuperheroDatabase ()

@end

@implementation MovieDatabase

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

    // Configure the cell...

    if(cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }


    cell.textLabel.text =  [[[[Singleton sharedInstance] movies]  objectAtIndex:indexPath.row] objectForKey:@"name"];
    //cell.detailTextLabel.text =  [NSString stringWithFormat:@"row %i", indexPath.row];

    return cell;
}

【问题讨论】:

  • 应该可以,有什么不行的?
  • Apple 可能会因为使用 Marvel、DC Comics 等的注册名称(例如蝙蝠侠或超人)而拒绝您的应用 :)
  • @Kashiv:名字只是一个例子
  • Dan F:我的 TableView 中没有显示任何内容
  • NSLog "[[Singleton sharedInstance] movies]" 并查看字典包含的内容 - 可能没有键“title”

标签: objective-c ios xcode nsarray nsdictionary


【解决方案1】:

我认为您的问题是您的数组和字典没有被正确保留。尝试在您的 init 方法中添加 retain 调用:

-(id) init {

    if (self=[super init]) {

        batman = [[[NSDictionary alloc] initWithObjectsAndKeys: 
                             @"name", @"Back to the Future", 
                             @"year", @"1985", nil] retain];

        superman = [[[NSDictionary alloc]initWithObjectsAndKeys:
                    @"name", @"Superman", 
                    @"year", @"1990", nil] retain];

        superheroes = [[NSArray arrayWithObjects:batman,superman, nil] retain]; 
    }
    return self;
}

【讨论】:

  • 嘿@Dan F,我使用 ARC 重要吗?
  • @ARFIRST 这很重要,但我认为您不是因为您在上面的代码中将属性声明为 retain 而不是 strong。如果情况仍然如此(您正在使用 ARC 并且属性仍然是 retain,请尝试将它们更改为 strong
  • @ARFIRST 抱歉,如果我的第一条评论让您感到困惑,我编辑了它。今天早上咖啡还没喝完,我原来误读了你的评论
  • 有人刚刚向我提到它有什么问题。它是对象和键,我应该使用 @"Back to the Future", @"name" 而不是 @"name", @"Back to the Future"
  • @ARFIRST 啊是的,一个常见的错误,你先列出对象,然后是它的键。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多