An NSValue object is a simple container for a single C or Objective-C data item. It can hold any of the scalar types such as int, float, and char, as well as pointers, structures, and object ids. The purpose of this class is to allow items of such data types to be added to collections such as instances of NSArray and NSSet, which require their elements to be objects. NSValue objects are always immutable.
Questions tagged [nsvalue]
64 questions
3
votes
2 answers
iOS: Sorting NSArray of NSValue wrapping CGPoint
Qeustion: Pretty much as stated in the title. I looked over the internet but cannot find anything that is easy to understand. I have NSArray that contains many NSValue. Those NSValue each hold a CGPoint. I want to sort them by x then by y…

Byte
- 2,920
- 3
- 33
- 55
2
votes
2 answers
Convert NSValue to NSData and back again, with the correct type
I would like to be able to convert an objective-c object, such as an NSArray or UIImage into an NSData object, which I can then use to write to disk. I first converted them to an NSValue, which I then planned on converting to NSData. This question…

Hamish
- 78,605
- 19
- 187
- 280
2
votes
2 answers
How to know the type of the object boxed in NSValue with Swift?
I have an NSValue object that can "box" an instance of an unknown type (CGPoint, CGRect, etc.) and I need to determine this type in runtime. How would I do it in Swift?
I tried to do something like this:
if ((value as Any) is CGPoint) {}
else if…

Bruno Morgado
- 507
- 1
- 8
- 26
2
votes
1 answer
Get buffer pointer from `NSValue`
I'd like to get a pointer to the object stored in an NSValue so that I can modify it. The gist being like this:
// Get a pointer to the value's buffer (it'd be void*, I guess).
CGRect *myRect = [rectValue getPointer];
// Modify the…

Benjohn
- 13,228
- 9
- 65
- 127
2
votes
2 answers
how to pass a constant in NSDictionary
I'm wondering if there is a way to pass a constant like UITableViewStylePlain into NSDictionary.
I am passing value types using NSValue like so:
CGRect myRec = CGRectMake(0,0,320,568);
NSDictionary *myDic = @{@"val":[NSValue…

JackyJohnson
- 3,106
- 3
- 28
- 35
2
votes
1 answer
Swift compile error, subclassing NSValue, using super.init(nonretainedObject:)
This code
class ID : NSValue {
init(baseObject: T) {
super.init(nonretainedObject: baseObject)
}
}
gives this compiler error:
error: must call a designated initializer of the superclass 'NSValue'
…

Clay Bridges
- 11,602
- 10
- 68
- 118
2
votes
2 answers
Swift vs. +[NSValue valueWithNonretainedObject:]
In Objective-C, I might use +[NSValue valueWithNonretainedObject:] to keep a unique ID of an object, where I don't wish to retain the object itself. Seems like that's deprecated for Swift.
How to do in Swift?

Clay Bridges
- 11,602
- 10
- 68
- 118
2
votes
2 answers
NSKeyedArchiver: How to archive an NSValue containing a custom struct
Is there any way I can use an NSKeyedArchiver to encode an NSValue that contains a custom struct?. I have an NSDictionary which contains structs wrapped in NSValues and I want to archive this as part of a larger object graph but I receive the…

Rory O'Bryan
- 1,894
- 14
- 22
2
votes
1 answer
Can't withdraw CGPoint Values from A mutableArray of NSValue
I made an NSMUtableArray of CGpoints and I stored them by subclassing as NSValue, but I cant get the values out. here I what I did.
CGPoint graphPoint;
NSMutableArray *pointValues = [[NSMutableArray alloc] init];
for( int x =0;x<5; x++)
…

Terrel Gibson
- 481
- 1
- 6
- 21
2
votes
1 answer
NSValue/NSNumber creation of a given encoding/objCType
I'm trying to sync objects over GameCenter, accessing their values with KVC on both sides. Setting numeric values using setValue:forKey: requires them to be NSNumber objects.
NSValue initWithBytes:objCType: gives me NSValue objects even passing…

weezor
- 2,621
- 1
- 16
- 10
2
votes
3 answers
NSMutableArray: add and extract struct
I'm trying to store some data in an NSMutableArray. This is my struct:
typedef struct{
int time;
char name[15];
}person;
This is the code to add a person:
person h1;
h1.time = 108000;
strcpy(h1.name, "Anonymous");
[highscore…

charles
- 713
- 1
- 6
- 16
1
vote
1 answer
NSSecureCoding crash in Core Data
I am getting crash reports from users when fetching from my Core Data entity with a fetch request. This is what the crash looks like:
SIGABRT: Unhandled error (NSCocoaErrorDomain, 4864) occurred during
faulting and was thrown: Error…

Z S
- 7,039
- 12
- 53
- 105
1
vote
1 answer
NSData vs NSValue ? how to encapsulate a C-structure in sendMIDISysExEvent:(NSData *)midiData
I'm trying work out the way to use sendMIDISysExEvent:(NSData *)midiData as a way to retune a handful of MIDI notes in a music app. I'd like the ViewController to send a MIDI message to the Synth class to change the tuning of 10 notes. The standard…

Greg
- 1,750
- 2
- 29
- 56
1
vote
2 answers
Use NSValue to wrap a C pointer
I have a C type pointer variable:
a_c_type *cTypePointer = [self getCTypeValue];
How can I convert cTypePointer to NSObject type & vise versa?
Should I use NSValue? What is the proper way to do so with NSValue?

Leem.fin
- 40,781
- 83
- 202
- 354
1
vote
1 answer
NSValue getValue: method returns garbage value on iPhone 6 but correct value on iPhone 5
I am storing an NSInteger inside NSValue and while retrieving the value back I am getting a garbage value. The interesting part is that I am getting this garbage value on iPhone 6 and the correct one on iPhone 5.
Here is a part of my code:
NSValue…

enigma
- 865
- 9
- 22