[swift-users] Making functions generic on optionals

Kenny Leung kenny_leung at pobox.com
Fri Feb 5 15:53:19 CST 2016


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)
    }

}

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


More information about the swift-users mailing list