Why does NSMutableURLRequest instantiation cause a crash? - objective-c

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?

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];

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).

Related

+[NSData bookmarkDataWithContentsOfURL:]: unrecognized selector sent to class

I am trying to resolve an alias file's original path using Objective-C(or maybe C++; it's an .mm file). Not being very much familiar, I am somehow missing + and - methods' usage. I am aware of them being class and instance methods respectively, but in practice, the following the code, with the indicated lines give me following warning and error(at build):
Class method '+bookmarkDataWithContentsOfURL:' not found (return type defaults to 'id')
-
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSData bookmarkDataWithContentsOfURL:]: unrecognized selector sent to class 0x7fff88942cb8'
with 0x7fff88942cb8 being the NSData address as per lldb.
Which files should I make the changes in, to get bookmarkDataWithContentsOfURL:error: and URLByResolvingBookmarkData to work?
void *pathclass::resolveAliasFromURL(const char *filepath) const
{
NSError *error = nil;
NSString *filepathh = [[NSString alloc] initWithUTF8String:filepath];
NSData *bookmarkk = [NSData bookmarkDataWithContentsOfURL:filepathh]; /*problematic line*/
BOOL isstale = NO;
NSURL *actual = [NSURL URLByResolvingBookmarkData:bookmarkk bookmarkDataIsStale:isstale error:error];/*another problematic line, but build fails already*/
NSString *urlString = [actual absoluteString];
NSLog(#"%#",urlString);
}
If there are any other faults, please point out.
Your call to bookmarkDataWithContentsOfURL: is wrong in a few ways:
The signature looks like this:
+ (NSData *)bookmarkDataWithContentsOfURL:(NSURL *)bookmarkFileURL error:(NSError * _Nullable *)error;
First, the first parameter is of type NSURL*, not NSString*. Next, you miss off the error parameter completely (despite defining a variable for it). Lastly, the method is a class method on NSURL not NSData (NSData* is the return type).
So, first, make your file path into an NSURL*:
NSURL* bookmarkUrl = [NSURL URLWithString:filepathh];
Then, call the function using the proper arguments:
NSData *bookmarkk = [NSURL bookmarkDataWithContentsOfURL:bookmarkUrl error:&error];
You should check the returned value against nil - if it's nil, then an error occurred, and the error information will be contained inside error.
The documentation is quite helpful.
Your call to URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error: has similar problems: you are missing several parameters, the first parameter should be NSURL, etc. Again, the documentation should help.

Objective-c NSInvalidArgumentException Xcode7

Hey Guys I'm just gonna cut to the chase I was trying to play audio
- (IBAction)click1:(id)sender{
Drill.image = [UIImage imageNamed:#"Drillx2"];
AudioServicesPlaySystemSound(PlaySoundID);
}
- (IBAction)click2:(id)sender
{
Drill.image = [UIImage imageNamed:#"Drillx1"];
}
- (void)viewDidLoad {
NSURL *SoundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Drill" ofType:#"m4a"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, &PlaySoundID);
[super viewDidLoad];
}
And I get this error
'NSInvalidArgumentException', reason: '*** -[NSURL
initFileURLWithPath:]: nil string parameter'
Thanks,
Derek
Note : Yes the resource for the sound is named Drill.
In the code
NSURL *SoundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Drill" ofType:#"m4a"]];
It didn't find the given file in the bundle file path. make sure you have added file in the proper target and also build again after adding the resource to make sure it exist in the bundle
Reference : As written in the parameter Passing nil for this parameter produces an exception.

how to solve run time error of [NSURL fileURLWithPath:error:]: unrecognized selector sent to class?

I am beginner in IPhone Developing. I want play sound. so, that's why I have apply this code
(void)viewDidLoad
{
NSError* err;
path=[[NSBundle mainBundle] pathForResource:#"Animalfile" ofType:#"plist"];
dict=[NSDictionary dictionaryWithContentsOfFile:path];
NSArray *animalaudio=[dict valueForKey:#"audio"];
NSString *audiolist=[animalaudio objectAtIndex:currentsound];
AVAudioPlayer *audio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:audiolist error:&err]];
audio.delegate=self;
[audio play];
}
and I have got the run time error of
[NSURL fileURLWithPath:error:]: unrecognized selector sent to class 0x182121c
2012-07-14 14:51:21.711 plistdemo[1236:10703] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '+[NSURL fileURLWithPath:error:]:
unrecognized selector sent to class 0x182121c'
terminate called throwing an exceptionsharedlibrary apply-load-rules all
so, give any suggestion and source code which apply in my code
current documentation for NSURL doesn't list any such method like 'fileURLWithPath:error:'... seems like its either deprecated or incorrect.
instead try using something like:
[NSURL fileURLWithPath:audioList]
hope it helps...

Formatting NSURL

In my app, I need to make a json request to google direction service through NSURL.
I'm able to get JSON response, when the URL is static.
NSURL *googleMapsURL=[NSURL URLWithString:#"http://maps.googleapis.com/maps/api/directions/json?origin=SanFrancisco,CA&destination=Dallas,TX&waypoints=Los+Angeles,CA%7CHouston,TX&mode=driving&sensor=false"];
NSData *data=[NSData dataWithContentsOfURL:googleMapsURL];
But, when I specify the source and destination stations dynamically, it's not working.
NSString *urlString=[NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/directions/json?origin=%#&destination=%#&waypoints=Los+Angeles,CA%7CHouston,TX&mode=driving&sensor=false",sourceStation, destinationStation];
NSURL *googleMapsURL=[NSURL URLWithString:urlString];
NSData *data=[NSData dataWithContentsOfURL:googleMapsURL];
It's generating the following error.
2012-05-08 03:30:15.960 SupplyTrackerApp2[3079:16403] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(0x19a7052 0x2448d0a 0x194fa78 0x194f9e9 0xecdfbd 0x2aae6 0x2a900 0x56ffbf 0x57021b 0x5704c3 0x580b71 0x5813df 0x581986 0x5815a4 0x34650 0x19a8ec9 0x4ab5c2 0x4ab55a 0x550b76 0x55103f 0x5502fe 0x4d0a30 0x4d0c56 0x4b7384 0x4aaaa9 0x1bbefa9 0x197b1c5 0x18e0022 0x18de90a 0x18dddb4 0x18ddccb 0x1bbd879 0x1bbd93e 0x4a8a9b 0x25cd 0x2535 0x1)
terminate called throwing an exception
Can any one help me?
To me it looks like either sourceStation or destinationStation is nil. For debugging you could try setting them explicitly in the lines before your NSString *urlString definition.
NSString *sourceStation = #"SanFrancisco,CA";
NSString *destinationStation = #"Dallas,TX";
If this works, then it's a problem with filling the respective NSStrings...

Can't instantiate NSURL with file path

When creating an NSURL with [NSURL fileURLWithPath:[#"~/Movies" stringByExpandingTildeInPath]], I get following error in the console:
-[NSURL initWithScheme:host:path:]: path file:/localhost/Users/michael is not absolute.
It worked while compiling in Debug mode, this problem occured only after switching to Release.
EDIT: Just to clarify, I get the error message at run time when the NSURL object is initialized, not while building.
I think your code needs to look like:
NSURL *url = [NSURL fileURLWithPath:[#"~/Movies" stringByExpandingTildeInPath]];
Looks to me like you are missing the #"" that needs to surround '~/Movies'.
works fine here ...
NSURL *url = [NSURL fileURLWithPath:[#"~/Movies" stringByExpandingTildeInPath]];
NSLog(#"url: %#", url);
BUT
is your app sandboxed in release mode? That could explain this