How to resolve a NULL cString crash - ios

I'm getting a crash with the following encoding fix I'm trying to implement:
// encoding fix
NSString *correctStringTitle = [NSString stringWithCString:[[item objectForKey:#"main_tag"] cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
cell.titleLabel.text = [correctStringTitle capitalizedString];
my crash log output states:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithCString:encoding:]: NULL cString'
thanks for any help

You seem to think that a double conversion is necessary in order to get the correct result. It isn't, nor is it possible (that's why the call to cStringUsingEncoding:NSISOLatin1StringEncoding returns NULL). Just leave this part out and assign to correctStringTitle directly.

I had the same problem with italian accented characters while trying to get text from html and my solution to avoid the situation that cStringUsingEncoding:NSISOLatin1StringEncoding returns NULL is ensuring that when you get the html from data you use:
[[NSString alloc] initWithData:self.responseData encoding:NSISOLatin1StringEncoding]
and not
encoding:NSUTF8StringEncoding

Related

NSArray valueForKey:#"#max.self"?

I have an Array of numbers in which I want to find the highest value.
I am doing this as follows:
NSNumber *test = [fuelConsumption valueForKey:#"#max.self"];
When I Build then Run, I am presented with the following error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSArrayM 0x7b26e8d0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key max.self.'
From what I understand, this is how one looks for this value.
Any ideas where this may be going wrong?
I am programming for iOS9.x and using Xcode 7.3.1
You are getting exception because, when you give valueForKey: an NSString, it will invoke objectForKey: and when string starts with an # it invokes [super valueForKey:], which may call valueForUndefinedKey: which may raise an exception.
So, change valueForKey: to valueForKeyPath: like,
NSNumber *test = [fuelConsumption valueForKeyPath:#"#max.self"];
Read more about kvc-collection-operators.
You need to use valueForKeyPath for that
NSNumber *test = [fuelConsumption valueForKeyPath:#"#max.self"];
For more about Collection Operators read apple documentation.

Unable to append String NSMutableString - Error

I have the following code
NSMutableString *ibmcountryCodeLabel=[[NSMutableString alloc]init];
NSMutableString *imobileNumberValue=[[NSMutableString alloc]init];
ibmcountryCodeLabel=#"555";
imobileNumberValue=#"333";
NSLog(#"Done1");
[ibmcountryCodeLabel appendString:imobileNumberValue];
NSLog(#"Done2");
Though both the string are mutable when I try to append one with another I am getting "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'"
read through all the threads. But not able to find a solution. Kindly help me. Thanks for your time
Here is the problem: this assignment produces a mutable string object:
NSMutableString *ibmcountryCodeLabel=[[NSMutableString alloc]init];
After this assignment, however, the object pointed to by ibmcountryCodeLabel is no longer mutable:
ibmcountryCodeLabel=#"555";
This is because the #"555" constant is an immutable subclass of NSString. Trying to modify it leads to runtime errors.
Change the code as follows to fix this problem:
NSMutableString *ibmcountryCodeLabel=[NSMutableString stringWithString:#"555"];
NSMutableString *imobileNumberValue=[NSMutableString stringWithString:#"333"];

AFNetworking - Invalid parameter not satisfying: url

I just tried AFNetworking on ios7 and i get this error:
/Classes/AFHTTPClient.m:227
2013-09-16 18:25:57.557 App[13531:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: url
I don't know what's going on, is there an issue with the lib an ios7 ?
thanks.
As Leon says in his comment, commenting out NSParameterAssert is not an ideal solution. An assertion has been put there for a reason by the author of AFNetworking. The code not passing the assertion is likely to be caused by an invalid URL.
Remember that the NSURL factory methods (that is, + URLWithString: and its siblings) will return nil when they're passed an invalid URL string. In this context, an invalid string is a string containing an invalid URL.
What you should do instead of commenting our the assertion, is making sure that you are not passing any invalid URL's to your AFHTTPClient instance. This Stackoverflow answers gives an example of how you could validate a URL. Here's a sample from the answer:
- (BOOL)validateUrl:(NSString *)candidate {
NSString *urlRegEx = #"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", urlRegEx];
return [urlTest evaluateWithObject:candidate];
}
Alternatively, you could add percentage escapes using NSString's stringByAddingPercentEscapesUsingEncoding method. Like this:
NSString *encoded = [notEncoded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I just had this error, the cause was a space at the start of the url:
http://host.com/etc
^
as Kasper says --> "You could add percentage escapes using NSString's stringByAddingPercentEscapesUsingEncoding method"
or better use the following since it deprecated in ios 9
path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
I had a weird case today with this assert. My problem was that I have copied url from other text editor (Sublime) and that's why url was invalid.
I was not believing until I have tested it few times.
Hope its Working.. In AFnetworing class name AFURLRequestSerialization.m
commented the line 227 : // NSParameterAssert(url);

append a NSString converted from dictionary object to NSMutableArray

I have been bothered by this problem for a long time.
I have a NSString which is the received from a RSS parser, I can successfully NSlog it on the screen, but when I try to append it to an existing NSmutablearray, it causes exception.
Here is my code.
//mystring is a NSMutableString with some content initialized succesfully
NSString *myDate = [dic objectForKey:#"date"];
NSLog(#"%# and %#",myString,myDate);
[myString appendString:myDate];
until the NSLog, both myDate and myString are printed on screen correctly as I desire, but the appendString line causes error
[_NSDate length]: unrecognized selector sent to instance 0*7141a00
Terminating app due to uncaught exception 'NSInvalidArgumentException', reasons: '-[__NSDate length]: ..........
Could someone please help me?
You're calling -appendString: with myDate, which isn't a string. It's an NSDate. You can't pass it to an API that expects a string. You need to convert it into a string somehow. This is probably best done using NSDateFormatter, which gives you full control over how to format your date as a string.
For testing purposes though, you can just replace your last line with [myString appendString:[myDate description]] and it should stop crashing.

RaptureXML iterate:usingBlock warning and crash

I'm parsing an XML file which looks like this:
<partie numero="1">
<exercice numero="1" libelle="blabla"></exercice>
<exercice numero="2" libelle="anything"></exercice>
</partie>
I'm using the Rapture XML Library, so as explained on GitHub, I do the following :
RXMLElement *rxmlPartie = [rxmlParties child:#"partie"];
NSArray *rxmlUnExercice = [rxmlPartie children:#"exercice"];
I can print correctly the "numero" attribute from partie with this line :
NSLog(#"Partie #%#",[rxmlPartie attribute:#"numero"] );
But when I try to use the iterate method on my NSArray :
[rxmlPartie iterate:#"partie.exercice" usingBlock: ^(RXMLElement *exercice) {NSLog(#"Exercice: %# (#%#)", [exercice attribute:#"libelle"], [exercice attribute:#"numero"]);}];
I get a warning, and the the application crash, saying :
-[RXMLElement iterate:usingBlock:]: unrecognized selector sent to instance 0xc67f870
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [RXMLElement iterate:usingBlock:]: unrecognized selector sent to instance 0xc67f870'
Did I forget to import something, or do I implement the method in a bad way?
The same error happens if i try to use iterateWithRootXPath...
Thanks for your help!!
So, I found out what was my problem.
I didn't know anything about paring with Rapture XML before doing this...So I sticked to the doc (as somebody told me in school :-) ). But the problem was that a method used in the doc, this "iterate usingBlock" is not defined in the framework.
Instead of using
[rxmlPartie iterate:#"partie.exercice" usingBlock: ^(RXMLElement *exercice) {NSLog(#"Exercice: %# (#%#)", [exercice attribute:#"libelle"], [exercice attribute:#"numero"]);}];
I'm using
[rxmlPartie iterate:#"exercice" with: ^(RXMLElement *exercice) {NSLog(#"Exercice: %# (#%#)", [exercice attribute:#"libelle"], [exercice attribute:#"numero"]);}];
Which is well defined within the framework!
It made my day!!

Resources