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 9029109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:10:16+00:00 2026-06-16T07:10:16+00:00

Please take a look of my code 1st. I am trying to add some

  • 0

Please take a look of my code 1st. I am trying to add some Car objects to a MutableArray *parkPlace (which a property belongs to Park class)

My expecting result should be showing as below:

CarParkSim[2662:303] Parking: Car Number:1 At:3 o'clok Position:0
CarParkSim[2662:303] Parking: Car Number:2 At:4 o'clok Position:1
CarParkSim[2662:303] Parking: Car Number:3 At:5 o'clok Position:2

However, the actualy result I got is like this:

2012-12-22 12:13:46.085 CarParkSim[2662:303] Parking: Car Number:1 At:3 o'clok Position:0
2012-12-22 12:13:46.087 CarParkSim[2662:303] Parking: Car Number:2 At:4 o'clok Position:0
2012-12-22 12:13:46.088 CarParkSim[2662:303] Parking: Car Number:3 At:5 o'clok Position:0

I did check the NSmutablearray, it is empty…it dosent seem to push anything to the stack…
any suggestions? thanks

anyway, here is the code:
Park class:

#import <Foundation/Foundation.h>
#import "Stack.h"
#import "Car.h"

@interface Park : NSObject
@property(strong,nonatomic)NSMutableArray *parkPlace;
@property(strong,nonatomic)Car *car;
-(void)carMoveIntoPark:(Car *)car;
@end

#import "Park.h"
@implementation Park

-(void)carMoveIntoPark:(Car *)car
{

if ([self.parkPlace count] <= 2) {

    Car *car1 = [[Car alloc] init];
    car1.carNumber = 1;
    car1.timeIO = 3;

    [self.parkPlace push:car1];
    int carNumber = car.carNumber;
    int carTimer = car.timeIO;
    NSUInteger parkPosition = [self.parkPlace count];
    NSLog(@"Parking: Car Number:%d At:%d o'clok Position:%ld", carNumber, carTimer, parkPosition);

} else {
    //to be implement later
} 
}
 @end

and here is my Main:

int main(int argc, const char * argv[])
{
Car *car1 = [[Car alloc] init];
car1.carNumber = 1;
car1.timeIO = 3;

Car *car2 = [[Car alloc] init];
car2.carNumber = 2;
car2.timeIO = 4;

Car *car3 = [[Car alloc] init];
car3.carNumber = 3;
car3.timeIO = 5;

Park *park = [[Park alloc] init];
[park carMoveIntoPark:car1];
[park carMoveIntoPark:car2];
[park carMoveIntoPark:car3];

and this is my Car class:

#import <Foundation/Foundation.h>

@interface Car : NSObject
@property(assign,nonatomic)int carNumber;
@property(assign,nonatomic)int timeIO;

@end


#import "Car.h"
@implementation Car
@end

By the way, I m trying to use stack data structure to push cars on to a ‘stack’, since there is no such data structure in objective c, so i have implemented my own, here is the stack class:

#import <Foundation/Foundation.h>

@interface NSMutableArray (Stack)
-(void) push:(id)item;
-(id)pop;
-(id)peek;
-(void)replaceTop:(id)item;

@end


#import "Stack.h"

@implementation NSMutableArray (Stack)

-(void)push: (id)item{
[self addObject:item];
}

-(id)pop {
id item = nil;
if ([self count] != 0) {
    item = [self lastObject];
    [self removeLastObject];
}
return item;
}

-(id)peek{
id item = nil;
if([self count] != 0) {
    item= [self lastObject];
}
return item;
}

-(void) replaceTop:(id)item{
if([self count] == 0) {
    [self addObject:item];
}else{
    [self removeLastObject];
    [self addObject:item];
}
}

@end
  • 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-06-16T07:10:17+00:00Added an answer on June 16, 2026 at 7:10 am

    You don’t allocate memory anywhere for your array. That’s what constructors have been invented for:

    @implementation Park
    
    - (id)init
    {
        if ((self = [super init])) {
            parkPlace = [[NSMutableArray alloc] init];
        }
        return self;
    }
    
    - (void)dealloc
    {
        // The destructor needs to be implemented as well
        [parkPlace release];
        [super dealloc];
    }
    
    // Other parts of the implementation here
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please take a look at this code: template<class T> class A { class base
Hi please take a look at this fiddle http://jsfiddle.net/bloodygeese/pwjNQ/1/ I am trying to figure
Please take a look at the code snippet below. I want the 2nd MongoDB
Please take a look at following code: #include <stdio.h> #include <iostream> using namespace std;
I'm trying to solve problem effects during the loading the page. please take look
I want to create button to add tabs with body tabs Please take look
Please take a look at my code: #include <windows.h> #include <Sti.h> #include <iostream> #pragma
Can you please take a look at following code and let me know why
Please take a look at the code. It shouldn't take long to have a
Please take a look at following code: public class SomeEntity { public int Id

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.