Error trying to separate an array containing managed object context - objective-c

Could anyone please tell me why I am getting this error and why this code isn't working?
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSArrayI componentsSeparatedByString:]: unrecognized
selector sent to instance 0x109494750'
This is the code with problems:
NSArray *array = [self.managedObjectContext executeFetchRequest:request error:nil];
NSString *dateString = [array valueForKey:#"dateString"];
NSArray *datesArray = [dateString componentsSeparatedByString:#","];//line with problems

When you call valueForKey: on array the result will be an NSArray containing the result of calling valueForKey: on each of it's elements.
So dateString is not actually a string it's an instance of NSArray, which does not respond to componentsSeparatedByString:. You need to index into the array to get the date you want before calling componentsSeparatedByString: on that

It's an indication that dateString isn't a string. When you call -valueForKey: on an array, it returns an array. Per the docs:
Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.
So you're calling a string method on an array. It's not clear what you're trying to accomplish by calling -valueForKey:. Perhaps you meant -objectAtIndex:?

Related

How to get string from NSArray which contains bracket

my code is like this:
(here name is NSArray and object is also array which is passed as function parameter)
[name addObject:object];
NSlog(#"%#",name);
When i do this then i got output like this:
{name:\"Malay Basu"\ date:\"2013-04-18"\ senderId:\"24" receiverId:\"25"}
Now when i am retrieving name field of array into string then i got the string as:
NSString *temp = [name valueForKey:#"name"];
output: `( Malay Basu )`
Now when i do string operations like string appending or comparison of string then i got the error like:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x16635ed0'
To remove this error i need this string without bracket as displays above. So i need Malay Basu instead of ( Malay Basu ).So what is my next step to fetch proper string into another string variable because i need to do operations like string compare,string appending. Any help will be appreciated.
Check the purpose of the methods you are using.
-valueForKey: when called on an NSArray
Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.
So your object isn't a string, it's an array of strings.
The log statement shows brackets because that is the log notation for NSArray. Similarly {} brackets are the log notation for NSDictionary.
To process the result strings, use a loop.
for (NSString *aString in anArray) {
NSLog(#"%# is an %#", aString, NSStringFromClass([aString class]);
}

unrecognized selector sent to instance when trying to retrieve string value

I have a NSMutablearray wich contain NSArrays(each array contain int and String values).
When i try to retrieve and display the data from the first array:
That was ok with the int value, it was displayed correctly.
NSLog(#"%i",[[[lesQuestions objectAtIndex:0] objectAtIndex:0] intValue]);
But when i try to display the String value:
NSLog(#"%#",[[[lesQuestions objectAtIndex:0] objectAtIndex:1] stringValue]);
I got exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString stringValue]: unrecognized selector sent to instance
I am definitely sure that the int value is the first item (index 0) and the String value is the second (index 1).
When i log the MutableArray which holds the NSArrays, i got the values correctly, so the problem is on the reference of the item i guess.
The object in your array is already an NSString, so the call to stringValue is unnecessary. NSString doesn't implement a method called stringValue, hence the exception you're seeing. Just do this:
NSLog(#"%#",[[lesQuestions objectAtIndex:0] objectAtIndex:1]);
-intValue is a method implemented by NSNumber, to get an integer primitive out of the NSNumber instance, and your use of intValue is correct assuming the first object in the array is an NSNumber (or an NSString, which also implements -intValue).
All that said, I don't generally think it's a great idea to store instances of different classes in the same array like you're doing. You'd probably be better off using an NSDictionary where each value is stored with a unique key, say #"index" for the number and #"name" for the string.
NSLog respectively the ability of standard objects do display themselfs is more powerful than you may think.
NSLog(#"%#",[[lesQuestions objectAtIndex:0] objectAtIndex:0]);
NSLog(#"%#",[[lesQuestions objectAtIndex:0] objectAtIndex:1]);
You could even find this useful unless you have a really high number of array elements:
NSLog(#"%#",[lesQuestions objectAtIndex:0]);
or
NSLog(#"%#",lesQuestions);
Give it a try!

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

Error when calling allKeys on NSDictionary containg JSON objects

I am working through Apple's sample code for displaying a Twitter feed from here http://bit.ly/x4nhG5
From the example, the code is taking the JSON and putting it into an NSDictionary:
NSDictionary *publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
What I was just trying to do at this point was look at all the keys that came back by doing this:
NSArray *allKeysArray = [publicTimeline allKeys];
I then receive an error while trying to run the program:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFArray allKeys]:
unrecognized selector sent to instance 0x6a950f0'
Any reason why this NSDictionary that is loaded from a JSON is behaving this way?
Thanks,
Flea
If you look at the exception you see '-[__NSCFArray allKeys], this indicates that you actually have an __NSCFArray which is a private subclass of NSArray. This is not an NSDictionary, which is why you get the exception.
If you look at the JSON feed it is of the form
[
{
coordinates: null,
truncated: false,
// ...
},
{
coordinates: null,
truncated: false,
// ...
}
]
In JSON [] represents an array and {} represents an object.
An [JSON] array is an ordered collection of values
This means a JSON array can easily be mapped to an NSArray.
An [JSON] object is an unordered set of name/value pairs
This means a JSON Object can be mapped to an NSDictionary.
So looking at the feed we can see that we actually have an array of objects. Which NSJSONSerialization will turn into an NSArray of NSDictionary's. Therefore to get to a dictionary we first need to access it from the array first resulting in:
NSDictionary *tweet = [publicTimeline objectAtIndex:0];
NSArray *allKeys = [tweet allKeys];
What the error tells you is that your publicTimeline is not NSDictionary but NSArray.
So, my guess is that instead of
NSArray *allKeysArray = [publicTimeline allKeys];
this would work as you intend.
NSArray *allKeysArray = [[publicTimeline objectAtIndex:0] allKeys];

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.