 |
 |
Adding stucts to NSMutableArray
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Nov 2002
Status:
Offline
|
|
How can I add NSPoint structs to a NSMutableArray. When I try [array addObject  oint] the compiler complains that point is not an object. Is there an easy way to convert an NSPoint to an object, or is there a way to add structs to a NSMutableArray?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Status:
Offline
|
|
The easiest way to convert an NSPoint to an object would be to write a wrapper class. You can't add structs directly to NSArray-family classes.
|
|
[vash:~] banana% killall killall
Terminated
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
Even easier would be to use NSStringFromPoint() and NSPointFromString().
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by Rickster:
Even easier would be to use NSStringFromPoint() and NSPointFromString().
Or, alternately, [NSValue valueWithPoint:].
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2000
Status:
Offline
|
|
As an aside, you can always add any old structure to an NSMutableArray, NSMutableDictionary or other items that need an object by wrapping it in an NSData.
Something like [NSData dataWithBytes:mystruct length:sizeof(mystruct)] should create an NSData for use with this, assuming mystruct is a pointer to the data you want to use.
- proton
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Nov 2002
Status:
Offline
|
|
What I'm trying to do is record the points of mouse clicks into an NSArray. I take the location of the event, turn it into an NSPoint, and then I want to add them to an NSMutableArray for later access. I've thought about making the two members into NSNumbers then adding them to the array. Then when i want to access them, take those two numbers, convert them to primitive ints, and use them in the memebers of a NSPoint. Here's the code:
- (void)mouseDown:(NSEvent *)event
{
NSNumber * pointX, * pointY;
NSPoint eventLocation = [event locationInWindow];
pointX = [NSNumber numberWithInt:eventLocation.x] ;
pointY = [NSNumber numberWithInt:eventLocation.y];
[clickLocations addObject:pointX];
[clickLocations addObject:pointY];
}
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status:
Offline
|
|
The difficulty with adding a struct is that you can only add Objective-C objects to an NSArray or NSDictionary. Anything else must be in a wrapper class of some type. Several people have already listed pre-existing wrapper classes for an NSPoint. Your choices are to use one of those or to write your own.
As far as using an NSNumber, this just means that you will have to come up with a quick and dirty equation to figure out which locations in the array refer to which points. One of the other methods would be cleaner and less prone to error on your part (though the NSNumber thing would be difficult to mess up and miss in the debugging).
|

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by Spiffster:
What I'm trying to do is record the points of mouse clicks into an NSArray. I take the location of the event, turn it into an NSPoint, and then I want to add them to an NSMutableArray for later access.
This is exactly what NSValue is for.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
If, for some reason, you don't want to use the wrapper for NSPoint, you could always insert a dictionary object into your array. Untested code follows:
Code:
NSDictionary *pointDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: eventLocation.x],
@"X-Coordinate",
[NSNumber numberWithFloat: eventLocation.y],
@"Y-Coordinate",
nil];
[clickLocations addObject: pointDictionary];
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Since everybody else is posting examples, I suppose I'd just look rude not to. Here's an example with NSValue:
Code:
NSPoint point1 = { 1, 1 };
NSPoint point2;
NSArray *myArray = [NSArray arrayWithObject: [NSValue valueWithPoint:point1] ];
point2 = [[myArray objectAtIndex:0] pointValue];
//point2 now equals {1, 1}
I don't really know how this would be more efficient than the string route, but it certainly seems better than storing the two floats separately.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Status:
Offline
|
|
Why not just add the NSEvent to the array?
|
|
I be that insane n***a from the psycho ward.
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Nov 2002
Status:
Offline
|
|
Originally posted by type_r503:
Why not just add the NSEvent to the array?
Never thought of that. BTW, is there any conflict if i add multiple objects into a NSArray that have the same name? ex. multiple objects named clickPoint.
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by Spiffster:
Never thought of that. BTW, is there any conflict if i add multiple objects into a NSArray that have the same name? ex. multiple objects named clickPoint.
If you're referring to the name of the variable, no. You're passing the memory address contained in the variable, not the variable itself.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Nov 2002
Status:
Offline
|
|
Well, I'm still having trouble adding these values to the NSMutable array. Even though as far as I know, my code is right, the objects are not being added to the array. code is as follows:
MyView.h
Code:
/* MyView */
#import <Cocoa/Cocoa.h>
@interface MyView : NSView
{
NSMutableArray * clickArray;
}
- (IBAction)drawCircle:(id)sender;
- (IBAction)drawLine:(id)sender;
- (IBAction)drawRectangle:(id)sender;
@end
MyView.m
Code:
#import "MyView.h"
@implementation MyView
- (id) init
{
clickArray = [[NSMutableArray alloc] init];
[super init];
return self;
}
-(void)mouseDown:(NSEvent *)event
{
NSValue * click = [NSValue valueWithPoint:[event locationInWindow]];
[clickArray addObject:click];
}
I stop execution right after mouseDown finishes [clickArray addObject:click]. Then i type
Code:
(gdb) print [clickArray count]
$1 = 0
(gdb) print-object [clickArray objectAtIndex:0]
<nil>
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status:
Offline
|
|
it would be a good idea to retain your array in the init function. Otherwise, the array will be deallocated the first time the event loop finishes.
|

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2000
Status:
Offline
|
|
Originally posted by Detrius:
it would be a good idea to retain your array in the init function. Otherwise, the array will be deallocated the first time the event loop finishes.
I thought if you called alloc the instance's reference count was incremented. If you use one of the class convenience methods that returns an autoreleased instance you have to retain. Correct?
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Aug 2000
Location: UK
Status:
Offline
|
|
Originally posted by int69h:
I thought if you called alloc the instance's reference count was incremented. If you use one of the class convenience methods that returns an autoreleased instance you have to retain. Correct?
Yes, you are right and Detrius is wrong. If you alloc and object, then you only need to release it when you are done (you don't need to retain it again).
One thing I would suggest is using NSLog(@"clickArray = %@", clickArray); rather than using breakpoints. If anything, it means that you can debug without having to use the debugger (which takes ages to start up).
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Originally posted by calumr:
One thing I would suggest is using NSLog(@"clickArray = %@", clickArray); rather than using breakpoints. If anything, it means that you can debug without having to use the debugger (which takes ages to start up).
But then you have to recompile between moving breakpoints about, yadda yadda... They both have their place 
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status:
Offline
|
|
Another way to write the function to make things a bit more direct:
Code:
-(void)mouseDown:(NSEvent *)event
{
[clickArray addObject:[NSValue valueWithPoint:[event locationInWindow]]];
}
Another thing is that this is the only way that I have ever used NSMutableArrays:
Code:
clickArray = [NSMutableArray arrayWithCapacity: INITIAL_CAPACITY];
I've only been doing this stuff for a year and a half, so I could have this wrong, but I'm actually trying to look at this stuff for you, which I don't often do. It's just an idea.
(Last edited by Detrius; Jan 31, 2003 at 11:58 AM.
)
|

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
The problem is that NSView's designated initializer is initWithFrame:, not init, so clickArray is never getting created.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Nov 2002
Status:
Offline
|
|
Thanks everyone, problem solved. 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|