How can i convert NSData to const uint8_t?or getting error while passing a NSData over Wifi
Date : March 29 2020, 07:55 AM
it should still fix some issue i have a array with two strings: const uint8_t *bytes = (const uint8_t*)[myData bytes];
// ^^ note the asterisk
|
Converting UIImage to NSData and NSData to NSString for posting images to a server
Tag : ios , By : user118656
Date : March 29 2020, 07:55 AM
I hope this helps . After Long time I post my answer here. Option 1 : Image to server and Image from Server NSData *postData = UIImageJPEGRepresentation(myImage, 1.0);
NSString *strEncoded = [postData base64EncodedStringWithOptions:0];
NSData *getData = [[NSData alloc] initWithBase64EncodedString:strEncodedFromServer options:0];
UIImage *image = [UIImage imageWithData:getData];
NSString *strName = @"iOS";
NSData *dataEncoded = [strName dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64EncodedString = [dataEncoded base64EncodedStringWithOptions:0];
NSLog(@"The Encoded String is - %@", base64EncodedString);
NSData *dataDecoded = [[NSData alloc] initWithBase64EncodedString:base64EncodedString options:0];
NSString *strDecoded = [[NSString alloc] initWithData:dataDecoded encoding:NSUTF8StringEncoding];
NSLog(@"The DeCoded String is - %@", strDecoded);
NSDictionary *dictEncoded = @{
@"OS":@"iOS",
@"Mobile":@"iPhone",
@"Version":@"iOS10"
};
NSData *dataDictEncode = [NSJSONSerialization dataWithJSONObject:dictEncoded options:(NSJSONWritingOptions) 0 error:nil];
NSString *strBase64Encode = [dataDictEncode base64EncodedStringWithOptions:0];
NSLog(@"The encoded dictionary is - %@", strBase64Encode);
NSData *dataDictDecode = [[NSData alloc] initWithBase64EncodedString:strBase64Encode options:0];
NSDictionary *dictDecoded = [NSJSONSerialization JSONObjectWithData:dataDictDecode options:NSJSONReadingMutableContainers error:nil];
NSLog(@"The decoded dictionary is - %@", dictDecoded);
|
Using Structs (Bytes) with SWIFT - Struct to NSData and NSData to Struct
Date : March 29 2020, 07:55 AM
around this issue Change Byte! to Byte. Otherwise you are creating struct holding Optional which is larger than one byte because in addition to hold a byte, it need extra byte to indicate if it is nil
|
Swift. How to write bytes from NSData into another NSData?
Tag : ios , By : Novi Indrayani
Date : March 29 2020, 07:55 AM
I hope this helps you . I'm trying to concatenate two NSData objects into one NSMutableData, and than get them back. For now i'm trying to do it in such way: , You can extract the data using subdataWithRange(): let firstData1 = mutableData.subdataWithRange(NSMakeRange(sizeof(Int), length))
if let firstString1 = NSString(data: firstData1, encoding: NSUTF8StringEncoding) as? String {
println(firstString1)
} else {
// bad encoding
}
var data = NSData()
mutableData.getBytes(&data, range: NSMakeRange(sizeof(Int), length))
|
why [NSData bytes] can be changed and impact NSData object itself
Date : March 29 2020, 07:55 AM
This might help you The above code is allowed, not because there's a NSConcreteMutableData behind the scene, but rather simply because the author is casting a const pointer to a non-const pointer. In Objective-C, you can bypass all type and mutability safety when you engage in casts. This mutating of data pointed to by the bytes reference is a horrible practice because you have no assurances as to what assumptions or actions this NSData may have made with respect to this underlying data buffer. You should be doing a mutableCopy to get NSMutableData and then working with mutableBytes or using one of the replaceBytes... methods.
|