nsdata

Can't convert NSData to NSString in swift

╄→гoц情女王★ 提交于 2019-11-28 07:03:06
This is my function. First println print correct hash to console but in the next row program crashes. Can you help me? func sha256(string: NSString) -> NSString { var data : NSData! = string.dataUsingEncoding(NSUTF8StringEncoding) var hash = [UInt8](count: Int(CC_SHA256_DIGEST_LENGTH), repeatedValue: 0) CC_SHA256(data.bytes, CC_LONG(data.length), &hash) let res = NSData(bytes: hash, length: Int(CC_SHA256_DIGEST_LENGTH)) println(res) let resstr = NSString(data: res, encoding: NSUTF8StringEncoding) println(resstr) return resstr } let resstr = NSString(data: res, encoding: NSUTF8StringEncoding)

Guess encoding when creating an NSString from NSData

一个人想着一个人 提交于 2019-11-28 06:55:33
When reading an NSString from a file I can use initWithContentsOfFile:usedEncoding:error: and it will guess the encoding of the file. When I create it from an NSData though my only option is initWithData:encoding: where I have to explicitly pass the encoding. How can I reliably guess the encoding when I work with NSData instead of files? In general, you can’t. However, you can quite reliably identify UTF-8 files – if a file is valid UTF-8, it’s not very likely that it’s supposed to be any other encoding (except if all the bytes are in the ASCII range, in which case any “extended ASCII”

iOS: Select a GIF from the photo library, convert to NSData for use in multipart/form-data

不问归期 提交于 2019-11-28 06:05:24
What's currently working in my code: I select a JPG or PNG from the Photo Library (using standard ImagePicker methods), and convert that image to NSData using: self.myImageData = UIImageJPEGRepresentation(myImage, 0.9); which I then post to a server using multipart/form-data. I now want to do the same for a GIF, while retaining the original GIF data (so that an animated GIF going into the library, comes back out still animating). In didFinishPickingMediaWithInfo, I am able to get the URL of the original GIF using self.myGIFURL = [info objectForKey:UIImagePickerControllerReferenceURL]. Here's

Converting NSData to int

天涯浪子 提交于 2019-11-28 05:57:21
I've an NSData object, the length is 4 bytes .These four bytes i'm extracting from another NSData object using , fourByteData=[completeData subdataWithRange:NSMakeRange(0, 16)]; My first question is, will the above statement provide me the first four bytes of complete data. If Yes, then how to convert all these bytes to an equivalent int. Is 268566528 the value you expect or perhaps you expect 528? If the correct value is 528 then the byte order is big-endian but the cpu is little-endian, the bytes need to be reversed. So, if the correct value should be 528 then: NSData *data4 = [completeData

How can I read a property list from data in Swift 3

左心房为你撑大大i 提交于 2019-11-28 05:08:13
问题 I'm trying to read a property list from Data in Swift 3 but I can't achieve that. I'm tried something like this: let datasourceDictionary = try! PropertyListSerialization.propertyList(from: data!, options: [PropertyListSerialization.ReadOptions], format: nil) as! Dictionary and a I got this error: Cannot convert value of type 'PropertyListSerialization.ReadOptions.Type' (aka 'PropertyListSerialization.MutabilityOptions.Type') to expected element type 'PropertyListSerialization

Converting UIImage to NSData and NSData to NSString for posting images to a server

可紊 提交于 2019-11-28 05:04:35
问题 I have two UIImageViews. I want to post two images to server through the script. Before that I have to convert these images to data and data to string but I don't know how to convert the images from UIImageView to NSData and NSData to NSString .I have referred all the codes but it does not work. I would also like to know as to where should I declare the conversion coding (image to data and data to image) in my code? This is what I have implemented .h part #import <UIKit/UIKit.h> #import

Progress bar while downloading files iOS

偶尔善良 提交于 2019-11-28 03:52:44
问题 I am using the following to download the files from the internet: NSData *myXMLData1 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"Link]]; Now, I would like to put a progress bar while the download is going on. I checked various posts but i am not able to find out how to do it exactly. Please help me!! 回答1: I would recommend looking into the ASIHTTP library, which contains a lot of usefull classes on mobile handling and download handling. Here is a link where they describe

How send NSData using POST from a iOS application?

梦想与她 提交于 2019-11-28 03:50:42
问题 NSData *imageData = UIImagePNGRepresentation(image); How send imageData using POST? 回答1: The following code should help NSData *imageData = UIImagePNGRepresentation(image); NSURL *yourURL = ... NSMutableURLRequest *yourRequest = [NSMutableURLRequest requestWithURL:yourURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; //Set request to post [yourRequest setHTTPMethod:@"POST"]; //Set content type [yourRequest setValue:@"image/png" forHTTPHeaderField:@"Content-Type"]; //

NSKeyedUnarchiver error after renaming Xcode project

霸气de小男生 提交于 2019-11-28 03:49:36
问题 I just renamed my Xcode project and when I ran it I got this error: 2015-11-14 05:32:42.337 Buck Tracker[3537:1456100] * Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '* -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (iBudgeter.Record) for key (NS.objects); the class may be defined in source code or a library that is not linked' The Buck Tracker is the new name and iBudgeter is the original name. Record is a custom NSObject

Convert NSData to String?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 03:31:10
I am storing a openssl private Key EVP_PKEY as nsdata. For this I am serializing into a byte stream using the code below unsigned char *buf, *p; int len; len = i2d_PrivateKey(pkey, NULL); buf = OPENSSL_malloc(len); p = buf; i2d_PrivateKey(pkey, &p); where pkey is of type EVP_PKEY. Then I am storing the bytes from buffer 'p' as an NSData using the line given below NSData *keydata = [NSData dataWithBytes:P length:len]; Now I am converting it to a NSString using the code given below but when i print it into console its giving some other characters. NSString *content =[ NSString stringWithCString: