append a NSString converted from dictionary object to NSMutableArray - ios

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.

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

How to resolve a NULL cString crash

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

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!!

JSON: NSString stringWithContentsOfURL... with null in stream, crashes

Environment: iOS 4.3+ using Xcode 4.3+
I'm always getting an iOS/Xcode crash when I implement the following string that returns data with nulls in it:
[NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
NSDictionary *myResult = (NSDictionary *)[myString JSONValue];
The result:
-[NSNull isEqualToString:]: unrecognized selector sent to instance 0x17315e8
When the data stream contains no nulls (mostly), it works fine.
Is this a known problem?
Any remedy?
You can also fix the data coming in from the source. In almost all of my web service interaction, I use COALESCE on queries and SP's to make sure that the data coming out is always consistent.

Resources