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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:44:11+00:00 2026-05-30T01:44:11+00:00

In my iPad app, I have the following function: + (void)addDot:(Dot *)d { if(dots

  • 0

In my iPad app, I have the following function:

+ (void)addDot:(Dot *)d {
    if(dots == nil)
        dots = [[NSMutableArray alloc] init];

    NSLog(@"before adding, dots = %@",dots);
    NSLog(@"adding dot %@",d);
    [dots addObject:d];
    NSLog(@"dots is now %@",dots);
}

Note that printing a dot results in the x,y coordinates separated by a space.

Everytime the subject taps the screen, a dot is drawn, and this method is called, which adds a dot. Note that dots is defined as static NSMutableArray *dots at the top of the class this function is in. Something weird is going on. Every element of the array is replaced. Look at this output from NSLog at the very beginning after tapping the screen, making 2 dots:

before adding, dots = (
)
2012-02-13 23:58:48.159 MoreMost[520:707] adding dot 418.000000 548.000000
2012-02-13 23:58:48.161 MoreMost[520:707] dots is now (
    "418.000000 548.000000"
)
2012-02-13 23:58:48.748 MoreMost[520:707] before adding, dots = (
    "635.000000 410.000000"
)
2012-02-13 23:58:48.749 MoreMost[520:707] adding dot 635.000000 410.000000
2012-02-13 23:58:48.750 MoreMost[520:707] dots is now (
    "635.000000 410.000000",
    "635.000000 410.000000"
)

See how the whole array is being replaced with the incoming element? Why is this? And no, the function is not being called anywhere else in the code. It is only called once, each time the user taps the screen to draw a dot at that location).

Note that the dots are not being used anywhere else in the program. Only this function.

Here is the implementation of Dot:

#import "Dot.h"

@implementation Dot
@synthesize tapPosition;
CGRect frame;
UIColor *dotColor;

float radius = 20;

- (id)initWithTapPosition:(CGPoint)pt color:(UIColor *)col {
    if(self = [super init]) {
    tapPosition = pt;
    float topLeftX = pt.x - radius;
    float topLeftY = pt.y - radius;
    if(topLeftX + (radius*2) >= 1024)
        return nil;
    if(topLeftY + (radius*2) >= 728)
        return nil;
    if(topLeftY <= 0 || topLeftX <= 0)
        return nil;

    frame = CGRectMake(topLeftX, topLeftY, radius*2, radius*2);
    dotColor = col;
    }
    return self;
}

- (id)copyWithZone:(NSZone *)zone
{
   Dot *dot = [[[self class] allocWithZone:zone] initWithTapPosition:tapPosition color:dotColor];
   return dot;
}

- (CGRect)getFrame {
    return frame;
}

- (UIColor *)getColor {
    return dotColor;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"%f %f",frame.origin.x,frame.origin.y];
}

@end

and the header:

    #import <Foundation/Foundation.h>

@interface Dot : NSObject {
@public
    CGPoint tapPosition;
}

@property (nonatomic, assign) CGPoint tapPosition;

- (CGRect)getFrame;
- (id)initWithTapPosition:(CGPoint)pt color:(UIColor *)col;

- (UIColor *)getColor;
@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-05-30T01:44:12+00:00Added an answer on May 30, 2026 at 1:44 am

    Move the

    CGRect frame;
    UIColor *dotColor;
    CGPoint tapPosition;
    float radius = 20;
    

    from @implementation to the @interface:

    @interface Dot : NSObject
    {
       CGRect frame;
       // etc.
    }
    ...
    @end
    

    The way you’re declaring them make them actually “global variables”, so all Dot instances will share the same value. Putting them in the @interface make them “instance variables” instead so each Dot can have different values.


    Old answer

    Note that

    [dots addObject:d];
    

    only adds a reference of the dot d. If you modify the dot in-place later e.g.

    d.x = 123;
    d.y = 456;
    

    then the one in the array will see the same change also.

    You need to add a copy instead, e.g.

    [dots addObject:[d copy]];
    // note: you need to implement -copyWithZone: with the <NSCopying> protocol
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following button in my iPad app: subject = [[UIButton alloc] init];
I have the following UI for an iPad app: When I click on the
I have set my iPad app to Landscape mode using following code.. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
I have the following requirements for an iPad App and would like to know
I have the following structure in my app (iPad, iOS 5): UIView1 --> UIView2
I have the following code in my app. Its an iPad app, with five
I have a iPad app with the following code tied to a button's (bMyDocuments)
I have a few labels on my first Ipad App. How to make them
I'm creating an iPad app, and I have two classes: NWRootViewController : UITableViewController and
I have an iPhone app and would like to create iPad version of it.

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.