Converting a CGPoint to NSValue
Asked Answered
H

7

19

In CABasicAnimation.fromValue I want to convert a CGPoint to a "class" so I used NSValue valueWithPoint but in device mode or simulator one is not working... need to use NSMakePoint or CGPointMake if in device or simulator.

Hermilahermina answered 21/4, 2009 at 14:52 Comment(0)
L
53

There is a UIKit addition to NSValue that defines a function

+ (NSValue *)valueWithCGPoint:(CGPoint)point

See iPhone doc

Lobbyism answered 21/4, 2009 at 15:24 Comment(1)
Thanks, very helpful, but consider that these methods copy values. Checkout my answer.Frodeen
F
13

@ashcatch 's answer is very helpful, but consider that those methods from addition copy values, when native NSValue methods store pointers! Here is my code checking it:

CGPoint point = CGPointMake(2, 4);
NSValue *val = [NSValue valueWithCGPoint:point];
point.x = 10;
CGPoint newPoint = [val CGPointValue];

here newPoint.x = 2; point.x = 10


CGPoint point = CGPointMake(2, 4);
NSValue *val = [NSValue valueWithPointer:&point];
point.x = 10;
CGPoint *newPoint = [val pointerValue];

here newPoint.x = 10; point.x = 10

Frodeen answered 19/6, 2012 at 17:52 Comment(1)
But be careful with the second approach since in the example, you store a pointer to a memory location on the heap. So if you use the created NSValue object (or rather the pointer stored in it) after you left the scope of the point declaration, your application might crash.Lobbyism
T
3

In Swift, you can change a value like this:

    var pointValueare = CGPointMake(30,30)
    NSValue(CGPoint: pointValueare)
Topgallant answered 25/4, 2015 at 5:47 Comment(0)
S
1

&(cgpoint) -> get a reference (address) to cgpoint (NSPoint *)&(cgpoint) -> casts that reference to an NSPoint pointer *(NSPoint )(cgpoint) -> dereferences that NSPoint pointer to return an NSPoint to make the return type happy

Shoat answered 4/5, 2011 at 21:56 Comment(0)
M
1

In Swift the static method is change to an initialiser method:

var pointValue = CGPointMake(10,10)
NSValue(CGPoint: pointValue)
Man answered 17/4, 2015 at 13:26 Comment(0)
D
0

If you are using a recent-ish version of Xcode (post 2015), you can adopt the modern Objective-C syntax for this. You just need to wrap your CGPoint in @():

CGPoint primitivePoint = CGPointMake(4, 6);
NSValue *wrappedPoint = @(primitivePoint);

Under the hood the compiler will call +[NSValue valueWithCGPoint:] for you.

https://developer.apple.com/library/archive/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html

Deen answered 21/2, 2019 at 17:8 Comment(0)
F
-2

Don't think of it as "converting" your point-- NSValue is a wrapper class that holds primitive structs like NSPoint. Anyway, here's the function you need. It's part of Cocoa, but not Cocoa Touch. You can add the entire function to your project, or just do the same conversion wherever you need it.

NSPoint NSPointFromCGPoint(CGPoint cgpoint) {
   return (*(NSPoint *)&(cgpoint));
}
Flatus answered 21/4, 2009 at 15:12 Comment(3)
Can you please explain the syntax used in your reply? Looks useful.Convertiplane
that syntax is wrong, so the function, whatever it is… (it's not explained), is useless.Caye
The syntax is correct, but the cast is useless, CGPoint and NSPoint are the same type (NSPoint it typedef'd to be a CGPoint).Apotheosis

© 2022 - 2024 — McMap. All rights reserved.