1

I have an NSValue that I am returning from an NSDictionary. The NSValue was created using sizeWithAttributes. I would like to know how to get the NSValue back into a value where I can use it to create the label size.

I get the size of the label calculated from this

betweensLabelSize = [betweensString sizeWithAttributes: @{
                      NSFontAttributeName: [UIFont systemFontOfSize:14.5f]
                }];

I put this into the NSDictionary as a NSValue and retrieve like so

NSValue *tempSize = [currAxisDictionary objectForKey:@"betweensLabelSize"];

which returns this value

NSSize: {64.278503, 86.4925}
halfer
  • 19,824
  • 17
  • 99
  • 186
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

2 Answers2

2

To be clear, you presumably created the NSValue using +[NSValue valueWithCGSize:]. -sizeWithAttributes: didn't create the NSValue; it returned a CGSize which you wrapped in an NSValue.

Anyway, you can get the CGSize out of the NSValue object by calling -CGSizeValue on it:

CGSize size = [tempSize CGSizeValue];
Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
0

Try below code for accessing the size value:-

NSValue *val=[mut objectForKey:@"betweensLabelSize"];
CGSize sz=val.CGSizeValue;
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • Please mention the reason for downvote. So that will correct the answer. – Hussain Shabbir Nov 30 '14 at 22:47
  • 1
    Sorry but everything in your answer is incorrect. The OP's line to get the `NSValue` `tempSize` from the dictionary is just fine. It does *not* return a `CGSize`, it returns an `NSValue`. Your suggested solution will crash. The OP's goal is to extract the `CGSize` value from the `NSValue`. See the docs for `NSValue` for a simple way to do this. – rmaddy Nov 30 '14 at 23:37
  • `NSValue` does not have a property named `sizeValue`. Perhaps you meant to use the `CGSizeValue` method. – rmaddy Dec 01 '14 at 02:57
  • Right that only i meant – Hussain Shabbir Dec 01 '14 at 02:58