Swift 3.0: Data to JSON [String : Any]
The right method is: do { let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String : Any] } catch { print(“errorMsg”) } Thanks to Eric Aya
The right method is: do { let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String : Any] } catch { print(“errorMsg”) } Thanks to Eric Aya
Use: stringByAddingPercentEscapesUsingEncoding: Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string. -(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding A representation of the receiver using encoding to determine the percent escapes necessary to convert the receiver into a legal URL string. Returns nil if encoding … Read more
There are 1024 bytes in a kilobyte and 1024 kilobytes in a megabyte, so… NSLog(@”File size is : %.2f MB”,(float)myData.length/1024.0f/1024.0f); Mind you, this is a simplistic approach that couldn’t really properly accommodate for byte sizes below 1,048,576 bytes or above 1,073,741,823 bytes. For a more complete solution that can handle varying file sizes, see: ObjC/Cocoa … Read more
There is a more elegant way. Swift 3: let str = “Hello” let buf = [UInt8](str.utf8) Swift 4: (thanks to @PJ_Finnegan) let str = “Hello” let buf: [UInt8] = Array(str.utf8)
The delay seems to be related to instantiating AVAudioPlayer for the first time. If I load any audio, run [audioPlayer prepareToPlay] and then immediately release it, the load times for all of my other audio is very close to imperceptible. So now I’m doing that in applicationDidFinishLaunching and everything else runs well. I can’t find … Read more
Use the NSString initWithData:encoding: method. NSString *someString = [[NSString alloc] initWithData:hashedData encoding:NSASCIIStringEncoding]; (edit to respond to your comment:) In that case, Joshua’s answer does help: NSCharacterSet *charsToRemove = [NSCharacterSet characterSetWithCharactersInString:@”< >”]; NSString *someString = [[hashedData description] stringByTrimmingCharactersInSet:charsToRemove];
EDIT As of OS X 10.9 / iOS 7, this is built into the frameworks. See -[NSData base64EncodedDataWithOptions:] Prior to iOS7/OS X 10.9: Matt Gallagher wrote an article on this very topic. At the bottom he gives a link to his embeddable code for iPhone. On the mac you can use the OpenSSL library, on … Read more
Scroll down to the Conclusion section on the page you linked and download the provided NSData+Base64 files. Its the best solution I have seen so far and is incredibly easy to use. If you can learn anything about Cocoa, you can learn to use that project. Example NSString *originalString = [NSString stringWithFormat:@”test”]; NSData *data = … Read more
NSString *command = @”72ff63cea198b3edba8f7e0c23acc345050187a0cde5a9872cbab091ab73e553″; command = [command stringByReplacingOccurrencesOfString:@” ” withString:@””]; NSMutableData *commandToSend= [[NSMutableData alloc] init]; unsigned char whole_byte; char byte_chars[3] = {‘\0′,’\0′,’\0’}; int i; for (i=0; i < [command length]/2; i++) { byte_chars[0] = [command characterAtIndex:i*2]; byte_chars[1] = [command characterAtIndex:i*2+1]; whole_byte = strtol(byte_chars, NULL, 16); [commandToSend appendBytes:&whole_byte length:1]; } NSLog(@”%@”, commandToSend);
Use write(to: fileURL). For example: let fileURL = try! FileManager.default .url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) .appendingPathComponent(“test.jpg”) do { try jpegData.write(to: fileURL, options: .atomic) } catch { print(error) } Or, if you really are stuck with a path, convert that to a URL: do { try data.write(to: URL(fileURLWithPath: path), options: .atomic) } catch … Read more