NSData & NSURL – url with space having problem

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

convert NSData Length from bytes to megs

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

Slow start for AVAudioPlayer the first time a sound is played

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

NSData to display as a string

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];

Converting NSData to base64

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

Converting HEX NSString To NSData

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);

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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)