Objective-c NSInvalidArgumentException Xcode7 - objective-c

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.

Related

Objective-C set variable to what is returned from method

I have this code:
-(NSURL *)urlOfCurrentlyPlayingInPlayer:(AVPlayer *)player{
// get current asset
AVAsset *currentPlayerAsset = player.currentItem.asset;
// make sure the current asset is an AVURLAsset
if (![currentPlayerAsset isKindOfClass:AVURLAsset.class]) return nil;
// return the NSURL
return [(AVURLAsset *)currentPlayerAsset URL];
}
why can't i just do this?
NSURL *currentSong=[urlOfCurrentlyPlayingInPlayer:_audioPlayer1];
the error message is "Expected expression" with the N of NSURL underlined in red
sorry for the noob question
Assuming urlOfCurrentlyPlayingInPlayer is declared in the same class:
NSURL *currentSong = [self urlOfCurrentlyPlayingInPlayer:_audioPlayer1];
If it's an instance method of some other class it would be
NSURL *currentSong = [myInstanceOfSomeOtherClass urlOfCurrentlyPlayingInPlayer:_audioPlayer1];

Error building from 5 to 6 IOS

I have IPad application for IOS 5.1, now i'm trying to build it for 6.0, and get an error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
-[NSFileManager copyItemAtPath:toPath:error:]: source path is nil'
"
i found where is this problem, but i don't know how to correct it
in my AppDelegate
-(void)copyFileWithName:(NSString*)name Extension:(NSString*)extension ToDir:(NSString*)dirName withName:(NSString*)newName
{
NSString *folderPath = dirName;
NSString *filePathDocTxt = [folderPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.%#", newName, extension]];
NSString *filePathBundleTxt = [[NSBundle mainBundle] pathForResource:name ofType:extension];
//when i comment these two lines, my code compiles success, but i need these code
if(![[NSFileManager defaultManager] fileExistsAtPath:filePathDocTxt])
[[NSFileManager defaultManager] copyItemAtPath:filePathBundleTxt toPath:filePathDocTxt error:nil];
}
well the error is source path for the copyItemAtPath method is nil.
Since you copy from the bundle (your app) to another folder that implies that the file is not in the target.
make sure the file you attempt to copy is there and correctly added to the target (in Xcode)
to resolve the crash add an if to exclude copying if the item isn't there / assert it
NSString *filePathBundleTxt = [[NSBundle mainBundle] pathForResource:name ofType:extension];
assert(filePathBundleTxT);

Why does NSMutableURLRequest instantiation cause a crash?

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

Playing video on IOS

I have downloaded the source files from
http://mobile.tutsplus.com/tutorials/iphone/mediaplayer-framework_mpmovieplayercontroller_ios4/
and the app works perfectly, but when I try to swap out my own video by changing the following code:
pathForResource:#"big-buck-bunny-clip" ofType:#"m4v"
to
pathForResource:#"example" ofType:#"mp4"
the app crashes when I try to play the video and I receive a message in the output saying:
"2012-09-13 20:32:59.106 BigBuckBunny[1081:11f03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[NSURL initFileURLWithPath:]: nil string parameter'
** First throw call stack:
(0x1722022 0x10f6cd6 0x16caa48 0x16ca9b9 0x4e53b 0x4e4c5 0x2da4 0x1723e99 0x36d14e 0x36d0e6 0x413ade 0x413fa7 0x413266 0x3923c0 0x3925e6 0x378dc4 0x36c634 0x1e1aef5 0x16f6195 0x165aff2 0x16598da 0x1658d84 0x1658c9b 0x1e197d8 0x1e1988a 0x36a626 0x297d 0x28f5)
terminate called throwing an exception(lldb) "
I have my new video file inside the folder with the original big-buck-bunny-clip.m4v file, and I put it in the project.
What am I doing wrong and how do I fix this?
I managed to play my own video by going to the file within Xcode and checking the box next to BigBuckBunny under Target Membership
Check file exits and filepath you r passing is nil:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"example" ofType:#"mp4"];
if([[NSFileManager defaultManager] fileExitsAtPath:filepath])
{
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
else
{
NSLog(#"File not exists");
}

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