[swift-users] Making functions generic on optionals

Jordan Rose jordan_rose at apple.com
Fri Feb 5 16:34:55 CST 2016


The smallest way to write this is at the call site:

sortStringOpt.map { NSSortDescriptor.sortDescriptorsFromString($0) }

But really I'm a little curious about why you're thinking about "every time [you] want to do this". Does this really come up that often? In this particular case, why would you ever not have a sort string?

Jordan


> On Feb 5, 2016, at 13:53, Kenny Leung via swift-users <swift-users at swift.org> wrote:
> 
> Hi All.
> 
> In the code below, I have the method
> 
> public class func sortDescriptorsFromString(sortString :String) throws -> [NSSortDescriptor]
> 
> In order to make life easier, I would like to make sortString optional, but then I would have to make the return type optional to to be able to return nil if the argument is nil. To get around this, I’ve added a stub method
> 
> public class func sortDescriptorsFromString(sortString :String?) throws -> [NSSortDescriptor]?
> 
> that declares the argument to be optional and the return type to be optional.
> 
> I get the feeling that there is a way to not have to write a stub method every time I want to do this. I also get the feeling that I should be able to accomplish it through generics. But I do not know how to write the declaration for such a method. Can anyone help?
> 
> Thanks!
> 
> -Kenny
> 
> 
> extension NSSortDescriptor {
> 
>     public class func sortDescriptorsFromString(sortString :String) throws -> [NSSortDescriptor] {
>         var descriptors = [NSSortDescriptor]()
>         let components = sortString.split("[, ]+")
> 
>         for i in 0.stride(to: components.count, by: 2) {
>             let key = components[i]
>             let direction = components[i + 1]
>             var descriptor :NSSortDescriptor?
> 
>             if SORT_STRINGS_ASCENDING.contains(direction) {
>                 descriptor = NSSortDescriptor(key: key, ascending: true)
>             } else if SORT_STRINGS_DESCENDING.contains(direction) {
>                 descriptor = NSSortDescriptor(key: key, ascending: false)
>             } else if SORT_STRINGS_CASEINSENSITIVE_ASCENDING.contains(direction) {
>                 descriptor = NSSortDescriptor(key: key, ascending: true, selector: "caseInsensitiveCompare:");
>             } else if SORT_STRINGS_CASEINSENSITIVE_DESCENDING.contains(direction) {
>                 descriptor = NSSortDescriptor(key: key, ascending: false, selector: "caseInsensitiveCompare:");
>             } else if SORT_DATE_ASCENDING.contains(direction) {
>                 descriptor = NSSortDescriptor(key: key, ascending: true, selector: "compare:");
>             } else if SORT_DATE_DESCENDING.contains(direction) {
>                 descriptor = NSSortDescriptor(key: key, ascending: false, selector: "compare:");
>             }
> 
>             if let nnDescriptor = descriptor {
>                 descriptors.append(nnDescriptor)
>             } else {
>                 throw NSSortDescriptorError.UnsupportedSortDirection
>             }
>         }
> 
>         return descriptors
>     }
> 
>     public class func sortDescriptorsFromString(sortString :String?) throws -> [NSSortDescriptor]? {
>         guard let sortString = sortString else {return nil}
>         return try self.sortDescriptorsFromString(sortString)
>     }
> 
> }
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160205/a5b177d8/attachment.html>


More information about the swift-users mailing list