Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 915605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:54:58+00:00 2026-05-15T17:54:58+00:00

I have a method to generate a Deck object (NSObject subclass with an NSMutableArray

  • 0

I have a method to generate a Deck object (NSObject subclass with an NSMutableArray property) and it populates the array with Card objects (UIView subclass with some integer and one NSString property). When I ask for a Deck I check to see if one already exists (I think) and if so, release it before getting a new one.

The code from my viewcontroller…

#import "FlashTestViewController.h"

@implementation FlashTestViewController

- (IBAction)generateDeck {

    if (aDeck != nil) {
        [aDeck release];
    }

    aDeck = [[Deck alloc] initDeckWithOperator:@"+"];
}


- (IBAction)generateCard {

    if (aCard != nil) {
        [aCard fadeAway];
    }

    aCard = [aDeck newCardFromDeck];
    [self.view addSubview:aCard];
}

- (void)fadeAway {
    [aCard removeFromSuperview];
    [aCard release];
    }

    @end

The Deck class is as follows…

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

@class Deck;

@interface Deck : NSObject {

    NSMutableArray* cards;
}

@property(nonatomic, retain) NSMutableArray* cards;

- (id)initDeckWithOperator: (NSString*)mathOper;
- (id)newCardFromDeck;

@end

- (id)initDeckWithOperator: (NSString*)mathOper {

    if (cards != nil) {
        [cards release];
    }
    cards = [[NSMutableArray alloc] init];
    for (int i=0; i<11; i++) {
        for (int j=0; j<11; j++) {
            int xPos = (random() % 220) + 10;
            int yPos = (random() % 360) + 10;
            Card* aCard = [[Card alloc] initWithFrame:CGRectMake(xPos, yPos, 60, 80)];
            aCard.upperOperand = i;
            aCard.lowerOperand = j;
            aCard.theOperator = mathOper;
            aCard.theResult = i + j;

            UITextView* upperTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 50, 20)];
        NSString* upperOper = [[NSString alloc] initWithFormat:@"     %d", i];
        upperTextView.text = upperOper;
        [aCard addSubview:upperTextView];
        [upperTextView release];
        [upperOper release];

        UITextView* middleTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 50, 20)];
        NSString* middleOper = [[NSString alloc] initWithFormat:@"%@  %d", mathOper, j];
        middleTextView.text = middleOper;
        [aCard addSubview:middleTextView];
        [middleTextView release];
        [middleOper release];

        UITextView* lowerTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 55, 50, 20)];
        NSString* lowerOper = [[NSString alloc] initWithFormat:@"     %d", j+i];
            lowerTextView.text = lowerOper;
            [aCard addSubview:lowerTextView];
            [lowerTextView release];
            [lowerOper release];

            [cards addObject: aCard];
            [aCard release];
        }
    }
    return self;
}

- (id)newCardFromDeck {
    int index = random() % [cards count];
    Card* selectedCard = [[cards objectAtIndex:index] retain];
    [cards removeObjectAtIndex:index];
    return selectedCard;
}

@end

I do the same thing when I ask for a new card from the newCardFromDeck method and it works. Any suggestions?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:54:59+00:00Added an answer on May 15, 2026 at 5:54 pm

    Add this code to your Deck.m file:

    - (void)dealloc
    {
        [cards release];
        [super dealloc];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 481k
  • Answers 481k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your implementation of the calculation as shown in the Wikipedia… May 16, 2026 at 6:29 am
  • Editorial Team
    Editorial Team added an answer SomeModel.instance_methods(false) & BaseModel.instance_methods The false makes instance_methods not include inherited… May 16, 2026 at 6:29 am
  • Editorial Team
    Editorial Team added an answer First, I use client side logging all of the times.… May 16, 2026 at 6:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.