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
75
votes
11 answers

Print the size (megabytes) of Data in Swift

I have a variable fileData of Data type and I am struggling to find how to print the size of this. In the past NSData you would print the length but unable to do that with this type. How to print the size of a Data in Swift?
user2512523
  • 1,229
  • 2
  • 15
  • 25
60
votes
5 answers

NSData and UIImage

I am trying to load UIImage object from NSData, and the sample code was to NSImage, I guess they should be the same. But just now loading the image, I am wondering what's the best to troubleshoot the UIImage loading NSData issue.
BlueDolphin
  • 9,765
  • 20
  • 59
  • 74
59
votes
6 answers

NSDictionary to NSData and NSData to NSDictionary in Swift

I am not sure if I am using the dictionary or the data object or both incorrectly. I m trying to get used to the switch to swift but I'm having a little trouble. var dictionaryExample : [String:AnyObject] = ["user":"UserName", …
IanTimmis
  • 815
  • 1
  • 8
  • 16
51
votes
7 answers

How to convert String to byte in Swift?

How to convert String to byte in Swift? Like String .getBytes()in Java.
YuXuan Fu
  • 1,911
  • 3
  • 14
  • 13
46
votes
1 answer

Is there a writeToFile equivalent for Swift 3's 'Data' type?

I've some code which returns the new iOS 10 / Swift 3 NSData replacement(?) type: Data if let jpegData = UIImageJPEGRepresentation(newImage, 0.8) { ... } I want to write this image to disk, however NSData's writeToFile: method is not present for…
Kyle G
  • 4,347
  • 4
  • 26
  • 39
44
votes
4 answers

Converting HEX NSString To NSData

I'm trying to convert a Hex NSString to NSData (I'm using the below attached code). The following is the output: <00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000> which looks totally irrelevant to me. Any idea/ suggestions…
Pranav Jaiswal
  • 3,752
  • 3
  • 32
  • 50
43
votes
9 answers

Slow start for AVAudioPlayer the first time a sound is played

I'm trying to eliminate startup lag when playing a (very short -- less than 2 seconds) audio file via AVAudioPlayer on the iPhone. First, the code: NSString *audioFile = [NSString stringWithFormat:@"%@/%@.caf", [[NSBundle mainBundle] resourcePath],…
John Biesnecker
  • 3,782
  • 2
  • 34
  • 43
43
votes
6 answers

Converting between NSData and base64 strings

What is the easiest and fastest code to do a conversion between NSData and a base64 string? I've read a bunch of solutions at SO and mostly they involve in adding another class etc. I found a great solution here but it's too complex.
aherlambang
  • 14,290
  • 50
  • 150
  • 253
43
votes
3 answers

NSData to display as a string

I am building an iPhone app and stuck with the following: unsigned char hashedChars[32]; CC_SHA256([inputString UTF8String], [inputString lengthOfBytesUsingEncoding:NSASCIIStringEncoding], hashedChars); NSData *hashedData = [NSData…
topace
  • 1,791
  • 3
  • 17
  • 23
43
votes
7 answers

Converting NSData to base64

How to convert NSData to base64. I have NSData and want to convert into base64 how can I do this?
mactalent
  • 971
  • 2
  • 13
  • 24
40
votes
2 answers

Swift 3.0: Data to JSON [String : Any]

Evening, I'm trying to creating an APIClient, but I'm having a problem with a warning: APIClient.swift:53:81: Cast from 'Data' to unrelated type '[String : Any]' always fails In this code I'm trying to convert Data into JSON as a dictionary [String…
Andrea Miotto
  • 7,084
  • 8
  • 45
  • 70
40
votes
3 answers

NSData & NSURL - url with space having problem

I have following code in my application. NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]]; pathOfThumbNail has following path http://70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg When I open above path in…
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
40
votes
3 answers

convert NSData Length from bytes to megs

I am trying to NSLog the number of megs my NSData object is however currently all I can get is bytes by using NSLog(@"%u", myData.length); So how would I change this NSLog statement so I can see something like 2.00 megs any help would be…
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183
37
votes
5 answers

NSData and Uploading Images via POST in iOS

I have been combing through the many many posts about uploading images via POST in iOS. Despite the wealth of information on this topic, I cannot manage to correctly upload JPEG data taken from my iPhone Simulator Photo Library. The data, once on…
corescan
  • 477
  • 1
  • 5
  • 10
37
votes
7 answers

How to convert an Int into NSData in Swift?

In Objective-C I use the following code to Convert an Int variable into NSData, a packet of bytes. int myScore = 0; NSData *packet = [NSData dataWithBytes:&myScore length:sizeof(myScore)]; Use the converted NSData variable into a method. [match…
Cesare
  • 9,139
  • 16
  • 78
  • 130