Does Nssortdescriptor convert contents of the array to integer after sorting?

情到浓时终转凉″ 提交于 2020-01-16 09:08:36

问题


Given the below code, in the first iteration the logs are displayed normally. For the second iteration the app crashes with the following error message:

019-11-07 09:54:08.156277+0100 xxxion[520:202084] iconNumberString : 1026
2019-11-07 09:54:12.160849+0100 xxxtion[520:202084] [self.iconNumbers lastObject]) : 1026
2019-11-07 09:54:24.598805+0100 xxxion[520:202084] -[__NSSingleObjectArrayI addObject:]: unrecognized selector sent to instance 0x2824a3fd0
(lldb) 

The nssortdescriptor used:

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"intValue" ascending:YES];

To solve this issue: 1-as I thought that the nssordescriptor converts the contents of the array to integer as the specified key specifies in the nssortdescriptor, the following log added: NSLog(@"[self.iconNumbers lastObject]) : %i", [self.iconNumbers lastObject]); to check if the values were converted to integer or not

the question is: 1-does nssortdescriptopn converts the contents of the NSArray to integer as the nssordescriptor specifies -initWithKey:@"intValue"-?

2-for this line_

 self.iconNumbers = (NSMutableArray *)[self.iconNumbers    
 sortedArrayUsingDescriptors:@[sortDescriptor]];

why the changes should not have been applied directly on -self.iconnumbers-?? why I have to assign the results of the RightHandSide to the same -self.iconnumber- again??

code:

For loop 
 [self.iconNumbers addObject:iconNumberString];
         self.iconNumbers = (NSMutableArray *)[self.iconNumbers sortedArrayUsingDescriptors:@[sortDescriptor]];
        NSLog(@"iconNumberString : %@", iconNumberString);
        NSLog(@"[self.iconNumbers lastObject]) : %@", [self.iconNumbers lastObject]);

        NSLog(@"[self.iconNumbers lastObject]) : %i", [self.iconNumbers lastObject]);//added for testing after received the crash
End of loop

回答1:


sortedArrayUsingDescriptors: returns a NSArray. It's not applying on itself, it's applying on a "copy" and returning the sorted copy.

Casting, ie doing (NSMutableArray *)something isn't magic. It won't transform the NSArray into a NSMutableArray. It just tells the compiler, don't be afraid, I'm telling you it's a NSMutableArray, treat it as such for the next time, trust me (and you're wrong).

Either, use sortUsingDescriptors: directly, or replace (NSMutableArray *) with [[NSMutableArray alloc] initWithArray:

For loop 
    [self.iconNumbers addObject:iconNumberString];
    self.iconNumbers = [[NSMutableArray alloc] initWithArray:[self.iconNumbers sortedArrayUsingDescriptors:@[sortDescriptor]]];
End of loop

or

For loop 
    [self.iconNumbers addObject:iconNumberString];
    [self.iconNumbers sortUsingDescriptors:@[sortDescriptor]];
End of loop

For the rest:

It will use the sortDescriptor as a "conversion" for the sort, but it won't replace the value with the conversion. Imagine, you have an array of NSDictionary looking like this: @{@"key1": @"Hello", @"key2": @"Hi"}, setting the descriptor won't replace each dictionary with the value of key1 (Hello).

Finally, you can only have a NSObject in a NS(Mutable)Array, no primitive. It'd be then, at the max, a NSNumber encapsulating a Int.



来源:https://stackoverflow.com/questions/58745408/does-nssortdescriptor-convert-contents-of-the-array-to-integer-after-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!