How to Add Cgpoint Objects to an Nsarray the Easy Way

How can I add CGPoint objects to an NSArray the easy way?

With UIKit Apple added support for CGPoint to NSValue, so you can do:

NSArray *points = [NSArray arrayWithObjects:
[NSValue valueWithCGPoint:CGPointMake(5.5, 6.6)],
[NSValue valueWithCGPoint:CGPointMake(7.7, 8.8)],
nil];

List as many [NSValue] instances as you have CGPoint, and end the list in nil. All objects in this structure are auto-released.

On the flip side, when you're pulling the values out of the array:

NSValue *val = [points objectAtIndex:0];
CGPoint p = [val CGPointValue];

How do I add a CGPoint to NSMutableArray?

The problem is that CGPoint is actually just a C structure it is not an object:

struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;

If you are on the iPhone you can use the NSValue UIKit additions to convert the CGPoint to an NSValue object.

See this previous answer for examples: How can I add CGPoint objects to an NSArray the easy way?

Is there a way to put a scalar type like CGPoint (or any self created) into a NSArray?

Make it an object. You could try this:

CGPoint point = CGPointMake(1.f,1.f);

[NSValue valueWithCGPoint:point];

This goes for pretty much every scalar you want to put in an NSArray:

CGFloat foo = 1.f;

[NSNumber numberWithFloat:foo];

How to form CGPoint array in Objective-C?

C arrays are not really arrays at run time, where they are just pointers to a contiguous block of objects of the same type. When you see items[n], that's just syntactic sugar for *(items+n).

In your example addLines[1] would be *(lines+1) and addLines[0] would be *(lines+0), which is *lines. So, addLines is just lines without the pointer dereference. *lines is the first item in the array and lines is the whole array.

Arrays have some differences to pointers at compile time. For example, sizeof(addLines) would give you the size of the whole array.

Array-ness is lost as soon as you pass the array somewhere where it's size might be variable, but you can still use the subscript operator. For example:

#include <Foundation/Foundation.h>

#define showsize( expr ) ( printf(#expr " = %zd\n", ( expr ) ) )

CGPoint *
pass_back(CGPoint points[4])
{
showsize(sizeof(points));
return points;
}

int
main(void)
{
CGPoint square[] = {CGPointMake(-1.0, 1.0),
CGPointMake( 1.0, 1.0),
CGPointMake( 1.0, -1.0),
CGPointMake(-1.0, -1.0)};
CGPoint* returned;
int i;

showsize(sizeof(CGPoint));
showsize(sizeof(CGPoint*));
showsize(sizeof(square));
returned = pass_back(square);
showsize(sizeof(returned));

for (i = 0; i < 4; ++i) {
printf("returned[%d] = {%0.1f, %0.1f}\n", i, (float) returned[i].x,
(float) returned[i].y);
}

return 0;
}

This outputs the following on my Mac:


sizeof(CGPoint) = 8
sizeof(CGPoint*) = 4
sizeof(square) = 32
sizeof(points) = 4
sizeof(returned) = 4
returned[0] = {-1.0, 1.0}
returned[1] = {1.0, 1.0}
returned[2] = {1.0, -1.0}
returned[3] = {-1.0, -1.0}

Here, square is the size of four CGPoints, but once sent to the pass_back function, it's only the size of a pointer, because that's what it is. When the pointer comes back (and named returned) it can still be used like an array.

Note the magic number 4 in the loop. The pointer doesn't know the length of the array it's pointing to.

Arrays cannot be reassigned with the = operator. If you really must populate addLines with the points from lines, you can do that with something like the following:

memcpy(addLines, lines, sizeof(CGPoint) * numberOfPoints);

You'll have to get numberOfPoints from somewhere, and addLines will have to be large enough to handle those points. That's okay if the number of points is a constant, but it would be bad if the number of points can vary at run time, especially if the points come from the outside world (think arbitrary code execution).

I'd change averageResponseTimePoints to return an NSArray rather than a C-style array. You'll need to encapsulate the CGPoints in objects - either your own object or NSValues.

Here's an example of how you could write averageResponseTimePoints:

- (NSArray*) averageResponseTimePoints
{
NSMutableArray* result = [[[NSMutableArray alloc] init] autorelease];

for (int i = 0; i < numberOfPoints; ++i) {
NSValue* point = [NSValue value:points+i
withObjCType:@encode(CGPoint)];
[result addObject:point];
}

return result;
}

If your code runs with CocoaTouch, you can use this to create the point value instead:

NSValue* point = [NSValue valueWithCGPoint:points[i]];

To get the CGPoints out of the array, you could write something like this:

for (NSValue* value in result) {
NSPoint pt;
[value getValue:&pt];
NSLog(@"%f %f", pt.x, pt.y);
}

Or with CocoaTouch:

CGPoint pt = [value CGPointValue];

How to Find the nearest CGPoint and make a cluster from NSArray?

You need to check if object already exist before adding them. Something like this:

for (CGPoint nextPoint in cgPointGroupArray) {
if (30>[self distanceBetween:point and: nextPoint]) {
if (![shortestClusterArr containsObject:nextPoint])
[shortestClusterArr addObject:nextPoint];
}
else{
if (![longestClusterArr containsObject:nextPoint])
[longestClusterArr addObject:nextPoint];
}
}

AnyObject To CGPoint

Your translatesPoints method returns an NSArray that contains NSValues that wrap CGPoints. Let's create such an array:

let arr:NSArray = [NSValue(CGPoint: CGPointMake(1,2)), NSValue(CGPoint: CGPointMake(3,4))]

You can get the values from this array and call CGPointValue() on them:

for val in arr as [NSValue] {
let point = val.CGPointValue()
println("CGPoint = (\(point.x), \(point.y))")
}

If you want, you can convert the entire NSArray to a Swift array of CGPoints like this:

let points = (arr as [NSValue]).map({$0.CGPointValue()})

Now points has the type [CGPoint].

Adding integers from a file to NSarray

best way to add points in an array is

[NSValue valueWithCGPoint:point]    

where point is of type CGPoint.

while fetching you can fetch the CGPoint as

CGPoint point = [value CGPointValue];
where value is [NSValue valueWithCGPoint:point]

Moving Shapes to Random Points stored in an NSArray in C4

The error you are getting is indicating that it can't convert the id of the NSValue to a CGPoint. There is a similar discussion at this link:

How can I add CGPoint objects to an NSArray the easy way?

What you need to do is get the CGPoint back from the NSValue. NSValue provides the CGPointValue for this very situation. So, from the NSValue you get back from the NSArray you would call the CGPointValue method to get you the last step. If you had an NSArray named array and a C4Shape named Square you could do this:

    Square.center = [array[randomInteger] CGPointValue];

Where randomInteger would be some integer that you picked somewhere else.

Add CGPDFDocumentRef to NSDictionary or NSArray

You can easily add CGPDFDocumentRef to your NSArray or NSMutableArray as long as you cast them to appropriate types when adding/accessing.

Here is a sample code that I tested:

CFStringRef path;
CFURLRef url;
CGPDFDocumentRef document;

NSString *nsstringPath = [[NSURL pathForDocumentsDirectory] stringByAppendingPathComponent:@"test.pdf"];

path = CFStringCreateWithCString (NULL, [nsstringPath cStringUsingEncoding:NSUTF8StringEncoding], kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);

CFRelease (path);
document = CGPDFDocumentCreateWithURL (url);
CFRelease(url);
count = CGPDFDocumentGetNumberOfPages (document);

NSMutableArray *testArray = [NSMutableArray array];
[test addObject:(__bridge id)(document)];

CGPDFDocumentRef testPdf = (__bridge CGPDFDocumentRef)([testArray objectAtIndex:0]);

Here is a link to Apple docs covering toll-free bridging. It covers Foundation framework though, but should give an idea.

Hope this helps, Cheers!



Related Topics



Leave a reply



Submit