Pregunta

I have searched around a bit and not found an answer to my question. I am a beginner to objective-c and I am currently experimenting around with dictionaries. I have a class called "SETestBank" that creates 3 dictionaries of images and then adds them to a large dictionary. I am doing it this way because down the line I may add more smaller dictionaries. I've created a specific "accessBank" method to pull objects out via other classes.

SETestBank.m

#import "SETestBank.h"

@implementation SETestBank
@synthesize mathBankA, mathBankB, mathBankC, mathTest;

- (id)init
{
     [self createBankA];
     [self createBankB];
     [self createBankC];
     [self createTest];
     return 0;
 }

 - (NSMutableDictionary *)createBankA
 {
     mathBankA = [[NSMutableDictionary alloc] init];
     for (int i=0; i < 11; i++) {
         NSString *aKey = [NSString stringWithFormat:@"%da", i];
         NSString* imageName = [NSString stringWithFormat:@"%da.png",i];
         [mathBankA setObject:imageName forKey:aKey];
         NSLog(@"%@", aKey);
     }
     return mathBankA;
 }

 //same occurs to generate bankB and bankC

 - (NSMutableDictionary *)createTest
 {
     mathTest = [[NSMutableDictionary alloc] init];

     [mathTest addEntriesFromDictionary:mathBankA];
     [mathTest addEntriesFromDictionary:mathBankB];
     [mathTest addEntriesFromDictionary:mathBankC];

     return mathTest;
 }

 - (NSString *)accessBank:(NSString *)accessor
 {
     NSString *img = [mathTest objectForKey:accessor];

     return img;
 }

 @end

now this code seems to work fine. The dictionaries are created and console logs display the correct keys and object filenames (the images are all in the project and properly named)

However, when I access it in my view controller and try to apply an image to a UIImageView I get nothing.

SEViewController:

- (void)viewDidLoad
{
    SETestBank *mathTest = [[SETestBank alloc] init];

    UIImage *img = [UIImage imageNamed:[mathTest accessBank:@"1a"]];
    NSString *test = [mathTest accessBank:@"1a"];
    NSLog(@"%@", test);
    [imageView setImage:img];
    [self.view addSubview:imageView];
    [super viewDidLoad];
}

the log here simply returns null along with a "CUICatalog: Invalid asset name supplied: (null), or invalid scale factor: 1.000000" error which I gather is from trying to assign null to an image. I'm stumped on this one. If I hardcode the image to display "1a.png" rather than trying to access it by key it works fine but that is not what I'm trying to accomplish. imageView is connected to a UIImageView in storyboard and the view controller is set to use SEViewController as it's class.

Thanks for any help!

¿Fue útil?

Solución

In the init method, you return a 0, which means nothing (nil)

- (id)init
{
     [self createBankA];
     [self createBankB];
     [self createBankC];
     [self createTest];
     return 0;
}

Change this to

- (id)init
{
    if (self = [super init]) {
      [self createBankA];
      [self createBankB];
      [self createBankC];
      [self createTest];
    }
    return self;
}

may solve your problem.

Otros consejos

Your init in SETestBank is returning 0, so your mathTest object is nil when you do

 SETestBank *mathTest = [[SETestBank alloc] init];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top