How to Convert Cgpoint in Nsvalue in Swift

Converting a CGPoint to NSValue

There is a UIKit addition to NSValue that defines a function

+ (NSValue *)valueWithCGPoint:(CGPoint)point

See iPhone doc

How to convert CGPoint in NSValue in swift?

Try something like that:

let point = CGPointMake(9, 9)
var pointObj = NSValue(CGPoint: point)

CGPoint to NSValue and reverse

The class NSValue has methods +[valueWithPoint:] and -[CGPointValue]? Is this what you are looking for?

//Getting mouse coordinates
NSMutableArray *vertices = [[NSMutableArray alloc] init];
CGPoint location = [self convertPoint:event.locationInWindow fromView:self];
NSValue *locationValue = [NSValue valueWithPoint:location];
[vertices addObject:locationValue];

//Converting from NSMutableArray to GLFloat to work with OpenGL
NSUInteger count = vertices.count * 2; // * 2 for the two coordinates
GLFloat GLVertices[] = (GLFloat *)malloc(count * sizeof(GLFloat));
for (NSUInteger i = 0; i < count; i++) {
NSValue *locationValue = [vertices objectAtIndex:i];
CGPoint location = locationValue.CGPointValue;
GLVertices[i] = location.x;
GLVertices[i] = location.y;
}

convert NSMutableArray to CGPoint Array

Here is something that will create a closed path from an NSArray of NSValue created with CGPoint using the code your provided:

BOOL isCGPoint(NSValue *value){
return value && strcmp([value objCType], @encode(CGPoint)) == 0;
}

- (CGPathRef) closedPathFromPointArray:(NSArray *)points{
CGMutablePathRef path = CGPathCreateMutable();

if(points.count){
CGPoint origin = ((NSValue *)points[0]).CGPointValue;
CGPathMoveToPoint (path, NULL, origin.x, origin.y);

// see https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGPath/#//apple_ref/c/func/CGPathAddLines
for(NSValue *value in points){
CGPathAddLineToPoint (path, NULL, value.CGPointValue.x, value.CGPointValue.y);
}
}

CGPathCloseSubpath(path);
return path;
}

As you see, you don't really need malloc, or even creating a C array of CGPoint. This assumes you only need this array for creating the closed path.
Two extra things of note:

  • See the commented link for CGPathAddLines, as it describes how CGPathAddLines works internally. This gives you the hint about how to go about this.
  • The isCGPoint function is included so you can test if a given NSValue instance was actually created using [NSValue valueWithCGPoint:]. My previous answer checked this, but I thought it was ooverkill to check everywhere. In any case, it's included here for didactic purposes.

Convert MKMapPoint to NSValue in Swift

Sadly this is currently not possible in Swift.

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 expose an NSArrayNSValue of CGPoint as [CGPoint] to Swift?

You cannot write, in Obj-C, a property that is of type [CGPoint] in Swift.

What you can do is annotate your vertices property with NS_REFINED_FOR_SWIFT. This will expose it to Swift as a property named __vertices instead. Now you can write an extension on your class in Swift that provides its own var vertices: [CGPoint] computed property. It might look like

var vertices: [CGPoint] {
return __vertices.map({ $0.cgPointValue })
}

The downside to this approach is every time you access the vertices property from Swift it will have to allocate a brand new array. But there's not really any way around that if you want to expose a [CGPoint] (besides trying to see if you can implement any sort of caching behavior, where you only create the new [CGPoint] if the original NSArray<NSValue *> changed).

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].



Related Topics



Leave a reply



Submit