nsdata

How to initialise a string from NSData in Swift

余生长醉 提交于 2019-11-28 02:58:11
I have been trying to initialise a string from NSData in Swift. In the NSString Cocoa Documentation Apple is saying you have to use this: init(data data: NSData!, encoding encoding: UInt) However Apple did not include any example for usage or where to put the init . I am trying to convert the following code from Objective-C to Swift NSString *string; string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; I have been trying a lot of possible syntaxes such as the following (of course it did not work): var string:NSString! string = init(data: fooData,encoding:

NSMutableArray Data Attachement With E-mail Body?

China☆狼群 提交于 2019-11-28 02:20:33
问题 My NSMutableArray data are in NSData formate.I am trying to attached NSMutableArray data to E-mail body.Here is my NSMutableArray code: NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults]; NSString *msg1 = [defaults1 objectForKey:@"key5"]; NSData *colorData = [defaults1 objectForKey:@"key6"]; UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData]; NSData *colorData1 = [defaults1 objectForKey:@"key7"]; UIColor *color1 = [NSKeyedUnarchiver unarchiveObjectWithData

NSData dataWithContentsOfURL: not returning data for URL that shows in browser

扶醉桌前 提交于 2019-11-28 02:11:02
问题 I am making an iOS client for the Stack Exchange API. After a long, drawn out fight I finally managed to implement authentication - which gives me a token I stick into a URL. When the token is valid, the URL looks like this: https://api.stackexchange.com/2.1/me/associated?key=_____MY_SECRET_KEY______&access_token=_____ACCESS_TOKEN_:)_____ which, when valid, brings me to this JSON in a webpage: {"items":[{"site_name":"Stack Overflow","site_url":"http://stackoverflow.com","user_id":1849664,

NSMutableData datawithBytesNoCopy:length:freeWhenDone: seems to make a copy of the buffer provided to it

末鹿安然 提交于 2019-11-28 00:26:34
According to Apple documentation, the class method +datawithBytesNoCopy:length:freeWhenDone: inherited from NSData Creates and returns a data object that holds a given number of bytes from a given buffer. But NSUInteger len = 1024; char *buffer = malloc(len); NSMutableData *data = [NSMutableData dataWithBytesNoCopy:buffer length:len freeWhenDone:YES]; char *dataBytes = data.mutableBytes; NSLog(@"%@", dataBytes == buffer ? @":D" : @":("); prints :( The method seems to actually make a copy, where I expected it not to. Am I using this the wrong way? The equivalent NSData method works as expected.

NSString containing hex convert to ascii equivalent

隐身守侯 提交于 2019-11-28 00:20:21
I have an NSString that has hex information such as <00000020 66747970 4d344120 00000000 4d344120 6d703432 69736f6d 00000000 00031203 which came from NSData. What I need to do is convert that NSString of Hex data to Ascii which would be: [0][0][0] ftypM4A [0][0][0][0]M4A mp42isom[0][0][0][0][0][3][18][3] As you might be able to tell this is a M4A file. I loaded the first part of the file in to NSData using NSFileHandle. I then stored it into NSString: NSData *data = [filehandle readDataOfLength:1000]; NSString *str = [[NSString alloc]initWithString:[NSString stringWithFormat:@"%@",data]];

Convert AVAudioPCMBuffer to NSData and back

五迷三道 提交于 2019-11-27 22:58:20
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 ? Buffer length is frameCapacity * bytesPerFrame. Here are functions that can do conversion between NSData and AVAudioPCMBuffer. func toNSData(PCMBuffer: AVAudioPCMBuffer) -> NSData { let channelCount = 1 // given PCMBuffer channel count is 1 var channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: channelCount) var ch0Data = NSData(bytes: channels[0],

NSData to NSString with JSON response

感情迁移 提交于 2019-11-27 22:28:34
NSData* jsonData is the http response contains JSON data. NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"jsonString: %@", jsonString); I got the result: { "result": "\u8aaa" } What is the proper way to encoding the data to the correct string, not unicode string like "\uxxxx"? If you convert the JSON data { "result" : "\u8aaa" } to a NSDictionary (e.g. using NSJSONSerialization ) and print the dictionary NSError *error; NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; NSLog(@"%@", jsonDict)

Determine MIME type from NSData?

三世轮回 提交于 2019-11-27 20:07:37
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 only a few answers to this question and the most recent one is from 2010 (4 years ago!). Thanks! NSData *data; // can be an image or video NSString *mimeType = [data getMimetype]; // how would I implement getMimeType Based on ml's answer from a similar post , I've added the mime types determination for NSData: ObjC: + (NSString *)mimeTypeForData:

How to downscale a UIImage in IOS by the Data size

感情迁移 提交于 2019-11-27 17:53:56
I am looking to downscale a UIImage in iOS . I have seen other questions below and their approach on how to downscale the image by size. Resizing Images Objective-C How to resize the image programmatically in objective-c in iphone The simplest way to resize an UIImage? These questions are all based on re-sizing the image to a specific size. In my case I am looking to re-size/downscale the image based on a maximum size. As an example, I would like to set a maximum NSData size to be 500 KB. I know that I can get the size of the image like this:// Check the size of the image returned NSData

'bytes' is unavailable: use withUnsafeBytes instead

ⅰ亾dé卋堺 提交于 2019-11-27 17:53:28
问题 Code that was previously working in Swift 2.2 is now throwing the following error in Swift 3: Here is my code: let tempData: NSMutableData = NSMutableData(length: 26)! tempData.replaceBytes(in: NSMakeRange(0, data.count), withBytes:data.bytes) What should I replace "data.bytes" with to fix the error? I've tried implementing 'withUnsafeBytes' and had a look at Apple's documentation, but can't get my head around it! 回答1: Assuming that data has type Data , the following should work: let tempData