简体   繁体   中英

Restkit and deletion of orphaned objects in swift 3

After the last update of XCode it suggested me to update my code to the swift 3 language. XCode helped me with the update and after I fixed some things that needed to be manually updated, I've run into one conversion that I can't figure out how to do.

It's about the deletion of orphaned objects in Restkit. Here's the generated code by XCode for the update of swift 2 to swift 3:

        // fetchRequestBlock to delete orphaned objects ------ tmenus --------
    manager?.addFetchRequest { (url:URL!) -> NSFetchRequest<AnyObject>! in
        let pathMatcher = RKPathMatcher(pattern: config.getDataPath)
        let match = pathMatcher.matchesPath(url.relativePath, tokenizeQueryStrings: false, parsedArguments: nil)
        guard match else {
            return nil
        }
        let fetchRequest = NSFetchRequest(entityName: "TipoMenuEntity")
        return fetchRequest
    }

And the error it shows :

/Users/mast/work/gColegios/iOSapp/gColegios/AppDelegate.swift:417:34: Cannot convert value of type '(URL!) -> <<error type>>' to expected argument type '((URL?) -> NSFetchRequest<NSFetchRequestResult>?)!'

So I have updated the code with the new method of managing the requests in swift 3:

// fetchRequestBlock to delete orphaned objects ------ tmenus --------
    manager?.addFetchRequest { (url:URL!) -> NSFetchRequest<TipoMenu>! in
        let pathMatcher = RKPathMatcher(pattern: config.getDataPath)
        let match = pathMatcher.matchesPath(url.relativePath, tokenizeQueryStrings: false, parsedArguments: nil)
        guard match else {
            return nil
        }
        let fetchRequest: NSFetchRequest<TipoMenu> = TipoMenu.fetchRequest()
        return fetchRequest
    }

And now it shows the new error :

/Users/mast/work/gColegios/iOSapp/gColegios/AppDelegate.swift:418:34: Cannot convert value of type '(URL!) -> NSFetchRequest<TipoMenu>!' to expected argument type '((URL?) -> NSFetchRequest<NSFetchRequestResult>?)!'

I have tried everything: changing '(URL!) -> NSFetchRequest!' by '((URL?) -> NSFetchRequest?)!' , and all kind of combinations between ? and !

Anyone can help me solve this issue?

Thanks in advance

Well, it turned out that the correct code was

       // fetchRequestBlock to delete orphaned objects ------ tmenus --------
    manager?.addFetchRequest { (url:URL?) -> NSFetchRequest<NSFetchRequestResult>? in
        let pathMatcher = RKPathMatcher(pattern: config.getDataPath)
        if let match = pathMatcher?.matchesPath(url?.relativePath, tokenizeQueryStrings: false, parsedArguments: nil)
        {
            if (match)
            {
                let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "TipoMenuEntity")
                return fetchRequest
            }
        }
        return nil
    }

What confused me was that in other requests, it was necessary to put the NSFetchRequest for a request to be created, since apparently with swift 3 the NSManagedObject class extends the code to comply the delegate of NSFetchRequest.

However, here it was just the header of a function that XCode had completed with to adapt it to swift 3. So it was just putting the correct header and adapt the code to the new optionals for this version.

Thanks New16 for leading me to this.

The error basically says it is expecting NSFetchRequestResult . It is result for example NSAsynchronousFetchResult . You are using request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM