setObjectForKey: object cannot be nil crash in iphone app - ios

i am new to iphone app development and i am getting this exception at below code line,can anyone please help me to solve this?
error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: varText)'
code
[bodyDict setObject:textobj.varText forKey:TEXT_KEY];

Short and most readable method:
bodyDict [TEXT_KEY] = textobj.varText ?: #"";
?: operator checks whether the first operand is nil. It returns the first operand if it isn't nil, and the second operand if the first operand is nil.

initially you get the nil value from textobj.varText, So check the string length is empty or not
if([textobj.varText length] != 0)
[bodyDict setObject:textobj.varText forKey:TEXT_KEY];
else
[bodyDict setObject:#"" forKey:TEXT_KEY];

Related

Loop through array then compare current object to string

I'm trying to iterate through an NSArray and check if an objectAtIndex is equal to a string.
NSLog(#"%#", myArray) // 3 items. 1 of them is "a"
for (id object in myArray)
{
NSLog(#"What"); // 3 times
if ([object isEqual:#"a"]) {
NSLog(#"Hello"); // Never gets executed
}
}
The problem is, the NSLog in the if statement never gets executed?
Edit
(
(
a
),
(
01
),
(
a
),
(
03
)
)
When I set it to isEqualToString, I get this error:
2015-03-30 14:42:54.206 MyApp[1575:50954] -[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x7fe721ce3bf0
2015-03-30 14:42:54.215 MyApp[1575:50954] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x7fe721ce3bf0'
Your problem is that you have an array that contains three sub-arrays, each of which contain, what is presumably, a single string. You can tell this because of the extra ()'s around the strings in the log output, and by the fact that it's telling you that you tried to send a selector to __NSArrayM.
Here is a quick fix:
NSLog(#"%#", myArray) // 3 items. 1 of them is "a"
for (NSArray *array in myArray)
{
NSLog(#"What"); // 3 times
if ([array.firstObject isEqualToString:#"a"])
{
NSLog(#"Hello"); // Never gets executed
}
}
But as others have pointed out you probably want to use isEqualToString: since it will be more performant.
You may also want to reconsider the code that is generating this nested array structure, or the schema that you're using in general, because it seems... unnecessary. Without further information there's not much to be done.

Can not get value of a NSDictionary key, get an error: The process has been returned to the state before expression evaluation

could any body help me out of this bug? I can get the NSDictionary hiinIdPair, but when I am trying to get the value of the tmpId key of hiinIdPair, it throw me this error:
error: Execution was interrupted, reason: signal SIGABRT.
below follows the lldb information.
(lldb) po hiinIdPair["tmpId"]
iV_Starter(371,0x198ebb310) malloc: *** error for object 0x10a7ddbb0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
error: Execution was interrupted, reason: signal SIGABRT.
The process has been returned to the state before expression evaluation.
(lldb) po hiinIdPair
{
mongoId = 5521ae815730c6051476f8b1;
tmpId = "#h20150405a215200a506705";
}
the code I'm using these variables:
var hiinIdPair = idPkg["hiin"] as NSDictionary
if hiinPkg_7.tmpId == hiinIdPair["tmpId"] as String {
hiinPkg_7.hiin.id = hiinIdPair["mongoId"] as String
...
}
Be careful because "tmpId" is not a NSString in objc. You should add the # character.
Wrong:
po hiinIdPair["tmpId"]
Correct:
po hiinIdPair[#"tmpId"]
Seems like it's a bug from other part of the code. The code is terminated by bug.
Thanks for help anyway!

NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array

Well, I know the cause of the error, my question here is how to avoid this kind of error. In my case, what I want is, if the user enters a wrong id which means when the array return 0 results, is says for example :
NSLog (#"The id you entered is false !");
else, it proceeds the code normally without crashing.
Any ideas on how I can solve it ?
id array = [dataDictionary objectForKey:#"routes"];
if([array count] == 0) {
NSLog(#"don't access it as the index is out of bounds);
return;
}

Xcode call stack

I'm getting an exception while running iOS app in simulator
2013-09-16 18:03:44.346 DEV[26529:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0xf98fc70'
*** First throw call stack:
(0x17bf012 0x1c38e7e 0x184a4bd 0x17aebbc 0x17ae94e 0x1b813 0x1c4c705 0x653920 0x6538b8 0x714671 0x714bcf 0x713d38 0x917213 0x1787afe 0x1787a3d 0x17657c2 0x1764f44 0x1764e1b 0x29db7e3 0x29db668 0x65065c 0x25c9 0x2505 0x1)
UPD:
Exception is caused by the following code:
if([self.media.idtype isEqualToString:IMAGETYPE])
Where idtype is #property(retain, nonatomic) NSString *idtype; and IMAGETYPE is static NSString *VIDEOTYPE = #"2";
How can I see function names instead of addresses?
isEqualToString can compare two NSstrings not the other type of data that u are comparing which is NSDecimalNumber
convert your NSDecimalNumber to nsstring then do this
NSDecimalNumber* dec1;
NSString* str;
str = dec1.stringValue;
NSString *str2=#"world";
[str isEqualToString:str2];
There is some where in your code you are setting the value of property idtype (self.media.idtype). This must be the source of the problem:
Possible Problems could be Extracting contents from Webservice and while parsing you might be assigning it to the self.media.idtype object.

AFNetworking does not returns NSString if the value is contains only digits

I'm trying to receive a certain value in a json using AFNetworking. The value only contains digits and I want to receive it as a NSString. When I compare the received value, I get a exception (exception is also mentioned later in this post)
here is the coding producing the error.
NSArray *overallGameResultsArray = [resultDictionary valueForKey:#"overall_game_results"];
winCountDictionary = [[NSDictionary alloc] init];
for (NSDictionary *gameResultsDictionary in overallGameResultsArray)
{
NSString *userId = [gameResultsDictionary valueForKey:#"winner_user_id"];
NSString *winCount = [gameResultsDictionary valueForKey:#"win_count"];
if ([winCount isEqualToString:#"0"])
{
NSLog(#"0 wins");
}
}
I don't get the exception if the "if comparison" is commented. The win_count only consists of digits in the jSON response.
Following is the exception error I'm getting.
2012-10-10 15:41:35.086 FMB[3549:c07] -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x7230af0 2012-10-10 15:41:35.086 FMB[3549:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x7230af0'
*** First throw call stack: (0x1638012 0x1345e7e 0x16c34bd 0x1627bbc 0x162794e 0x11e03 0x20583 0x1fab4 0x245053f 0x2462014 0x24527d5 0x15deaf5 0x15ddf44 0x15dde1b 0x26997e3 0x2699668 0x28d65c 0x2512 0x2445) libc++abi.dylib: terminate called throwing an exception
i think you'll have to do :
NSString *winCount=[NSString stringWithFormat:#"%d",[[gameResultsDictionary valueForKey:#"win_count"]intValue];
With this, you get the number value into the winCount NSString, and then you can use isEqualToString method
I suppose you use JSON. Then the type of the object is determined by JSON encoding. If there are " around, it's a string. If it's a number and you want string there, change how the data is encoded on the server.
However, in this context, using a number makes more sense. You should change the implementation in the following way.
NSNumber* winCount = [gameResultsDictionary valueForKey:#"win_count"];
if ([winCount intValue] == 0) { //this would actually work for both NSNumber and NSString
NSLog(#"0 wins");
}
Comparing numbers using string comparison is always a hint that you are doing something wrong.
The exception is thrown because you expect an object to be an NSString but actually is an NSNumber, so you try to call isEqualToString: on the NSNumber which doesn't implement the method.
Since you know that winCount is always a number and apparently the JSON deserializer that AFNetworking uses box the numbers in the response into NSNumber objects, you could easily grab a string out of the object like this:
NSString *winCount = [[gameResultsDictionary valueForKey:#"win_count"] stringValue];
Now you have a string representation of your number and you can therefor perform isEqualToString:
Also note that a more elegant solution might be to receive your objects as ids, find their class and do a comparison based on the class determined.

Resources