FAQ Database Discussion Community


Swift. How to write bytes from NSData into another NSData?

ios,swift,byte,nsdata
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: Get length of first object. Write into NSMutableData in such order: first object length, first object, second object. Code looks like: let firstString = "first_string";...

Saving and retrieving custom objects in Core Data

ios,swift,core-data,data,nsdata
It's the first time I'm trying to save and retrieve custom data to/from my core data, but I've run into an error saying: fatal error: array cannot be bridged from Objective-C when I try to load the data back. My code looks like this, the arrayOfNames is declared as [String]:...

Unable to edit screenshots, performChanges block fails

ios,ios8,nsdata,cgimage,photokit
I'm developing an app that allows users to edit photos using PhotoKit. I was previously saving the edited photo to disk as a JPEG. I would like to avoid converting to JPEG and have implemented the modifications in order to do that. It works great for photos taken with the...

isEqualToString not evaluating true

cocoa-touch,nsstring,nsdata
Method: - (void)serverResponse:(NSData *)data { NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@", response); NSLog(@"%i", [response isEqualToString:@"1"]); } Log: 2015-03-15 06:26:53.716 appName[687:60b] 1 2015-03-15 06:26:53.718 appName[687:60b] 0 This doesn't seem to make any sense. I'm guessing the problem has something to do with the encoding, but I'm not sure what....

Using dispatch_write to write NSData to unique file in background under ARC

ios,automatic-ref-counting,grand-central-dispatch,nsdata
I am attempting to create a file with a unique name and write data to it in the background. mktemp says Whenever it is possible, mkstemp() should be used instead, since it does not have the race condition. Using mkstemp results in an open file descriptor, so dispatch_write seems to...

base64 encoded image and add it to UIImage

ios,xcode,uiimage,nsdata
I am creating a captcha image as client needs. I am getting JSON for the image with two key values. key: CaptchaText, value: uEU3VNWh0lndY8UyL+jWsQ== key: CaptchaImage, value:...

Convert data to an UIImageView

parse.com,uiimage,nsdata
I try to convert data from parse.com into my UIImageView. for (PFObject *object in objects) { PFFile *file = [object objectForKey:@"imageData"]; [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { [contentImageArray addObject: [UIImage imageWithData:data]]; }]; } I load my data in a NSMutableArray, this still works, but then I want my data in...

NSJSONSerialization.JSONObjectWithData changes field type

objective-c,swift,nsstring,nsdictionary,nsdata
I'm getting the following JSON response from the server: { "userId":"123456789", "displayName":"display name" } When I use NSJSONSerialization.JSONObjectWithData and then prints the result NSDictionary I see in the console the following: userId = 123456789 displayName = "display name" Why do JSONObjectWithData changes the userId field type from String to a...

Sending NSDATA from 32bit to 64bit in multiplayer game using Game centre in ios

ios,nsdata,32bit-64bit,game-center
I am developing multiplayer game using game centre, Overall Code is working well if my both device is 64bit or 32bit, but if my one device is 32bit and other is 64bit than sending data is not correct, please help me, thanks in Advance - (void)sendDataToPlayers:(void *)data length:(NSInteger)length { NSError...

Error Store NSdata to NsDictonary

ios,nsdictionary,nsdata
plz help me to parse JSON data. we get response in NSMutable Data. but we cn't take out data in Dictionary. my jsonDictionary get nil. this is my Url . i have check on JSON Validator.They show Valid Json output. help.... http://mobileapp.merucabs.com/NearByCab_ETA/GetNearByCabs.svc/rest/nearby?Lat=23.0768222&Lng=72.645732&SuggestedRadiusMeters=5000&CabMaxCount=10 -(void)connectionDidFinishLoading:(NSURLConnection *)connection { // NSString *responseString = [[NSString...

Prevent loop from looping until inner loop writes to disk

ios,objective-c,grand-central-dispatch,nsdata
I have a nested for loop in where I call a getSnapShotData method many times and write this data to disk. I've noticed I get too much memory build up doing this and I run out of memory, so I thought this would be a good use case for using...

How to byte reverse NSData output in Swift the littleEndian way?

swift,nsdata,byte-order
Hey guys I have this output from NSData: <00000100 84000c00 071490fe 4dfbd7e9> So how could I byte reverse it in Swift and have this output: <00000001 0084000c 1407fe90 fb4de9d7>?...

How to convert image which taken by camera (UIImagePickerControllerSourceTypeCamera) to NSData in iOS

ios,objective-c,uiimageview,uiimagepickercontroller,nsdata
Codings that I'm having to get the photo in my UIViewController: -(IBAction) getPhoto:(id) sender { UIImagePickerController * picker = [[UIImagePickerController alloc] init]; picker.delegate = self; if((UIButton *) sender == choosePhotoBtn) { picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } else { picker.sourceType = UIImagePickerControllerSourceTypeCamera; } [self presentModalViewController:picker animated:YES]; } following is the function to...

UIDocumentInteractionController “invalid scheme (null)”

ios,excel,nsdata,preview,uidocumentinteraction
I'm trying to preview a document with UIDocumentInteractionController. The document is an .xls file which my app downloads from the web. I keep getting this error: 'UIDocumentInteractionController: invalid scheme (null). Only the file scheme is supported.' I use the next code: - (void) webServiceController:(WebServiceController *)webServiceController returnedArray:(NSMutableArray *)array { if ([webServiceController.webRequest...

read blob data extra argument bytes in call swift

sqlite,swift,blob,nsdata
I am trying to read blob data from sqlite, but I get Extra argument 'bytes' in call error. Here is my code: var len = sqlite3_column_bytes(compiledStatement, 0) var point = sqlite3_column_blob(compiledStatement, 0) var dbData:NSData? = NSData(bytes: point, length: len)// ERROR: Extra argument 'bytes' in call if dbData != nil {...

Swift UnsafePointer(data.bytes).memory crash under some (but not all) circumstances

swift,struct,nsuserdefaults,nsdata
I have the following struct: struct Identity { var id: Int var createdAt: NSDate var string: String var apnsToken: String } Over the course of my application's execution, instances (?) of this struct are turned into NSData (using the following code) and stored in NSUserDefaults: var id = Identity(id: 0,...

how to show the NSData value in UIImage View while retrieving the database

ios,objective-c,sqlite,uiimage,nsdata
Added the images as NSData in database successfully. But while i'm retrieving back NSData to UIImage its not working. The following codings that i used for retrieving the database any error please suggest me. Here i used primary key to fetch correct image presented in particular record: -(void)ShowImagesinImageView{ NSLog(@"Loaded in...

Does [NSData getBytes: length:] change position in buffer?

objective-c,buffer,nsdata
I have some NSData instance with 2 bytes of different value. Will calling getBytes: of any kind (length, range...) also advance the current position in the buffer? Example: NSData *data = ...; // 2 bytes data [data getBytes:&whatever1 length:1]; // reading first byte [data getBytes:&whatever2 length:1]; // reading first OR...

NSDataWritingFileProtectionComplete with WriteToFile not encrypt the data

objective-c,nsdata,writetofile
I hear that we can encrypt documents converting them to NSData and using the WriteToFile method to write to the directory for that I realized the following test: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"confidencial"]; NSString *content = @"This message is...

NSData to NSString losing data

nsstring,ascii,nsdata,nsstringencoding
I'm attempting to convert a binary file into text, the problem is that a large portion of the file was not encoded in ascii and ends up being special characters. I'm using [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; but am only getting a few characters back in a 20000 byte data block....

NSData to String in Swift Issues

ios,xcode,swift,nsdata
I'm having issues converting my NSData to NSString in swift. I'm using what I think is the correct command and format: NSString(data: <DATA>, encoding: <ENCODING>) however whatever I do i keep ending up with a nil value. I am running the latest Xcode beta so I'm not sure if that...

Objective C converting NSData with hex value to int

objective-c,int,nsdata
I have a NSData with a hex value inside e.g. <EC>. Now I need to convert this value into an int variable to get int x = -20. Ideas?...

Xocde - What is the difference of 'dataWithBytesNoCopy' and 'dataWithBytes'?

ios,byte,nsdata
What is the difference of + (instancetype)dataWithBytes:(const void *)bytes length:(NSUInteger)length; and + (instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length;' Also, + (instancetype)dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)b; if b == YES, it will free the bytes automatically after converted to data ? I am working on an app and almost finished it. But the last problem...

NSData contentsOfUrl returning nil

swift,nsdata
Im trying to simply get a jpeg image from an url but when using NSData with contentsOfUrl is returning nil. let aString = "http://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xfp1/t31.0-8/q88/s720x720/10848858_907722502601079_2834541930671169073_o.jpg" let url = NSURL(fileURLWithPath: aString) let data = NSData(contentsOfURL: url!) Any ideas?...

Unarchive NSData back to NSColor in swift

osx,swift,nsuserdefaults,nsdata,nscolor
I have archived an NSColor to store it in NSUserDefaults: var data = NSArchiver.archivedDataWithRootObject(NSColor.redColor()) storage.setObject(data, forKey: "color") storage.synchronize() But now I need to get the color back from NSData, I have no idea how to do that...

Saving app settings - iOS

ios,cocoa-touch,settings,nsuserdefaults,nsdata
I have an iOS app which loads certain features depending on the settings that the user sets. Currently I am using NSUseDefaults to save and retrieve these settings and it works fine. But from what I understand anyone can view and edit them with a simple XML editor. You don't...

UICollectionView Scroll Freeze with NSData (imageView)

ios,uicollectionview,nsdata,uicollectionviewcell,sdwebimage
I've UICollectionView with UITableView in the same page. I'm using SDWebImage with UITableView and it is working fine. I'm trying to use SDWebImage with UICollectionView but couldn't make it. So I have used NSData but it is freezing with it. How I can solve this issue? UPDATE: Is there a...

Can't load image with UIImage or NSData

ios,swift,uiimage,nsdata
I am programming a Swift application, and I can't load image saved in the application by using UIImage(contentsOfFile: imgPath) or NSData(contentsOfFile: imgPath) private func loadData() { println("load DATA DANGEROUS") let (dangerous, err) = SD.executeQuery("SELECT * FROM Dangerous") if err == nil && !dangerous.isEmpty { var tabPhoto : [DangerousImage] = []...

Check If Server's Image is Already Updated and Download Again

ios,swift,parse.com,nsdata,sdwebimage
I would like to know what is the proper way of caching image and store it to Parse.com, and load it back, update cache etc. So here is the scenario: I have a social network app where user can upload their profile picture. Once the user upload the picture. I...

How to convert AVAsset to NSData or save it to file manager

ios,objective-c,iphone,nsdata
I am trying to do that using AVAsset to record Audio file and then after first i store it on NSFileManager and after that by convert it to nsdata i Call API to store it. I am successful to Create AVAsset recording file and play it using third party Class...

How do I convert an Int value into NSData in Swift?

ios,swift,int,nsdata
In Objective-C I use the following code to Convert an Int variable into NSData, a packet of bytes. int myScore = 0; NSData *packet = [NSData dataWithBytes:&myScore length:sizeof(myScore)]; Use the converted NSData variable into a method. [match sendDataToAllPlayers: packet withDataMode: GKMatchSendDataUnreliable error: &error]; I tried converting the Objective-C code into...

Convert NSValue to NSData and back again, with the correct type

objective-c,core-data,nsdata,nsvalue
I would like to be able to convert an objective-c object, such as an NSArray or UIImage into an NSData object, which I can then use to write to disk. I first converted them to an NSValue, which I then planned on converting to NSData. This question provided part of...

Converting the NSData returned from an HTTP request in Swift to a String

xcode,string,swift,nsstring,nsdata
I have some code that makes an HTTP request, then calls a function with the data, response, and error results. This function then tries to call a function, handler, that takes a Swift String as its sole argument. I have tried to convert the NSData object (data) returned from the...

BytesArray. ObjC to Swift

objective-c,arrays,swift,byte,nsdata
I am trying to write Swift implementation of the following ObjC(header file) code. #include <stddef.h> #ifndef VO_CERTIFICATE_TYPE #define VO_CERTIFICATE_TYPE typedef struct _voCertificate { const char* bytes; size_t length; } voCertificate; #endif static const char myCertificate_BYTES[] = { 103, 92, -99, 33, 72, 48, 119, -72, -77, 75, -88, 81, 113,...

Not all keys work when converting JSON String to NSDictionary

ios,objective-c,json,nsdictionary,nsdata
I'm using the following code to convert a JSON string literal to an array holding an NSDictionary for each item: NSString* json = @"[{\"name\":\"Item 1\",\"id\":\"999\",\"lang\":\"en\",\"type\":\"A\",\"version\":15}]"; NSData* data = [json dataUsingEncoding:NSUTF8StringEncoding]; NSArray* values = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; After removing the app from my test device, the app started crashing when...

NSData dataWithContentsOfFile returns different results on device

ios,iphone,hash,md5,nsdata
I'm using dataWithContentsOfFile on a PNG image and the iOS Simulator returns a value that differs from an iOS Device. My device is a 64-bit iPad Air (iOS 8.3) and my simulator is set to iPad Retina iOS 8.3. Here's the code that I'm using: NSString *sourcePath = [[NSBundle mainBundle]...

Working with NSData in swift

pointers,swift,nsdata
So I've figured out how to extract NSData in swift but i'm getting confused with regards to setting it: var testBytes : [Byte] = [0x14, 0x00, 0xAB, 0x45, 0x49, 0x1F, 0xEF, 0x15, 0xA8, 0x89, 0x78, 0x0F, 0x09, 0xA9, 0x07, 0xB0, 0x01, 0x20, 0x01, 0x4E, 0x38, 0x32, 0x35, 0x56, 0x20, 0x20,...

Proper way to get the byte array from the JSON

ios,objective-c,json,uiimage,nsdata
I am trying to get the image from the byte array. I can only get the image if enter the byte array values into the string directly as follows: NSMutableString *imagen = [[NSMutableString alloc] initWithString:@"-1,-40,-1,-32,0,16,74,70,73,70,0,1,0,1,0,96,0,96,0,0,-1,-2,0,31,76,69,65,68,32,84,101,99,104,110,111,108,111,103,105,101,115,32,73,110,99,46,32,86,49,46,48,49,0,-1,-37,0,-124,0,5,5,5,8,5,8,12,7,7,12,12,9,9,9,12,13,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13]; //this way works fine IF i try to get the byte array...

Storing images to CoreData - Swift

ios,image,swift,core-data,nsdata
In my code I managed to save a textLabel with CoreData but I can't seem to properly save the image. I've read some tutorials and I know I have to convert it to NSData. But how do I do it? Thanks in advance!...

Swift structs to NSData and back

swift,serialization,struct,nsdata,nskeyedarchiver
I have a struct containing a struct and an NSObject that I want to serialize into an NSData object: struct Packet { var name: String var index: Int var numberOfPackets: Int var data: NSData } var thePacket = Packet(name: name, index: i, numberOfPackets: numberOfPackets, data: packetData) How do I best...

NSData does not get saved in local file

ios,ipad,swift,nsdata
I am trying to fetch data from an URL (in this case an image) and write this data to the disk. I receive the data and everything seems fine. The Debugger even shows that data is written to the disk and it is even the right size. But when I...

Converting a pdf image to NSData for Parse.com

ios,xcode,parse.com,nsdata
I am using vector graphics in my application and want to store some on Parse.com. I need to first convert these to NSData before upload. Is it possible to convert a pdf file to NSData? UIImage *image = [UIImage imageNamed:@"coolImage.pdf"]; NSData *data = UIImageJPEGRepresentation(image); I know NSData allows for a...

How do I do a bit-wise XOR on NSData in Objective-C?

objective-c,xcode5,nsdata,endianness,xor
I have two NSData objects, data1 and data2, and I'd like to do a bit-wise XOR and store the result in a third NSData object, xorData. The first thing I tried was this: *data1.bytes^*data2.bytes; but it gave an error of: Invalid operands to binary expression ('const void' and 'const void')...

Copy Video from Library to DocumentsDocument directroy

ios,video,nsdata,alasset,copyfile
I am building a task in ios app where ,i want to copy the video from library to local app Documents Directory. I have succesfully fetch the ALAssest List from Library in an array. Now when i try to copy any video from ALAssest url to our file i am...

NSData is returning nil for same file at different filepaths

ios,objective-c,ios-simulator,nsdata,nsurl
I stored an audio file of format .aiff in two different folders. The NSData is returning nil for one file path even though both file paths have the audio file. I double checked the file paths. The path is like this: //url1 file:///Users/VenkataManiteja/Library/Developer/CoreSimulator/Devices/80B8117E-D2C9-4B42-8A76-9A89A10FB1C1/data/Containers/Data/Application/FD17AD64-EAF9-4578-B50D-0B5BF6F2DEFF/Documents/28Apr15_090827AM.aif //url2...

NSData from NSURLConnection does not match when downloading file directly

ios,objective-c,nsurlconnection,nsdata,aac
I am trying to find FFF9 hex representation in and NSData object that I get with NSURLConnection when I type the url address to my web browser and download the file to my mac then I open it a text editor I get following values which seems true Few lines...

Decrypt p7s file on iOS

ios,objective-c,cocoa-touch,nsdata,pkcs7
Working on a test iOS app I have faced the following problem. From a remote webserver I recieve a .p7s file with the following contents (which are viewable in Mac's TextEdit app): 0Ä *ÜHܘ †Ä0Ä10 +�0Ä *ÜHܘ †Ä$ÄÇ˚<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict>...