nsdata

Swift -How to know if a file was successfully saved in .cachesDirectory

风格不统一 提交于 2020-08-10 01:17:22
问题 I have a .mp4 video saved in Firebase and I want to save it to the .cachesDirectory . I want to make sure it is saved and tried to use a do-try block but it wouldn't accept it: No calls to throwing functions occur within 'try' expression. How else can I check to see if the file was successfully saved to the .cachesDirectory ? let fbStr = "https://firebasestorage.googleapis.com/v0/b/myApp.appspot.com/o/abcd%277920FHqFBkl7D6j%2F-MC65EFG_qT0KZbdtFhU%2F48127-8C29-4666-96C9-E95BE178B268.mp4?alt

Checksum and XOR in Swift

微笑、不失礼 提交于 2020-05-14 21:34:38
问题 I worte these methods in Objective-C. They're just checksum and XOR some NSData - (void)XOR:(NSMutableData *)inputData withKey:(NSData *)key { unsigned char* inputByteData = (unsigned char*)[inputData mutableBytes]; unsigned char* keyByteData = (unsigned char*)[key bytes]; for (int i = 0; i < [inputData length]; i++) { inputByteData[i] = inputByteData[i] ^ keyByteData[i % [key length]]; } } - (Byte)checkSum:(NSMutableData *)data withLength:(Byte)dataLength { Byte * dataByte = (Byte *)malloc

In Swift, how does one assign Data to array of UInt32?

北城余情 提交于 2020-03-04 16:30:27
问题 I have Swift code which reads a binary file representing a sequence of UInt32 values like this: let fileData = binaryFile.readData(ofLength: 44) guard fileData.count > 0 else { break } let headerData = fileData.withUnsafeBytes { Array(UnsafeBufferPointer<UInt32>(start: $0, count: 11)) } let polyCount = headerData[1].bigEndian let polyFlags = headerData[2].bigEndian I'd not used the program containing this code for a while, but when returning to it recently, it still works as expected, but now

Convert UTF-8 encoded NSData to NSString

帅比萌擦擦* 提交于 2020-01-26 05:28:32
问题 I have UTF-8 encoded NSData from windows server and I want to convert it to NSString for iPhone. Since data contains characters (like a degree symbol) which have different values on both platforms, how do I convert data to string? 回答1: If the data is not null-terminated, you should use -initWithData:encoding: NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]; If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra

Convert UTF-8 encoded NSData to NSString

微笑、不失礼 提交于 2020-01-26 05:27:17
问题 I have UTF-8 encoded NSData from windows server and I want to convert it to NSString for iPhone. Since data contains characters (like a degree symbol) which have different values on both platforms, how do I convert data to string? 回答1: If the data is not null-terminated, you should use -initWithData:encoding: NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]; If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra

How do I pass a url to NSBundle for xml parsing with AEXML?

柔情痞子 提交于 2020-01-25 23:12:24
问题 I'm using AEXML and would like to pass XML from a REST service into the AEXML parser. However, in the example code provided the author passes a local xml file into his parser. Based on the example provided I can't seem to figure out how to pass xml data that was received via URL. I have tried capturing the url and parsing it with NSXMLParser before passing it to the bundler but that didn't work. I've also tried creating an NSData object from the URL stream. So it leads me to the question. How

How do I pass a url to NSBundle for xml parsing with AEXML?

蹲街弑〆低调 提交于 2020-01-25 23:11:13
问题 I'm using AEXML and would like to pass XML from a REST service into the AEXML parser. However, in the example code provided the author passes a local xml file into his parser. Based on the example provided I can't seem to figure out how to pass xml data that was received via URL. I have tried capturing the url and parsing it with NSXMLParser before passing it to the bundler but that didn't work. I've also tried creating an NSData object from the URL stream. So it leads me to the question. How

Determine if UTF-8 encoded NSData contains a null-terminated string

痞子三分冷 提交于 2020-01-24 10:48:33
问题 I have the NSData-to-NSString conversion in an NSData Category, because I'm always using the NSString method: initWithData:encoding: . But, according to this answer, https://stackoverflow.com/a/2467856/1231948, it is not that simple. So far, I have this method in my NSData Category, in an effort to keep consistent with methods in other data objects that return a string from a method with the same name: - (NSString *) stringValue { return [[NSString alloc] initWithData:self encoding

Checking if an NSString contains base64 data

雨燕双飞 提交于 2020-01-23 03:22:51
问题 How can I check to see if an NSString contains base64 data in an if statement? Because base64 encodes the data in a completely random way, I can't search for a phrase within the NSString so instead I will need to check to see if the contents of the string results in a data file. 回答1: Here's a category on NSString I created that should work: @interface NSString (MDBase64Additions) - (BOOL)isBase64Data; @end @implementation NSString (MDBase64Additions) - (BOOL)isBase64Data { if ([self length] %

Objective C SHA512 hash of two NSData

自作多情 提交于 2020-01-22 17:07:07
问题 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 *)