Questions tagged [nsdata]

The NSData class is an Apple class for holding generic data. Often used when reading/writing from and to files, and the internet.

From the Apple Developer Library:

NSData and its mutable subclass NSMutableData provide data objects, object-oriented wrappers for byte buffers. Data objects let simple allocated buffers (that is, data with no embedded pointers) take on the behavior of Foundation objects.

NSData creates static data objects, and NSMutableData creates dynamic data objects. NSData and NSMutableData are typically used for data storage and are also useful in Distributed Objects applications, where data contained in data objects can be copied or moved between applications.

The size of the data is subject to a theoretical limit of about 8 ExaBytes (in practice, the limit should not be a factor).

2485 questions
37
votes
4 answers

Create an Array in Swift from an NSData Object

I'm trying to store an array of integers to disk in swift. I can get them into an NSData object to store, but getting them back out into an array is difficult. I can get a raw COpaquePointer to the data with data.bytes but can't find a way to…
tassinari
  • 2,313
  • 2
  • 24
  • 32
36
votes
3 answers

converting data back into a string

So I have this: NSData *charlieSendData = [[charlieImputText stringValue] dataUsingEncoding:NSUTF8StringEncoding]; I know how to convert NSStrings to data but how I convert data back to an NSString? Elijah
objectiveccoder001
  • 2,981
  • 10
  • 48
  • 72
34
votes
1 answer

Convert unsigned char array to NSData and back

How do you convert an unsigned char array to an NSData in objective c? This is what I am trying to do, but it doesn't work. Buffer is my unsigned char array. NSData *data = [NSData dataWithBytes:message length:length];
Bewn
  • 575
  • 4
  • 9
  • 13
34
votes
4 answers

Determine MIME type from NSData?

How would you determine the mime type for an NSData object? I plan to have the user to upload a video/picture from their iPhone and have that file be wrapped in a NSData class. I was wondering if I can tell the mime type from the NSData. There are…
blee908
  • 12,165
  • 10
  • 34
  • 41
32
votes
2 answers

Can I create an NSURL that refers to in-memory NSData?

The docs for NSURL state that: An NSURL object represents a URL that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data. I have a blob of…
Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
31
votes
3 answers

Converting custom class object into NSData

I have a custom class that I want to save into NSUserDefaults. I am told that I need to convert the class object into data in order to save it to NSUserDefaults. I found a lot of discrete string or ints to NSData examples but nothing on custom class…
Tamarisk
  • 929
  • 2
  • 11
  • 27
31
votes
7 answers

Converting JSON to NSData, and NSData to JSON in Swift

I'm having problems converting a JSON element into NSData, and an NSData variable back into JSON in Swift. Firstly, I'd like to extract the encryptedData element of the following JSON data: { "transactionID" : 12345, "encryptedData" :…
John Smith
  • 415
  • 1
  • 6
  • 11
31
votes
4 answers

NSMutableData remove bytes?

I can add bytes to a NSMutableData instance easily by using the appendData method, however I do not see any similar method for removing data? Am I overlooking something, or do I need to create a new object and copy over only the bytes I need?
Kyle
  • 17,317
  • 32
  • 140
  • 246
27
votes
3 answers

Copy a part of NSData byte array to another NSData type

I have an original NSData type which contains let's say 100 bytes. I want to get 2 other NSData types. The first containing the first 20 bytes of the 100, and the second one containing the other 80. They should be copied from the original NSData.…
Claudio Ferraro
  • 4,551
  • 6
  • 43
  • 78
27
votes
1 answer

Split NSData objects into other NSData objects of a given size

I have an NSData object of approximately 1000kB in size. Now I want to transfer this via Bluetooth. It would be better if I have, let's say, 10 objects of 100kB. It comes to mind that I should use the -subdataWithRange: method of NSData. I haven't…
27
votes
5 answers

Swift structs to NSData and back

I have a struct containing a struct and an NSObject that I want to serialize into an NSData object: struct Packet { var name: String var index: Int var numberOfPackets: Int var data: NSData } var thePacket = Packet(name: name, index: i,…
niklassaers
  • 8,480
  • 20
  • 99
  • 146
27
votes
2 answers

How do we clear out contents in NSMutableData

How do we clear an NSMutableData without using release and then re-alloc/init again to be used again? I was looking at resetBytesInRange to be set at zero but I am unsure of this. Anyone can help?
Frank
  • 3,073
  • 5
  • 40
  • 67
27
votes
2 answers

Iphone - How to encrypt NSData with public key and decrypt with private key?

I am converting a UIImage to NSData. Now I need to encrypt that NSData using a public key and I need to decrypt using a private key. Please provide a step by step procedure. Which algorithm do I need to use? Is there any good library for encryption…
sachi
  • 2,122
  • 7
  • 30
  • 46
24
votes
5 answers

NSData won't accept valid base64 encoded string

I'm implementing JSON Web Token authentication on the iOS (7) cient-side. It's working nicely. My app rceives tokens, and can make authenticated calls to my server with them. Now, I want my client side code to check for an expiration date on the…
TomorrowPlusX
  • 1,205
  • 2
  • 14
  • 27
23
votes
2 answers

Convert AVAudioPCMBuffer to NSData and back

How to convert AVAudioPCMBuffer to NSData? If it should be done as let data = NSData(bytes: buffer.floatChannelData, length: bufferLength) then how to calculate bufferLength? And how to convert NSData to AVAudioPCMBuffer?
Shmidt
  • 16,436
  • 18
  • 88
  • 136