nsdata

Extract struct from NSData in Swift

北城以北 提交于 2019-12-11 10:51:53
问题 I'm making Game Center game. I send a data packet containing a struct MovePacket like this: var packet = MovePacket(packetID: 1, move: myMove) let dataPacket = NSData(bytes: &packet, length: sizeof(MovePacket)) currentMatch!.sendDataToAllPlayers(dataPacket, withDataMode: GKMatchSendDataMode.Reliable, error: nil) This works like in Objective-C, but I don't understand how to decode the received NSData back to my struct MovePacket . This is one of my tries: func match(match: GKMatch!,

Error Domain=NSCocoaErrorDomain Code=3840 “No value.” UserInfo={NSDebugDescription=No value.}

北慕城南 提交于 2019-12-11 10:49:15
问题 I'm learning JSON. I have problem with parsing JSON from URL. The error is "Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}" JSON is valid - I've checked. This is structure of JSON: [{"id":33,"name":"5","sort":2},{"id":34,"name":"6","sort":3},{"id":35,"name":"7","sortOrder":4}] And this is my code: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let urlAsString = "http://www.url.address/" let

Retrieve NSData to Hex by length

与世无争的帅哥 提交于 2019-12-11 09:49:23
问题 I got a NSData that contain bytes like <00350029 0033> with length 6, is there any correct way to split the bytes to array somehow like (00, 35, 00, 29, 00, 33) ? 回答1: NSData *data = ...; NSMutableArray *bytes = [NSMutableArray array]; for (NSUInteger i = 0; i < [data length]; i++) { unsigned char byte; [data getBytes:&byte range:NSMakeRange(i, 1)]; [bytes addObject:[NSString stringWithFormat:@"%x", byte]]; } NSLog(@"%@", bytes); (Assuming you want the bytes as a hex string representation, as

Objective-C gzipped NSData to Python gzipped string?

一曲冷凌霜 提交于 2019-12-11 09:46:51
问题 I'm using +[NSData gtm_dataByGzippingData:] to gzip the UTF-8 encoded data returned by +[NSJSONSerialization dataWithJSONObject:options:error:] . How do I convert this gzipped NSData to a string that I can send as a parameter to a Python server so that the server can decompress it with zlib.decompress()? 回答1: Use a method like -[NSData base64EncodedString] to base64-encode the gzipped NSData before you send it to the Python server. Then, the Python server can base64-decode it and then unzip

How to append data to NSKeyedArchive?

风流意气都作罢 提交于 2019-12-11 08:39:15
问题 I'm reading a file like so: NSData *data = [NSData dataWithContentsOfFile:myPath]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; NSData *imgData = [unarchiver decodeObjectForKey:@"myimage"]; if (imgData != nil) { self.image = [[NSImage alloc]initWithData:imgData]; }else{ self.image = nil; } [unarchiver finishDecoding]; [unarchiver release]; What I'd like to do is to append image data for key @"myimage" to the same file if it doesn't exist. How to go

How does NSData(bytes:length:) convert [Byte] to UnsafePointer<Void> in Swift?

泪湿孤枕 提交于 2019-12-11 07:26:50
问题 Playing with raw data in Swift I came across something I don't understand. NSData has a constructor: init(bytes: UnsafePointer<Void>, length: Int) where the first bytes parameter is clearly of UnsafePointer type. However, if I pass [Byte] object to this constructor, not only does the compiler not complain, but it works ok. But if I try to cast [Byte] to UnsafePointer , I fail. How does this work? For example (you can try it in Playgrounds): let buffer: [Byte] = [0x00, 0xff] let data = NSData

Low cost Image to NSData conversion for feeding SCNTechnique's sampler2D inputs

北城余情 提交于 2019-12-11 07:02:16
问题 Sometimes the only way to pass precious data from CPU to GPU is by hiding it in textures. I tried to trick SCNTechnique and simply pass [NSData dataWithBytes:length:] or a CGDataProviderRef containing my neatly prepared raw pixel data bytes, but SceneKit is smart enough to detect my sinister attempts. But I did not give up, and found a loophole: [_sceneView.technique setValue: UIImagePNGRepresentation(encodeInSinglePixelUIImage(pos.x, pos.y)) forKey:@"blob_pos_"]; Encoding and decoding single

response Data is nil

浪尽此生 提交于 2019-12-11 06:57:20
问题 I am getting response data nil . func fetchSinglePageData() { var response: NSData? var errors: NSError? dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { response = NSData(contentsOfFile:"url...?project_id=(self.projectID)&project_page=Request", options: NSDataReadingOptions(0), error: &errors) print("LatestURL Single page:(response)") if response == nil { var alertview = UIAlertView(title: "Network Error", message: "Data not received due to network connection

NSDictionary / Base64 String Logs

心不动则不痛 提交于 2019-12-11 06:46:15
问题 I have this weird problem about NSDictionary and base64 String and I cannot locate where the problem is. Here's the code: NSData *pictureData = [[NSData alloc]init]; pictureData = UIImagePNGRepresentation([UIImage imageNamed:@"logo"]); NSString * picture = [pictureData base64EncodedString]; picture = [picture stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"]; if(picture == nil){ picture = @""; } self.postDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:picture,@"picture",@

Get color values in byte stream- ios

半世苍凉 提交于 2019-12-11 06:39:12
问题 I read this byte stream from a binary file (.ase) Bold values represent colors: <41534546 00010000 00000005 c0010000 00140008 004f0070 0061006c 0074006f 006e0065 00000001 00000024 00080023 00320042 00420034 00300046 00005247 4220* 3e2c acad3f34 b4b53d70 f0f1 *0002 00010000 00240008 00230046 00460035 00420033 00310000 52474220 3f800000 3eb6b6b7 3e44c4c5 00020001 00000024 00080023 00460046 00380037 00340038 00005247 4220* 3f80 00003f07 87883e90 9091 *0002 00010000 00240008 00230030 00330038