nsdata

Swift 3.0: Data to JSON [String : Any]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 00:43:24
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 : Any] . I guess the compiler can't know if this cast could or could not be possible so it throws the error, but I'm pretty sure it will work. So how can I avoid this warning or how can I write safer code? case 200: do { let json = try JSONSerialization.data(withJSONObject: data!, options: []) as? [String : Any] completion(json, HTTPResponse, nil)

How can i convert a NSData to NSArray? [duplicate]

不羁的心 提交于 2019-12-04 00:29:58
Possible Duplicates: How to convert NSArray to NSData? Need to convert NSData to NSArray HI, I have a NSData object named as "myNsData"(it is converted form of NSArray) and a NSarray named as "myNsArray. can any one provide me a code to convert a NSData to NSArray? Simon Lee See this post... Convert NSArray to NSData The last comment deals with the reverse.... NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data] I am not sure but can you try NSArray *myNsArray = [NSKeyedUnarchiver unarchiveObjectWithData:myNsData]; 来源: https://stackoverflow.com/questions/6108924/how-can-i-convert

NSData compare to NSData in percents

守給你的承諾、 提交于 2019-12-03 23:15:26
问题 I Have NSData *object1 and another NSData *object2 . How can I compare this objects by what percentage they are similar? For example: Object1 similar to Object2 in - 99%. Thanks. 回答1: Get the bytes in both cases and iterate through checking how many of them are equal. uint8_t* bytes1 = (uint8_t*)[object1 bytes]; uint8_t* bytes2 = (uint8_t*)[object2 bytes]; NSUInteger sameCount = 0; for (NSUInteger i = 0 ; i < MIN([object1 length], [object2 length]) ; ++i) { if (bytes1[i] == bytes2[i]) {

How to encode NSData as base64? (iPhone/iPad)

馋奶兔 提交于 2019-12-03 23:07:16
问题 I'm trying to add a UIImage to an HTML email, however I'm having an issue. I have looked around at lots of other posts and they all state that I need to convert my NSData to base64 , however the method they all suggest doesn't seem to exist anymore (unless I'm missing something?!). Here is the code I have so far: UIImage *mapImage = [mapViewController convertMapToImage]; NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(mapImage)]; NSString *base64String = // [imageData

How to attach CGPDFDocument/CGPDFPage to MFMailComposeViewController

倾然丶 夕夏残阳落幕 提交于 2019-12-03 23:01:39
问题 I have a muti page pdf document stored on local storage. I want to extract any page out of that pdf doc and convert it into NSData to attach it with 'MFMailComposeViewController'. With the following lines of code, I can easily retrive the required page... CGPDFDocumentRef pdfDoc=CGPDFDocumentCreateWithURL(pdfURL); CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfDoc, pageNumber); But I am unable to find a way to convert pdfPage into NSData so that I can attach it with mail. NOTE: The

UIDocumentInteractionController Calendar Access

本秂侑毒 提交于 2019-12-03 21:33:55
I have an ics (Calendar) file that I'm opening with a UIDocumentInteractionController , using presentOptionsMenuFromRect: . When this runs, the "Open In" menu looks like this . As you can see, no "Add to Calendar" option. Here's what gets me: I'm using the same exact code for a .vcf (contact card) file, and it works as expected with the "Open In Contacts" option available. Am I missing some sort of permission in my Info.plist for calendar access? Why can't UIDocumentInteractionController handle the .ics file type correctly, but .vcf works just fine? Those two file types are very similar. From

Objective C SHA512 hash of two NSData

一世执手 提交于 2019-12-03 21:28:27
Here is a Java code, which computes SHA512 hash of a byte array with salt: private static String DIGEST_ALGORITHM = "SHA-512"; public static byte[] getHash(final byte[] data, final byte[] salt) throws NoSuchAlgorithmException { final MessageDigest md = MessageDigest.getInstance(DIGEST_ALGORITHM); md.reset(); if (salt != null) { md.update(salt); } return md.digest(data); In Objective C, I use this algorithm for compute the hash of an NSData: @implementation NSData (CommonDigest) - (NSData *) SHA512Hash { unsigned char hash[CC_SHA512_DIGEST_LENGTH]; (void) CC_SHA512( [self bytes], (CC_LONG)[self

Difference between dataWithBytesNoCopy and dataWithBytes?

随声附和 提交于 2019-12-03 20:21:23
What is the difference between + (instancetype)dataWithBytes:(const void *)bytes length:(NSUInteger)length; and + (instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length; Also, + (instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b; if b == YES , will it free the bytes automatically after converted to data? I am working on an app and almost finished it. But the last problem is it crashes with memory error when it runs on device. It only crashes when on device, but in simulator it is perfect. "malloc: * error for object 0x17415d0c0:

Converting NSString to NSData and vice versa

爷,独闯天下 提交于 2019-12-03 19:52:20
问题 I am having an issue while trying to convert NSString to NSData and vice versa. I am trying to store encrypted string to my database. For that I am using AES algorithm. Now what I am doing is I get encrypted NSData and I am converting this to NSString using following: // Not woking NSString *strTemp = [[NSString alloc] initWithData:encData encoding:NSUTF8StringEncoding]; // Working NSString *strTemp = [[NSString alloc] initWithData:encData encoding:NSASCIIStringEncoding]; Why NSData is not

NSData dataWithContentsOfURL cache

血红的双手。 提交于 2019-12-03 17:50:08
问题 NSData +dataWithContentsOfURL has any kind of caching by default? Has anyone experimented some kind of problems using this method, what is the most efficient way to get Data from the web? 回答1: The documentation doesn't say that it will cache, so I think we should assume that they don't do any caching. Which kinds of data you want to get UIImage : yes, I think you should use NSData Video: you should use MPMoviePlayerController for streaming Text: I think you can do normal NSUrlConnection. It