Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Adding stucts to NSMutableArray

Adding stucts to NSMutableArray
Thread Tools
Fresh-Faced Recruit
Join Date: Nov 2002
Status: Offline
Reply With Quote
Jan 26, 2003, 10:30 PM
 
How can I add NSPoint structs to a NSMutableArray. When I try [array addObjectoint] 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
Reply With Quote
Jan 26, 2003, 11:59 PM
 
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
Reply With Quote
Jan 27, 2003, 12:31 AM
 
Even easier would be to use NSStringFromPoint() and NSPointFromString().
Rick Roe
icons.cx | weblog
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jan 27, 2003, 03:44 AM
 
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
Reply With Quote
Jan 27, 2003, 10:45 AM
 
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
Reply With Quote
Jan 27, 2003, 10:55 PM
 
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: Asheville, NC
Status: Offline
Reply With Quote
Jan 27, 2003, 11:13 PM
 
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
Reply With Quote
Jan 28, 2003, 01:05 AM
 
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
Reply With Quote
Jan 28, 2003, 01:12 AM
 
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
Reply With Quote
Jan 28, 2003, 02:28 AM
 
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
Reply With Quote
Jan 28, 2003, 12:34 PM
 
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
Reply With Quote
Jan 28, 2003, 01:34 PM
 
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
Reply With Quote
Jan 28, 2003, 02:09 PM
 
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
Reply With Quote
Jan 30, 2003, 08:00 PM
 
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: Asheville, NC
Status: Offline
Reply With Quote
Jan 31, 2003, 01:31 AM
 
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
Reply With Quote
Jan 31, 2003, 07:40 AM
 
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: Sep 2000
Location: UK
Status: Offline
Reply With Quote
Jan 31, 2003, 08:44 AM
 
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
Reply With Quote
Jan 31, 2003, 12:24 PM
 
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: Asheville, NC
Status: Offline
Reply With Quote
Jan 31, 2003, 01:30 PM
 
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 01:58 PM. )
ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jan 31, 2003, 07:02 PM
 
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
Reply With Quote
Jan 31, 2003, 07:31 PM
 
Thanks everyone, problem solved.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -4. The time now is 02:16 AM.
All contents of these forums © 1995-2015 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2015, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2