Basically I did this and I got an error:
NSString *searchWord = #"Lilwayne";
NSString *resourceURL = (#"https://api.soundcloud.com/tracks?client_id=546952635e22cc0182d85daceff34381&q=%#&format=json", searchWord);
Error is:
reason: 'Resource 'Lilwayne' is invalid because the scheme is not 'https'.'
I don't understand why this doesn't work. However if I remove the "%#" and replace it with "Lilwayne" it works.
The reason why I am doing it this way is because I have a search feature in my app to search for songs using the soundcloud sdk and I want to dynamically change the value of the variable "searchword" to whatever the user typed in.
Try to use stringWithFormat
NSString *resourceURL = [NSString stringWithFormat:#"https://api.soundcloud.com/tracks?client_id=546952635e22cc0182d85daceff34381&q=%#&format=json", searchWord];
I suggest a trip to the NSString class reference in the Xcode help system. In addition to stringWithFormat, as suggested by Basheer, There is a section on combining strings.
Related
NSLog displays unknown garble with the following code:
NSString *sampleStr = #"你好";
NSLog(sampleStr);
But it outputs:
As I send the above characters to backend, backend shows
?????
How do I fix this problem?
What the problem specifically? NSLogging or the backend one?
If the former, first of all, you should use string formatting to output strings instead of passing the NSString object directly. This must give you desired output:
NSLog(#"%#", sampleStr);
Though, I tried your code without changes in Xcode 13.1 and it gave me the desired output nevertheless. So, probably the problem is somewhere else, not in your NSLog:
NSString *sampleStr = #"你好";
// "Format string is not a string literal (potentially insecure)"
NSLog(sampleStr); // 你好
NSLog(#"%#", sampleStr); // 你好
If you mean backend as your problem, it depends on how you "send" it to backend (in a body as data, as an HTTP parameter as text, with/without escaping, etc.) and whether your backend code supports the input you provide. To answer that more precisely we should have more information.
I have a url that retrieves data from a Web API which looks like this with the entry of "Pizza Hut":
NSString *urlString = #"https://api.nutritionix.com/v1_1/search/Pizza Hut?results=0%3A20&cal_min=0&cal_max=50000&fields=item_name%2Cbrand_name%2Citem_id%2Cbrand_id&appId=MY_APP_ID&appKey=MY_APP_KEY";
This URL will return all the menu items of Pizza Hut.
Now I want to take a step beyond hard coding values, and so I created a text box where users can enter their own restaurant, and the web api should return data.
Here is an example of that:
NSString *urlString = [NSString stringWithFormat:#"https://api.nutritionix.com/v1_1/search/%#?results=0%3A20&cal_min=0&cal_max=50000&fields=item_name%2Cbrand_name%2Citem_id%2Cbrand_id&appId=MY_APP_ID&appKey=MY_APP_KEY", searchText.text];
All I did here was change the "Pizza Hut" to "%#".
However, I get a warning from the compiler saying:
"More '%' conversions than data arguments. As you would expect, the API returns no data, for this code doesn't seem to be working.
How would I re-write this string so that I could put the placeholder in there?
You have other percent symbols that need to be escaped properly. You want:
NSString *urlString = [NSString stringWithFormat:#"https://api.nutritionix.com/v1_1/search/%#?results=0%%3A20&cal_min=0&cal_max=50000&fields=item_name%%2Cbrand_name%%2Citem_id%%2Cbrand_id&appId=MY_APP_ID&appKey=MY_APP_KEY", searchText.text];
Basically, add a 2nd % symbol before all of the % symbols that you actually want to appear in the string.
BTW - make sure you properly escape the search text so special characters (such as spaces) are properly encoded.
I loaded libqrencode library in my cocoa project but I'm not sure how to use it exactly. I have a text field in which you type a text, and once done you click a button and I log that text with NSLog. Now I want to encode that text to be able to use it later and generate a QRcode out of it, so in the manual it's saying to use this format
QRcode* QRcode_encodeString (const char * string,
int version,
QRecLevel level,
QRencodeMode hint,
int casesensitive
)
I am not sure how to use that in my method to log the results as well
- (IBAction)GenerateCode:(id)sender {
NSString *urlText = [[NSString alloc] initWithFormat:#"%#", [_urlField stringValue]];
NSLog(#"The url is %#", urlText);
}
You need to get from an NSString instance to a const char *. This has been answered several times on SO, but here's one. Once you do that, you can call QRCode_encodeString() directly and pass whatever you desire for the arguments.
If you need more specifics, you'll have to try something, post your code, and describe how it's not working for you so we can help you more directly without just writing it for you.
I'm attempting to generate an HTML-output for a model in my app. This will effectively go through and fill in various spots inside an HTML file with the relevant values within the model. I was originally just going to take the HTML template as a formatted string, but if anything changes later on the layout or anything, this will just get really messy and tedious to match up the order of the values to the order they appear in the template.
Instead, what I'm trying to do is run a sort of Ruby-style string interpolation of the file. Anywhere I want a value from the model, I put the name of the model attribute I want like so: #{key.path}.
Then, I'm attempting to process this with the following Regex:
#"#{([^}]+)}".
To process this, I'm using the following setup:
NSString *processedTemplate = [regex stringByReplacingMatchesInString:template
options:0
range:NSMakeRange(0, template.length)
withTemplate:[self valueForKeyPath:#"$1"]];
However, when I run this, I get the error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<Plan 0x78349d0> valueForUndefinedKey:]: this class is not
key value coding-compliant for the key $1.'
What I expect is that I can use the regex match and use it to grab the key-value coding compliant value in my model. However, this clearly doesn't work the way I'm using it.
On a side note, I think I'm using this right, but when I just run this to replace withTemplate:#"$1" I get NULL. So, I tried it using:
NSString *processedTemplate = [template stringByReplacingOccurrencesOfString:#"#{([^}]+)}"
withString:#"$1"
options:NSRegularExpressionSearch
range:NSMakeRange(0, template.length)];
However, when I run this it doesn't replace with the section in (). So one way or another, I'm not doing something right. Anyone have any pointers/solutions?
Update
So it looks like the withString: parameter will interpret #"$1" as whatever the regex match data has found. Is there another way to retrieve match data so it can be passed into such methods as valueForKeyPath:?
Update
On my side note, I don't know why, but my regex #{([^}]+)} does not match as I expect it to. Any other regex simulator I put it up against seems to match it just fine, but it doesn't match in obj-c on iOS. Is there something I'm missing with escapes on character set #{}?
What kind of object is self in your first example? Have you overridden valueForKeyPath:?
The method -valueForKeyPath: is defined for NSObject to return a value based on KVC key paths. The code
obj = [anotherObj valueForKeyPath: #"foo.bar.baz"];
will first send foo to anotherObj, then send bar to the result, then send baz to the result of that and return the final result.
Essentially, the runtime is complaining that you don't have a method called -$1.
And by the way, in Objective-C parameters to methods are evaluated before the method itself, so
[self valueForKeyPath:#"$1"]
is evaluated before stringByReplacingMatchesInString: and $1 means nothing special to valueForKeyPath:.
I want to print out the Text contents of a NSTextView using the NSLog function in Objective-C. The code I have so far is:
NSString *s=[updateSource textStorage];
NSLog(s);
All I get is the error:
[NSConcreteTextStorage getCharacters:range:]: selector not recognized [self = 0x43f4b0]
Use [updateSource string] instead. [updateSource textStorage] is not an NSString, but rather an NSTextStorage.
It's not the cause of your problem, but you should be using NSLog(#"%#",s); to log your string. The first argument of NSLog should always be a format string, and not the value you're trying to log.
(if you don't, your app will likely crash if the value contains percent characters)