Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / Foundation / NSSortDescriptor.m
blob570eff10b0ca16f5bc3a3371914551daff4d89f9
1 /* Copyright (c) 2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 #import <Foundation/NSSortDescriptor.h>
10 #import <Foundation/NSString.h>
11 #import <Foundation/NSKeyValueCoding.h>
12 #import <Foundation/NSRaise.h>
13 #import <Foundation/NSCoder.h> 
14 #import <Foundation/NSKeyedUnarchiver.h> 
16 @implementation NSSortDescriptor
18 +sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending {
19    return [[[NSSortDescriptor allocWithZone:NULL] initWithKey:key ascending:ascending selector:NULL] autorelease];
22 +sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending selector:(SEL)selector {
23    return [[[NSSortDescriptor allocWithZone:NULL] initWithKey:key ascending:ascending selector:selector] autorelease];
26 -initWithKey:(NSString *)key ascending:(BOOL)ascending {
27    return [self initWithKey:key ascending:ascending selector:NULL];
30 -initWithKey:(NSString *)key ascending:(BOOL)ascending selector:(SEL)selector {
31    _key=[key copy];
32    _ascending=ascending;
33    
34    if(selector==NULL) // Yes it does this
35     _selector=@selector(compare:);
36    else
37    _selector=selector;
38    return self;
41 -(void)dealloc {
42    [_key release];
43    [super dealloc];
46 -copyWithZone:(NSZone *)zone {
47    return [self retain];
50 -(NSString *)key {
51    return _key;
54 -(BOOL)ascending {
55    return _ascending;
58 -(SEL)selector {
59    return _selector;
62 -(NSComparisonResult)compareObject:first toObject:second {
63    id checkFirst=[first valueForKeyPath:_key];
64    id checkSecond=[second valueForKeyPath:_key];
65    NSComparisonResult result;
67    if(_ascending)
68     result=(NSComparisonResult)[checkFirst performSelector:_selector withObject:checkSecond];
69    else
70     result=(NSComparisonResult)[checkSecond performSelector:_selector withObject:checkFirst];
71    
72    return result;
75 -reversedSortDescriptor {
76    return [[[isa alloc] initWithKey:_key ascending:!_ascending selector:_selector] autorelease];
79 - (void)encodeWithCoder:(NSCoder *)coder { 
80    if ([coder allowsKeyedCoding]) 
81    { 
82       [coder encodeObject:_key forKey:@"Key"]; 
83       [coder encodeBool:_ascending forKey:@"Ascending"]; 
84       [coder encodeObject:NSStringFromSelector(_selector) forKey: @"Selector"]; 
85    } 
86    else 
87    { 
88       [coder encodeObject:_key]; 
89       [coder encodeValueOfObjCType:@encode(BOOL) at:&_ascending]; 
90       [coder encodeObject:NSStringFromSelector(_selector)]; 
91    } 
92
94 - (id)initWithCoder:(NSCoder *)coder { 
95    if ([coder allowsKeyedCoding]) 
96    { 
97       NSKeyedUnarchiver *keyed = (NSKeyedUnarchiver *)coder; 
98       _key = [[keyed decodeObjectForKey:@"Key"] copy]; 
99       _ascending = [keyed decodeBoolForKey:@"Ascending"]; 
100       _selector = NSSelectorFromString([keyed decodeObjectForKey:@"Selector"]); 
101    } 
103    else 
104    { 
105       _key = [[coder decodeObject] copy]; 
106       [coder decodeValueOfObjCType:@encode(BOOL) at:&_ascending]; 
107       _selector = NSSelectorFromString([coder decodeObject]); 
108    } 
110    return self; 
113 @end