Array cannot be formed from single element in NSMutableDictionary -[__NSCFString count]: - objective-c

I'm setting objects to my NSMutableDictionary like this
if ([detail isEqualToString:[check objectAtIndex:1]]) {//check is NSArray,getdict is NSMutableDictionary,keyverify is NSMutableArray
if ([getdict objectForKey:detail]==nil) {
[getdict setObject:[check objectAtIndex:2] forKey:detail];
[keyverify addObject:[check objectAtIndex:2]];
}
else{
[keyverify addObject:[check objectAtIndex:2]];
[getdict setObject:keyverify forKey:detail];
}
}
NSArray * passarray=[getdict valueForKey:detail];
when my NSMutableDictionary value is like this
dict:{
"Sample" = (
"Test1",
Test2,
Test3
);
}
i can get an array from NSMutableDictionary and displayed in UITableView but when i get my NSMutableDictionary value like this
dict:{
"Sample" = Test1;
}
my app crashes with an log
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x6eab4c0'
Kindly help me pls..Thanks..

In your second test case the key "Sample" is not an array that is why you get that error.
You can check this by using isKindOfClass : [NSArray class], if it passes the condition then it is an array and you can do the array methods you want to if it is not then get the key "Sample" as a string.

Related

How to set the value of UILabel in iphone?

I'm doing this to set the value in UILabel:
NSMutableDictionary *responseDict = [responseString objectFromJSONString];
self.pkrLbl.text= [responseDict objectForKey:#"amount"];
and I'm getting this error:
[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9440310
2013-05-16 15:23:34.281 EasyLoadPakistan[20341:19a03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9440310'
Please help me how can I set value in UIlabel
A UILabel expects a string (specifically NSString or one of its subclasses), and you are passing it a number (an NSNumber instance).
Convert the number to a string first, then set the label to that string:
NSNumber *n = [responseDict objectForKey:#"amount"];
NSString *stringVersion = [n stringValue];
self.pkrLbl.text = stringVersion;
If you prefer to have it all on one line, you can omit the variables and just chain the stringValue call. The approach shown above tends to facilitate debugging, though (you can for instance set a breakpoint and have a look at your variables).

Why does this code break at removeObject:?

+ (NSString *)numberMatching: (NSString *)number and: (NSString *)secondNumber
{
NSString *returnNumber;
if ([number isEqualToString:secondNumber]) {
returnNumber = number;
} else {
NSMutableArray *validNumber = [[self validNumbers] copy];
[validNumber removeObject:number];
[validNumber removeObject:secondNumber];
returnNumber = validNumber[0];
}
return returnNumber;
}
In case it matters, here is the code for validNumber:
+ (NSArray *)validNumbers
{
static NSArray *validNumbers = nil;
if (!validNumbers) validNumbers = #[#"one",#"two",#"three"];
return validNumbers;
}
When run, I get the following error:
-[_NSArrayI removeObject:]: unrecognized selector sent to instance 0x1ed60f00 ... Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[_NSArrayI removeObject:]:
unrecognized selector sent to instance 0x1ed60f00'
That would seem to indicate that removeObject is not a valid method for an NSMutableArray, but it is valid.
Essentially, what I want to do is this: if the two arguments match, I want to return the value. If they don't match, I want to return the third possibility (doesn't match either argument).
Because validNumber is an NSArray. You can't remove (or add) objects from an NSArray. Use NSMutableArray and mutableCopy instead:
NSMutableArray *validNumber = [[self validNumbers] mutableCopy];
[validNumber removeObject:number];
[validNumber removeObject:secondNumber];
When you send the copy message to an NSArray, the copy you get is also an NSArray instance and you can't remove objects from an immutable array. In order to get a mutable copy you need to send the mutableCopy message:
NSMutableArray *validNumber = [[self validNumbers] mutableCopy];

nsdictionary issue

I have the following data in a NSDictionary Variable:
{
value = "TV-PG";
}
I was wondering how to get the value for key "value" here.
I tried:
NSDictionary *fieldMaturityRating = [[parsedItems objectAtIndex:0] objectForKey:#"field_maturity_rating"];
NSString *dateRelease = [fieldMaturityRating objectForKey:#"value"];
(where, fieldMaturityRating is a NSDictionary with the given value)
and I get:
-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0xd9cd3f0
[10530:707] *** Terminating app due to uncaught exception: -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0xd9cd3f0
Can anyone kindly help me ?
Thanks.
Note: if I pause the execution and do a po after the 1st line of code presented here, I get the following:
(gdb) po fieldMaturityRatingNew
<__NSArrayM 0x79af250>(
{
value = "TV-PG";
}
)
The po actually shows your issue:
(gdb) po fieldMaturityRatingNew
<__NSArrayM 0x79af250>(
{
value = "TV-PG";
}
)
The outer ( and ) mean that your object is actually an array.
Inside that is where the { and } denote your dictionary.
So you really want:
NSString *value = [[fieldMaturityRatingNew objectAtIndex:0] objectForKey:#"value"];
You're actually sending that NSDictionary message to a NSMutableArray instance.
You might want to check your code again as the objectForKey: method is right when pointing to a NSDictionary.
This means your fieldMaturityRating is not actually an NSDictionary. Make sure you aren't setting it to an array somewhere in your code.
Edit:
This means your fieldMaturityRating is actually an NSArray containing an NSDictionary. If this is your intended data structure then you can access your value like so.
NSString *dateRelease = [[fieldMaturityRating objectAtIndex:0] objectForKey:#"value"];
I don't believe this is your intended data structure so you should look into why your parsedItems array returned you an NSArray instead of an NSDictionary. If you track this problem down you can stop any headaches in the future.
Based on your datastructure which is a dictionary inside an array, dateRelease should be like this
NSString *dateRelease = fieldMaturityRating[0][#"value"];

Why am I getting this: _cfurl: unrecognized selector

My init starts like this:
- (id) init {
[super init];
sounds = makeDictFromArrayOfURLs(getNoiseFileURLs());
[sounds retain];
NSURL *theFirstNoise = [[sounds allKeys] objectAtIndex:0];
CFURLRef uref = (CFURLRef)theFirstNoise;
OSStatus ret = AudioServicesCreateSystemSoundID(uref, &chosenNoise);
When we get to that last line, it throws this:
2011-06-09 23:19:18.744 SuperTimer[94516:207] -[NSPathStore2 _cfurl]: unrecognized selector sent to instance 0x940cfb0
2011-06-09 23:19:18.746 SuperTimer[94516:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSPathStore2 _cfurl]: unrecognized selector sent to instance 0x940cfb0'
Yeah, it's a bit uncompact for debugging.
Just before I get the dump, theFirstNoise contains the expected (sort of) data. (It's description method prints a weird form, but I am informed that's normal.)
Off the top of my head, it looks like theFirstNoise is actually an NSPathStore2 (a private subclass of NSString) instead of an NSURL.
Edit: NSPathStore2 objects will contain file paths. If you need to turn these into NSURLs, you can simply pass them to +[NSURL fileURLWithPath:].
This line:
NSURL *theFirstNoise = [[sounds allKeys] objectAtIndex:0];
is the problem: [sounds allKeys] returns an NSArray of keys, and objectAtIndex: therefore is returning an NSString, and not the URL. I wish the compiler would have been a little more helpful.

Trouble with initializing NSMutableArray in my Singleton

I am getting a weird error, and I can't figure it out. This takes place inside of a class that is created with the singleton pattern:
- (NSMutableArray *) getCurrentClasses
{
NSMutableArray *current_classes = [[NSMutableArray init] alloc];
NSLog([NSString stringWithFormat:#"%d", [current_classes count]]);
...
}
When I run this, even though I literally just initialized current_classes, it gives me this error in log:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray count]: method sent to an uninitialized mutable array object'
Does anyone know what this is happening? I initialized it literally last line.
Thanks
You mixed up the alloc/init calls. alloc comes first. It should be:
NSMutableArray *current_classes = [[NSMutableArray alloc] init];