Domanda

I am trying to create a NSMutableURLRequest like this:

NSURL *URLWithString = [
    NSString stringWithFormat:@"%@?%@",
    urlString,
    datas
];
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:URLWithString] autorelease];

When I run it on my iPhone 4S, the app crashes and I get the following exception:

2012-10-30 15:58:53.495 [429:907] -[__NSCFString absoluteURL]: unrecognized selector sent to instance 0x1cd74a90

2012-10-30 15:58:53.497 [429:907] --- Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString absoluteURL]: unrecognized selector sent to instance 0x1cd74a90'

--- First throw call stack:

(0x361b62a3 0x344c697f 0x361b9e07 0x361b8531 0x3610ff68 0x3611363f 0x320396e7 0x32039551 0x320394ed 0x33bde661 0x33bde597 0x387e1 0x376d9f1f 0x376da9a9 0x341c535d 0x3618b173 0x3618b117 0x36189f99 0x360fcebd 0x360fcd49 0x366392eb 0x374db301 0x37cc1 0x37c58)

libc++abi.dylib: terminate called throwing an exception

What's wrong?

È stato utile?

Soluzione

Lots of issues: For a start look into how to invoke NSString and NSURL methods. I have done it for the code you have pasted.

NSURL * myUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",urlString,datas]];

and

NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:myUrl] autorelease];

Altri suggerimenti

Your NSURL creation code is wrong.

NSURL *URLWithString = [NSString stringWithFormat: @"%@?%@", urlString, datas];

Here, you are trying to create an NSURL directly with an NSString class method (stringWithFormat). The result is that your variable URLWithString will be of the wrong type, and when you send it an NSURL message you'll get a crash like you are getting.

To fix this you need to create an NSString of the URL address first, and instantiate an NSURL using that, like so:

NSString *completeURLString = [NSString stringWithFormat:@"%@?%@", urlString, datas];
NSURL *completeURL = [NSURL URLWithString: completeURLString];

(Technically, these two lines can be combined; I've separated them to make it clear what's going on.)

Also, whilst probably not related to your crash, you shouldn't call your URL variable URLWithString, as that is the name for a class method of NSURL. Make sure to come up with a unique name for it, and to start it with a lower case character (at the very least, this will make your code easier to decipher for others).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top