0

I have swift project. I am stuck at one line to convert in objective c

var bezierPoints = [CGPoint](repeating: CGPoint(), count: 5)

How can I make array in objective C?

3
  • 2
    Would a C array CGPoint bezierPoints[5]; satisfy your needs? Commented Nov 13, 2016 at 15:35
  • The best approach depends on what you're actually trying to do. Most likely, you don't need this line at all. Commented Nov 13, 2016 at 16:12
  • @MartinR yes its satisfy... thanks Commented Nov 14, 2016 at 5:18

1 Answer 1

1

In Objective-C you cannot create an array of CGPoint(s) because NSArray can only contain objects.

Anyway you can wrap a CGPoint into a NSValue so

NSArray * points = [NSArray arrayWithObjects:
                    [NSValue valueWithCGPoint:CGPointZero],
                    [NSValue valueWithCGPoint:CGPointZero],
                    [NSValue valueWithCGPoint:CGPointZero],
                    [NSValue valueWithCGPoint:CGPointZero],
                    [NSValue valueWithCGPoint:CGPointZero],
                    nil];

Or as suggested by @Alexander Momchliov

NSArray * points = @[
                     [NSValue valueWithCGPoint:CGPointZero],
                     [NSValue valueWithCGPoint:CGPointZero],
                     [NSValue valueWithCGPoint:CGPointZero],
                     [NSValue valueWithCGPoint:CGPointZero],
                     [NSValue valueWithCGPoint:CGPointZero],
                    ];
Sign up to request clarification or add additional context in comments.

2 Comments

Why don't you use an array literal? @[a, b, c]
@AlexanderMomchliov: Because I'm forgetting Objective-C :D Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.