1

I suspect the solution to what I'm trying to do is fairly straight forward--yet, I'm unable to get it working myself.

Here's what I'm trying to do: I've built a card game that is initialized with the cards when the game loads. Now, some of those cards have certain properties (card type, card name, as well as a special array). The card type and card name objects are fairly easy to retrieve since they're just one object (and I can call using objectatindex). However, the special array contains several keywords that fluctuate depending on which card is chosen. So instead of initializing these keywords one by one (like I did for card type and card name), I put them into their own special array.. or an array within an array. Here's my code:

itemObjects class:

@synthesize cardName=_cardName;
@synthesize cardType=_cardType;

-(id) initWithCardName:(NSString*)cardName initWithCardType:(NSString*)cardType initWithSpecialArray:(NSArray*) specialArray{
    self=[super init];
    if (self){
        _cardName=cardName;
        _cardType=cardType;
    }
    return self;
}

model class

-(NSMutableArray*)deck{
    if (_deck==nil){
        _deck=[[NSMutableArray alloc]initWithObjects:
               [[itemObjects alloc]initWithCardName:@"The Long Way" initWithCardType:@"bill" initWithSpecialArray:[[NSArray alloc]initWithObjects:@"fast", @"high", nil]],
               [[itemObjects alloc]initWithCardName:@"A Short Cut" initWithCardType:@"bill" initWithTrendArray:[[NSArray alloc]initWithObjects:@"small", @"tall", nil]],nil];

View Controller class (this is where I'm trying to call one of the objects, "fast" for example, but with no success

NSString* testing=[[[self.model.deck objectAtIndex:indexPath.row]arrayForKey:@"specialArray"]objectAtIndex:0];
NSLog(@"%@",testing);

I believe I've initialized my "specialArray" correctly and the issue is with how I'm attempting to call it but if I've made a mistake there, any advice would be much appreciated. Thanks!

EDIT: This particular issue has been solved thanks to WendiKidd. It turned out that I wasn't initializing my specialArray correctly. This has led to a separate issue which I have linked to here. I've also posted my corrected code below for those interested in the future:

@synthesize cardName=_cardName;
@synthesize cardType=_cardType;
@synthesize specialArray=_specialArray;

-(id) initWithCardName:(NSString*)cardName initWithCardType:(NSString*)cardType initWithSpecialArray:(NSArray*) specialArray{
    self=[super init];
    if (self){
        _cardName=cardName;
        _cardType=cardType;
        _specialArray=specialArray;
    }
    return self;
}
2
  • 2
    I've rolled back the question to display your original code; still working on figuring out how else to help so I can update my answer for you, but I brought this up on meta and was told that the question itself should stay in its original state. :) Commented Aug 17, 2013 at 0:27
  • Thanks for all your help so far! I'm pretty sure I've pinpointed the issue--just updated the code to show where the specialArray isn't being transferred properly in the deck to hand method. Commented Aug 17, 2013 at 0:43

1 Answer 1

2

You aren't initializing your special array at all. You pass it to initWithCardName, but you're never setting it to anything. You need to store the special array inside that class, just like you do with cardName and cardType.

Secondly, I can't make heads or tails of the line where you're trying to access the special array. You haven't given us the proper information to see where the object is being stored to tell if you're even properly accessing a card object, but I definitely don't see where you've ever used the key @"specialArray" before, so there's no reason to expect that will return anything. However the rest of your data structure works, at some point you're going to have an object of whatever class the initWithCardName function initializes (for example purposes I'm going to go ahead and call it CardObject). To get the info from the special array, you're going to have to save an object called specialArray into the CardObject class, as already mentioned. Then you can write something like this:

CardObject* card = //[whatever you have to do to access the right card object]
for(NSString* specialAttribute in card.specialArray)
{
    NSLog(@"Special Attribute: %@", specialAttribute);
}

And that should print all the special attributes of the card quite nicely.

If you really do just want the first item in the list, as your example was trying to access, this should work just fine:

CardObject* card = //[whatever you have to do to access the right card object]
NSLog(@"Special Attribute: %@", [card.specialArray objectAtIndex:0]);
Sign up to request clarification or add additional context in comments.

12 Comments

Good catch! Not initializing the specialArray is the reason why it wasn't showing up in the view controller. However, it is still returning a null when I try to grab the info (the specialArray is called in ViewController, I had it there before using arrayForKey, because it wasn't auto-appearing--but just updated it to a better way).
@user2552070 Make sure all your objects exist. That is, make sure the card object you're working from isn't nil. If that checks out, make sure the specialArray isn't nil. If both those check out, show me the new line you're using to try and print the data and I'll see what I can figure out :)
Hmmm, the object definitely exists--as I'm able to print out the cardName and cardType just fine. As for the specialArray, I'm pretty sure it's not nil though I'm not sure how I can tell this if it won't print out. Here's the method that I'm trying to call it, which is similar to the one you suggested: NSString* testing=[[[self.model.deck objectAtIndex:indexPath.row]specialArray]objectAtIndex:0]; NSLog(@"%@",testing);
@user2552070 Good test. The way to check if the array isn't nil is to print it out in the console; put a breakpoint on the NSLog line and when the program pauses, go to the output console and type po card.specialArray (only use whatever you have to do to refer to the array properly). It should print either the value of the array (a valid memory location) or nil. I see the method call... If you can print cardName etc. from the object but not anything from the array, I'd think the array is either empty or nil (best guess).
Ok, just checked. It's definitely empty. So that means I must not be initializing it correctly. Does the way I initialized it in the model class seem correct to you?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.