//list has type of NSMutableArray
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"date" ascending:YES] autorelease];
[list sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
I got this exception:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDate 0x5d57980> valueForUndefinedKey:]: this class is not key value coding-compliant for the key date.'
Any idea? How to fix it?
An NSDate does not have the -date method, so your NSSortDescriptor won't work.
Why not just use
[list sortUsingSelector:#selector(compare:)];
?
Related
I am sorting my NSSet using :
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
[[testSet.questions allObjects] sortedArrayUsingDescriptors:descriptor]; // warning
But it causes warning :
Incompatible pointer types sending 'NSSortDescriptor *__strong' to parameter of type 'NSArray *
You're passing a single descriptor where an array is expected, try this:
[testSet.questions allObjects] sortedArrayUsingDescriptors:#[descriptor]];
You should see this method. It requires you to pass an Array object to it, not a NSSortDescriptor object.
[sortDescriptions sortedArrayUsingDescriptors:<#(NSArray *)#>]
so you have to create an array and put your NSSortDescriptor object in it. Try this
NSArray *_sortArray = [[NSArray alloc] initWithObjects:descriptor, nil];
[testSet.questions allObjects] sortedArrayUsingDescriptors:_sortArray];
or
[testSet.questions allObjects] sortedArrayUsingDescriptors:#[descriptor]];
see if it could help:)
NSSortDescriptor should be passed in an array. So you have to create an array then add the descriptor in the array. Now you should pass the array instead of NSSortDescriptor:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
NSArray *descriptorArray = [NSArray arrayWithObject:descriptor]
[[testSet.questions allObjects] sortedArrayUsingDescriptors:descriptorArray];
Try this :
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
[[testSet.questions allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
Hope this will help.
I have declared NSDictionary *dict; globally and trying to add this dictionary into array but getting an Error :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
My Code :
dict = #{#"Testcase" : Testcase, #"TestResult" : TestResult,#"Message": Message};
NSMutableArray * myArray = [[NSMutableArray alloc] init];
[myArray insertObject:dict atIndex:TestcaseId];
How to solve this Error ?
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '* -[__NSArrayM
insertObject:atIndex:]: object cannot be nil'
Problem : From the above ErrorMessage, it is clear that you are trying to add NULL value in your NSDictionary.
Explanation : At the time of inserting Value to your NSDictionary , note that the Value shouldn't be NULL and Non-String. You are trying to add Value of Variable to your Dictionary directly. Instead you should try to add like this :
Answer :
[NSString stringWithFormat:#"%#",Testcase];
Benefits :
It will add String-Value.
It will add Empty-Value when your Value of Variable is NULL.
dict = [NSDictionary dictionaryWithObjectsAndKeys:Testcase:#"Testcase", TestResult: #"TestResult",Message, #"Message", nil];
NSMutableArray * myArray = [[NSMutableArray alloc] init];
[myArray insertObject:dict atIndex:TestcaseId];
Make sure TestcaseId is not more than your array count
[myArray addobject:dict];
or
[myArray replaceObjectAtIndex:TestcaseId withObject:dict]
Please give me suggestion, How can I solve solve this problem..
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"Weight" ascending:NO selector:#selector(localizedStandardCompare:)];
NSArray *sortedArray = [arrayToSort sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
I am getting output :
-[__NSCFNumber length]: unrecognized selector sent to instance 0x6a81cf0
2012-05-16 09:54:21.480 MedzioSearch[2188:f803] *** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSInvalidArgumentException> -[__NSCFNumber length]: unrecognized selector sent to instance 0x6a81cf0
What kind of objects are in arrayToSort? What is the type of their "Weight" property?
At a guess, some of the objects have a Weight property which is an NSString while some have a Weight property which is an NSNumber. As a consequence, the sort is trying to do something like [someString localizedStandardCompare:someNumber]. Internal to -[NSString localizedStandardCompare:], it's invoking -length on the argument which is an NSNumber and doesn't recognize that selector.
By the way, property names should begin with a lowercase letter unless they begin with an acronym or initialism (like "URL" or "TIFF"). So, your property should be named "weight", not "Weight".
Getting the following error when trying to set value/key pairs:
2011-06-21 16:21:16.727 agent[94408:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<SBJsonWriter 0xab31230> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key key.'
Here is the code:
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
[writer setValue:#"val" forKey:#"key"];
NSString *json = [writer JSONRepresentation];
NSLog([NSString stringWithFormat:#"json: #%", json]);
that is using the key-value coding from the (NSObject) category,
to use the dictionary interface:
import JSON.h then:
NSMutableDictionary * dict = [NSMutableDictionary new];
[dict setValue:#"val" forKey:#"key"];
NSLog(#"json: %#", [dict JSONRepresentation]);
p.s. NSLog accepts a format, so you don't need to make a string from a format to pass to it.
I am sorting an mutable array. For sorting I use:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"pubDate" ascending:NO];
[recent sortUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
recent1 = [recent sortedArrayUsingDescriptors:descriptor];
[descriptor release];
I am getting this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
The line
recent1 = [recent sortedArrayUsingDescriptors:descriptor];
show warnings
"passing argument 1 of 'sortedarrayusingdescritors' from distinct objective c type " and
"assignment from distinct objective c type"
In my code, both recent and recent1 are NSMutable arrays. Where do I go wrong?
recent1 = [recent sortedArrayUsingDescriptors:descriptor];
must be:
recent1 = [recent sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
Though I have no idea why you would want to sort an array that you already sorted with the same sort descriptors on the line directly above.