NSInvalidArgumentException with stringByAppendingPathComponent - ios

i have a bundel settings with a string : version
in my bundle settings version = 2.9.1
in my project, if i use this code, all is good :
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"ma/V_2.9.1/gma2/fixture_layers/ifocus.xml"];
if i use this code :
NSString *path = [documentsDirectory stringByAppendingPathComponent:(#"ma/V_%#/gma2/fixture_layers/ifocus.xml", version)];
i have this error :
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITextField length]: unrecognized selector sent to instance 0x9753190'

NSString * path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"ma/V_%#/gma2/fixture_layers/ifocus.xml", version]];

Since you're already including multiple /'s in your string, there's no advantage to using stringByAppendingPathComponent. So you may as well just use:
NSString* path = [documentsDirectory stringByAppendingFormat:#"ma/V_%#/gma2/fixture_layers/ifocus.xml", version];
Another option, that gives you the benefit of stringByAppendingPathComponent, is to break it right down like this:
NSString* path = [[[[[documentsDirectory stringByAppendingPathComponent:#"ma"] stringByAppendingPathComponent:[NSString stringWithFormat:#"V_%#", version]] stringByAppendingPathComponent:#"gma2"] stringByAppendingPathComponent:#"fixture_layers"] stringByAppendingPathComponent:#"ifocus.xml"];
But that's kind of ugly.

Related

AFNetworking crashing in multilingual app

Ride[source_name]=2152,%20Mohali%20Stadium%20Rd,%20Phase%2010,%20Sector%2064,%20Sahibzada%20Ajit%20Singh%20Nagar,%20Punjab%20160062,%20भारत
This parameter is causing crash while running in Hindi while working fine with Spanish and English. Please suggest me on it. Crash description is as following:-
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not
satisfying: URLString'
Check with this
Objective - C
NSString *string = #"भारत";
NSString *encoded = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
Swift 3.0
let string = "भारत"
let urlString = string.addingPercentEncoding( withAllowedCharacters: . urlUserAllowed)
Output :: %E0%A4%AD%E0%A4%BE%E0%A4%B0%E0%A4%A4
Add below line of code to avoid invalid parameters in url.
NSString *str = ...; // Your URL
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet];
NSString *result = [str stringByAddingPercentEncodingWithAllowedCharacters:set];
Deprecated Code Before ios 9.0 :
NSString *str = ...; // Your URL
NSString *urlAsString = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

SIGABRT on cast to UIImage

I'm using FastttCamera as a wrapper to AVFoundation for taking, processing and displaying a picture. Here's my code (a FastttCamera delegate method) in which I cast (FastttCapturedImage *) to (UIImage *):
- (void)cameraController:(FastttCamera *)cameraController didFinishScalingCapturedImage:(FastttCapturedImage *)capturedImage
{
//Use the captured image's data--note that the FastttCapturedImage is cast to a UIImage
NSData *pngData = UIImagePNGRepresentation((UIImage *)capturedImage);
//Save the image, and add the path to this transaction's picPath attribute
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
int timestamp = [[NSDate date] timeIntervalSince1970];
NSString *timeTag = [NSString stringWithFormat:#"%d",timestamp];
NSString *filePath = [documentsPath stringByAppendingString:timeTag]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
self.thisTransaction.picPath = filePath;
[self.viewFinder setImage:(UIImage *)capturedImage];
}
However, I'm getting a SIGABRT at the line:
NSData *pngData = UIImagePNGRepresentation((UIImage *)capturedImage);
with this console readout:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FastttCapturedImage CGImage]: unrecognized selector sent to instance 0x17e2a100'
I'm confused by the reference to CGImage. Is it a problem with my typecast? There was no complaint on build. Can someone please set me straight?
Read the docs for FastttCapturedImage. It's not a UIImage so you can't just cast it like that. Use the provided property to get the UIImage.
Change the problematic line to:
NSData *pngData = UIImagePNGRepresentation(capturedImage.fullImage);
You have the same problem later:
[self.viewFinder setImage:capturedImage.fullImage];
BTW - you didn't get a problem while compiling because a cast is a way to tell the compiler "trust me, it's really what I'm telling you it is". The problem here is you were wrong, it's not really what you claimed it was. :)

UIPasteboard: NSString refuses to copy to clipboard

I have the following code to shorten a URL using the bit.ly API.
NSString *shortenedURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://api.bit.ly/v3/shorten?login=%#&apikey=%#&longUrl=%#&format=txt", login, key, self.link.text]] encoding:NSUTF8StringEncoding error:nil];
I also have the following code to copy the shortened URL to the pasteboard:
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = shortenedURL;
However, this does not work. In the output log, this is what is displayed:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPasteboard setString:]: Argument is not an object of type NSString [(null)]'
So if the argument isn't an object, what is it? I tried assuming it was a URL with this:
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.URL = shortenedURL;
The same type of error is produced, only saying that the argument is not an NSURL object, instead of the previous error saying the argument is not an NSString object.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPasteboard setURL:]: Argument is not an object of type NSURL [(null)]'
Anyone know what to do?
Unfortunately the string property on the UIPasteboard object is not actually a direct reference to a string, but an interface with getters and setters for an Objective-C array of the given type you care about.
Swift provides optionals, but unfortunately UIPasteboard is Objective-C under the hood, which doesn't really support optionals well. This is one of those cases.
If you assign nil (null) to the string property, execution will wrap your null in square brackets as an array [(null)] then attempt to add it to empty array of type [NSURL] and because [(null)] does not match the type of [NSURL] you will get:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPasteboard setURL:]: Argument is not an object of type NSURL [(null)]'
😤
UIPasteboard GitHub doc
unsafe initializer used
NSString *shortenedURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://api.bit.ly/v3/shorten?login=%#&apikey=%#&longUrl=%#&format=txt", login, key, self.link.text]] encoding:NSUTF8StringEncoding error:nil];
is nil, so it's not a good idea to ignore the error.
Instead of that, do
NSError *loadingError = nil
NSString *shortenedURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://api.bit.ly/v3/shorten?login=%#&apikey=%#&longUrl=%#&format=txt", login, key, self.link.text]] encoding:NSUTF8StringEncoding error:&loadingError];
if (!shortenURL) {
NSLog(#"Error loading: %#", loadingError);
return;
} else {
NSLog(#"Success loading: %#", shortenedURL);
}
You should get "Error loading: error message here", and debug the exact problem what happened.

'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'

Im inserting a string into a NSMutableString like as
NSMutableString *string = (NSMutableString *)self.label.text;
[string insertString:#"New " atIndex:0];
these line of codes working properly iOS 6 device. but in iOS 7 it throws an exception as Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds' (i'm running ios 6 app in ios 7 device).
can any body tell why it's happening? please.
thanks
You can't convert an NSString to an NSMutableString simply by casting. Do this instead:
NSMutableString *string = [self.label.text mutableCopy];

iOS Seemingly the Same Strings with different behaviour

I'm storing a WAV filename in an array, including the extenstion. When I try to load it it is requiring the extension be removed. If I substring the extension off it abends, while the seemingly same string works fine when hard-coded.
I also tried using the stringByDeletingPathExtension method, with the same crash.
This code causes a crash
NSString *fileName = [[currentWord.audioFile componentsSeparatedByString:#"."] objectAtIndex: 0];
SFCLog(#"fileName: %#",fileName);
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"wav"];
CFURLRef BaseURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
-[ViewController loadWord] [line 143] fileName: owe_fr
2012-08-20 17:50:21.561 Hello[8053:16a03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
But this code executes
NSString *fileName = #"owe_fr";
SFCLog(#"fileName: %#",fileName);
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"wav"];
CFURLRef fluentBaseURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
OK I found it. A leading space in the stored filename version - you can see it in the log statement. I found it by logging like this (Markers to either side of the string):
SFCLog(#"fileName :%#:",fileName);
Which logged as:
-[ViewController loadWord] [line 144] fileName : about_fr:
Cheers,
Sean

Resources