I have seen this done the other way. But I have a NSData object returned that is suppose to be a URL of an image file.
How do I do this convert a NSData object into a NSURL?
Thanks
You first need to convert the NSData object to an NSString object and then you can just create a NSURL with the new string.
NSString *urlString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; // Or any other appropriate encoding
NSURL *url = [[NSURL alloc] initWithString:[urlString autorelease]];
Related
I'm trying to make an xcode plugin that needs to write something to the .h file from the .m file. I haven't been able to find a stackoverflow post with an answer on how to do this. I've tried this code
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSString *contents = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
I've also tried using the StringWithContentsOfFile method, however they both return nil. Is it because you can't read an .h file with this code or is it something else I'm missing.
filePath is this and it's a correct filepath, my ftp client can read it atleast file:///Users/myUserName/Documents/code/macOsDev/XcodePlugIns/XcoderPlugin/XcoderPlugin/XcoderPlugin.h
So my question is, how do I read and write to a .h file? Thanks in advance.
EDIT as requested, some more code however this is as far as I've gotten to the read/write part of my plugin.
NSString *filePath = path;
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSString *contents = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
NSError *error;
NSString *contents1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
contents is #"" because data is nil
and contents1 is just nil;
error is error
NSError * domain: #"NSCocoaErrorDomain" - code: 260
in the debugger, but I'm not sure I'm using this error thing correctly.
Use "/Users/myUserName/Documents/code/macOsDev/XcodePlugIns/XcoderPlugin/XcoderPlugin/XcoderPlugin.h".
Dont use 'file://' prefix
Following is the code for reading and writing
NSString *path = #"your/path/tp/.h/file";
NSData *data = [NSData dataWithContentsOfFile:path];
//Get the contents of the file into the mutable string
NSMutableString *contents = [[NSMutableString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
//Make changes to your mutable string
[contents appendString:#"abc"];
//Write it back to the file
[contents writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
I packed an object (NSObject) to (NSData) and then encoded it with (NSASCIIStringEncoding) for sending it to a SQLite database with this code:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:canvasView.trazoYorch];
//convert NSData object to plain text for sending it to DB
NSData *data2 = [[NSString stringWithFormat:#"%#",data] dataUsingEncoding:NSASCIIStringEncoding];
NSString *dataStr = [[NSString alloc] initWithData:data2 encoding:NSASCIIStringEncoding];
everything works ok, but when I want to do the Inverse process NSString to NSData I got different results, this is my code for inverse process
NSString *FirmaString = [self traerFirmadeBD]; //returns the string content of DB
NSData *data2 = [FirmaString dataUsingEncoding:NSASCIIStringEncoding];
FirmaYorch *firmaCompleta = [NSKeyedUnarchiver unarchiveObjectWithData:data2];
Any help solving this I will appreciate
I found this method to convert an NSData to an NSString object.
NSData *data = //some data
NSString *string = [NSString stringWithFormat:#"%#", data];
How will the data be decoded? Is NSUTF8StringEncoding applied?
Thank you!
This is not the recommended approach. Instead use:
NSString *newString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
This will ensure you have a specified encoding for the data.
In this case, the stringWithFormat: method will send NSData object the description message, get the result, and use that for the content of the newly created string. Essentially, the result is identical to
NSString *string = [data description];
According to NSData's documentation, description returns
An NSString object that contains a hexadecimal representation of the receiver’s contents in NSData property list format.
You probably should be using NSString *string = [[NSString alloc] initWithData: data encoding:SomeFormOfEncoding];
You can view the method details here.
The forms of encoding can be seen here.
This is what I have in a public method - (IBAction)methodName
NSString *quoteNumber = [[self textBox] text];
NSURL *url = [[NSURL alloc] initWithString:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber];
The error I get is:
Too many arguments to method call, expected 1, have 2
What am I doing wrong?
I think you are thinking of NSString's stringWithFormat::
[NSURL URLWithString:[NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%#", quoteNumber]]
Also note the change to %# for the format specifier, since it is an instance of NSString (not an int)
You need to format your string. Try this:
NSString *urlString = [NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%#", quoteNumber];
NSURL *url = [[NSURL alloc] initWithString:urlString];
The initWithString method can only accept a normal NSString, you are passing it a formatted NSString, Take a look at this code:
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quotedNumber]];
That might be a bit confusing, you can break it up as follows:
NSString *urlString = [NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quotedNumber];
NSURL *url = [[NSURL alloc] initWithString:urlString];
Now your string is properly formated, and the NSURL initWithString method will work!
Also, just so it is clearer for you in the future, you can take advantage of Objective-C's dot notation syntax when you set your quoteNumber string, as follows:
NSString *quoteNumber = self.textBox.text;
Also, you are trying to pass this quoted number into your urlString as a digit (as seen with the %d), remember that quotedNumber is a NSString object, and will crash when you try to pass it to the stringWithFormat method. You must convert the string first to a NSInteger, or NSUInteger.
Please refer to this SO question to how to do that (don't worry it's very easy)!
The problem is
[NSURL initWithString:]
requires ONE parameter of NSString type but you passed TWO parameters .
You need to pass a single NSString parameter . Change your code from
NSURL *url = [[NSURL alloc] initWithString:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber];
to
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber]];
Here Below is my code
NSString *string = [NSString stringWithFormat:#" http://abc.com /Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#& ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];
Here in string there is value but url returns nil.
Can Anyone tell why this happened.
Thanks ....
"This won't work, so here's what I did instead"
NSString *string = [NSString stringWithFormat:#"http://abc.com/Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];
NSURL will return nil for URLs that contain illegal chars, like spaces.
Before using your string with [NSURL URLWithString:] make sure to escape all the disallowed chars by using [NSString stringByAddingPercentEscapesUsingEncoding:].
Here is the class reference for NSString.
Hi All Now My Problem Is solved here I did a mistake left a blank space before "http".
The correct code is
NSString *string = [NSString stringWithFormat:#"http://myserver.com/Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];