Thursday, February 2, 2012

iOS - Store CGPoint in NSArray or NSDictionary

Ever tried to add a CGPoint variable to an NSArray or NSDictionary?
Did you received the following error message ?
error: sending 'CGPoint' (aka 'struct CGPoint') to parameter of incompatible type 'id'

If so than following code snippet should be of good help to you:

NSArray *myPoints = [NSArray arrayWithObjects:
                     [NSValue valueWithCGPoint:CGPointMake(10.f,20.f)],
                     nil];


To restore your CGPoint value again just use following :

NSValue *myValue = [myPoints objectAtIndex:0];
CGPoint myPoint = [myValue CGPointValue];
Or in short :

CGPoint myPoint = [[myPoints objectAtIndex:0] CGPointValue] 

In case you never came across NSValue, just have a read through on the NSValue Class Reference.
As you can see, NSValue can come in quite handy.

9 comments:

  1. Assigning to 'CGPoint *' (aka 'struct CGPoint *') from incompatible type 'CGPoint' (aka 'struct CGPoint')

    ReplyDelete
  2. Please make sure you declare your CGPoint Variable without the Asterisk (*). You can't declare a CGPoint as a Pointer.
    Please have a close look at the example code.

    ReplyDelete
  3. These are some great tools that i definitely use for SEO work. This is a great list to use in the future.. Maharashtra Ration Card Download

    ReplyDelete
  4. Your music is amazing. You have some very talented artists. I wish you the best of success. The Best VPN

    ReplyDelete
  5. When you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style. cydia impactor ios12

    ReplyDelete
  6. Impressive web site, Distinguished feedback that I can tackle. I am moving forward and may apply to my current job as a pet sitter, which is very enjoyable, but I need to additional expand. Regards https://mejoresvpn.com

    ReplyDelete
  7. What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. hoverwatch discount

    ReplyDelete
  8. Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. Mobile phone

    ReplyDelete