Unrecognized selector error - ios

I am getting this error: [NSURL stringByAppendingFormat:]: unrecognized selector sent to instance 0x5869210 when it gets to the append. strcust is a plain number and strURI is correct until the append.
NSString *strUIR = [NSURL URLWithString:#"https://cid.hooru.mobi:36610/?";
strURI = [strURI stringByAppendingFormat:#&Cust=IPRR%#", strCust];
Any thoughts would be appreciated. I am just trying to append Name/value pairs from variables. Can't get the append to work.

There are several problems with this piece of code:
You're declaring an NSString but you are assigning an NSURL
You're missing a right square bracket ']'
You're missing a double quote on your second line
You're trying to call an NSString method on an NSURL object
You're misspelling strURI on your first line (it is strUIR)
Try this:
NSString *strURI = #"https://cid.hooru.mobi:36610/?";
strURI = [strURI stringByAppendingFormat:#"&Cust=IPRR%d", strCust];
//Note the %d (if strCust is an int. If it's an NSString use %#)
NSURL *myUrl = [NSURL UrlWithString:strURI];

[NSURL URLWithString:] returns pointer of type NSURL. Merely collecting the return value that is NSURL* type in NSString* type does not convert it to NSString* type. Therefore strUIR is NSURL* type, even though declared as NSString strUIR, and hence you cannot pass any message to strUIR that is supposed to be passed to NSString type.

NSURL doesn't respond to stringByAppendingFormat:
You specify strURI as an NSString so the compiler won't raise a warning, but you set it to an NSURL.
Either initialize the full string before creating the NSURL, or use URLByAppendingPathComponent:, I would recommend the first option.
NSString *path = #"https://cid.hooru.mobi:36610/?"
...
path = [path stringByAppendingFormat:#"&Cust=IPRR%#", strCust];
NSURL *url = [NSURL URLWithString:path]
For more information, see the docs: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html

Related

[__NSCFString absoluteString]: unrecognized selector sent to instance, attempt to convert url to string

Looking to simply convert the parse image url to a string so I can use SDWebImage for caching, etc.
After researching I found that I can convert from a url to string by calling absoluteString on the NSURL. I've also tried this:
NSURL *theUrl = [[obj objectForKey:#"image"] url];
NSString *finalUrl = [theUrl absoluteString];
[cell.carPhoto setImageWithURL:[NSURL URLWithString:finalUrl]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
but I am also crashing on this line with the same error
NSString *finalUrl = [theUrl absoluteString];
Any ideas? By the way obj is a PFObject
The error message is telling you that you have an instance of __NSCFString, and you're calling absoluteString on it. __NSCFString is a kind of NSString, so the situation is simply that you expect (for some reason) that you have an NSURL, but you actually have an NSString - so you don't need to call absoluteString.
It would be good to understand why you think you have an NSURL and whether you will ever have one in this piece of code. The inputs should really be consistent, but if not you can check the class and decide how to get the string version.
It looks like you're trying to convert a NSString to an NSURL with this line:
NSURL *theUrl = [[obj objectForKey:#"image"] url];
But that's not how you convert an NSString to an NSURL and so your NSString isn't being changed to an NSURL as you'd like. Instead, it seems as if it's remained an NSString, thus the error.
There's no need to convert the NSString to an NSURL if the only purpose of that NSURL is to be converted back to an NSString on the next line by accessing its absoluteString property; but if you do in fact need theURL to be an NSURL for reasons beyond the code you've posted (for example, if you need to access the NSURL variable later in your code), try this instead to properly convert your string into an url:
NSURL *theUrl = [NSURL URLWithString:[obj objectForKey:#"image"]];

How do I add a number to an NSURL? Too many arguments error

I've got a small problem that seems a little bit odd to me. I often used NSString or NSLog while adding NSNumbers into several places:
NSNumber *categoryId = [[NSNumber alloc]initWithInt:0];
NSURL *url = [NSURL URLWithString:#"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]];
Now xcode tells me that I'm too many arguments. What am I doing wrong? Setting up an NSNumber into NSStrings or NSLogs works as I did it above.
Best Regards
What is wrong is on
NSURL *url = [NSURL URLWithString:#"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]];
you are calling URLWithString: and then pass in a string that is not being formatted correctly. If you want to do it all on one line then you need to be using stringWithFormat: like
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]]];
Because it is adding a parameter you can't just create a string like you normally would with #"some text" you need to format it using the stringWithFormat: which will return an NSString * with the text held within #"" and the paramters you pass in. So [NSString stringWithFormat:#"My String will come with %#", #"Apples"]; this would provide an NSString with "My String will come with Apples". For more information check out the Apple Documentation for NSString and stringWithFormat:
Try this :
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://shop.rs/api/json.phpaction=getCategoryByCategory&category=%i", [categoryId integerValue]]];
Initially code was wrong because of : "categoryId integerValue]" (I forgot a '[').
You can use NSString to form your NSURL. You can then pass it to your URLWithString like below:
NSNumber *categoryId = [NSNumber numberWithInteger:0];
NSString *urlString = [NSString stringWithFormat:#"http://shop.rs/api/json.php?action=getCategoryByCategory&category=%i",[categoryId integerValue]];
NSURL *url = [NSURL URLWithString:urlString];

NSUrl fileURLWithPath returns strange chinese signs

I have an NSString path to my Documents folder.
NSString* stringURL = #"/var/mobile/Applications/5667FADC-F848-40CF-A309-
7BFE598AE6AB/Library/Application Support/MyAppDirectory";
When I cast it to NSUrl with
NSURL* url = [NSURL fileURLWithPath:stringUrl];
and NSLog(#"Created URL: %#",url);, i get some strange result:
///var/mobile/Applications/5667FADC-F848-40CF-A309-7BFE598AE6AB/Library/Application㤈㤋ތȀ乽啓汲唠䱒›楦敬⼺⼯慶⽲潭楢敬䄯灰楬慣楴湯⽳㘵㜶䅆䍄䘭㐸ⴸ〴䙃䄭〳ⴹ䈷䕆㤵䄸㙅䉁䰯扩慲祲䄯灰楬慣楴湯㈥匰灵潰瑲䴯䅹灰楄敲瑣牯⽹upport/MyAppDirectory/
Why is this so ?
What am I doing wrong ?
I didn't see any Chinese character when I log the value.
NSString* stringURL = #"/var/mobile/Applications/5667FADC-F848-40CF-A309-7BFE598AE6AB/Library/Application Support/MyAppDirectory";
NSURL* url = [NSURL fileURLWithPath:stringURL];
NSLog(#"%#",url);
Classical mistake. Don't use NSLog (url), use NSLog (#"%#", url). The first argument to NSLog is a format string, and % characters in format strings are interpreted, not printed. For example, %s in a format string means another C-String is expected in the argument list. Since url could contain all kinds of characters, this is likely to lead to rubbish results or even crashes.
Based on the answer you accepted from a previous question; it's because the use of stringByAddingPercentEscapesUsingEncoding will generate a printf-like formatting string containing %20S (the space between Application Support is converted to %20), which confuses NSLog():
NSURL *url = [NSURL fileURLWithString:[stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] isDirectory:YES];
NSLog(url);
use NSLog("#%", url) to avoid this error.

Data argument not used by format string but it works fine

I used this code from the Stack Overflow question: URLWithString: returns nil:
//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* stringURL = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
When I copied it into my code, there wasn't any issue but when I modified it to use my url, I got this issue:
Data argument not used by format string.
But it works fine. In my project:
.h:
NSString *localisationName;
.m:
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* stringURL = [NSString stringWithFormat:#"http://en.wikipedia.org/wiki/Hősök_tere", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
How can I solve this? Anything missing from my code?
The # in the original string is used as a placeholder where the value of webName is inserted. In your code, you have no such placeholder, so you are telling it to put webName into your string, but you aren't saying where.
If you don't want to insert webName into the string, then half your code is redundant. All you need is:
NSString* stringURL = #"http://en.wikipedia.org/wiki/Hősök_tere";
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
The +stringWithFormat: method will return a string created by using a given format string as a template into which the remaining argument values are substituted. And in the first code block, %# will be replaced by value of webName.
In your modified version, the format parameter, which is #"http://en.wikipedia.org/wiki/Hősök_tere", does not contain any format specifiers, so
NSString* stringURL = [NSString stringWithFormat:#"http://en.wikipedia.org/wiki/Hősök_tere", webName];
just runs like this (with the warning Data argument not used by format string.):
NSString* stringURL = #"http://en.wikipedia.org/wiki/Hősök_tere";

iOS: parse url of a selected row in didSelectRowAtIndexPath method

A. Is it possible/appropriate to initialize NSXML parse for a selected row inside didSelectRowAtIndexPath in order to populate the detail view that the method pushes to? I want to parse the URL associated with the title of a selected item as a detailview...
B. If yes, then how can I make it happen? I have 'afeed.title' In the following critter:
NSXMLParser *urlParser = [[NSXMLParser alloc] initWithContentsOfURL:afeed.url];
A caution line exclaims at afeed.url: "Incompatible pointer types sending 'NSString *' to parameter of type 'NSURL *'"
afeed is create by this:
ArticleGroupLink *afeed = [array objectAtIndex:indexPath.row];
ArticleGroupLink is a class containing title (title was used in the cellForRowAtIndexpath method) & url (string & # property), which synthesizes them.
Let me know anything else that I need to share : )
As the compiler is trying to tell you is that it's expecting a NSURL object but instead you are giving it a NSString object.
So you can try to convert your NSString into a NSURL by doing something like this:
NSString *urlString = afeed.url;
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Then you can do:
NSXMLParser *urlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

Resources