0

I need Help. While conversion from Swift 2.3 -> 3.2 I received below error. I'm not able to resolve this error.

Below is my coding stuff, where I'm facing some issues.

Error1 : Cannot convert value of type String to specified type NSManagedObjectContext**

Error2 : Cannot convert return expression of type URL to return type URL.

 class func persistentFileURL(_ name: String, enclosingDirectoryName: String) -> Foundation.URL {
        let directoryURL = self.directoryForPersistentStorage(enclosingDirectoryName)
        let urlPath = directoryURL.path
        let filePath: NSManagedObjectContext = (urlPath as NSString).appendingPathComponent(name) //Error1 : Cannot convert value of type String to specified type NSManagedObjectContext 
        
        return URL(context: filePath) // Error2 : Cannot convert return expression of type URL to return type URL.
    }

Note : URL is separate Class declared to handle this : URL_Class

Please help me. I'm very new to iOS. Not able to understand this type of error.

Community
  • 1
  • 1

2 Answers2

2

let filePath: NSManagedObjectContext = (urlPath as NSString).appendingPathComponent(name)

should read

let filePath: String = (urlPath as NSString).appendingPathComponent(name)

lostInTransit
  • 70,519
  • 61
  • 198
  • 274
0

Error 2:

URL doesn't have any constructor using context:. Try to use init(fileURLWithPath:) instead (which expects a string, so you need to make filePath an instance of string instead of an NSManagedObject).

See official docs on URL from Apple here.

EDIT

Seeing as you are returning a custom URL object (subclass of NSManagedObject), you need to change the return type of your function.

From -> Foundation.URL to -> URL. I'd suggest to rename your custom URL subclass to something else, since this name is already used by Apple and will probably cause some namespace issues (compiler will get confused and you will get errors).

oyvindhauge
  • 3,496
  • 2
  • 29
  • 45
  • Please check this one I've this separate URL class : https://i.stack.imgur.com/0VUYa.png –  Jun 14 '18 at 12:13
  • But your function is returning an instance of Foundation.URL and not an instance of your custom URL class. Seems like you need to change the return type of your function then. @Vijay – oyvindhauge Jun 14 '18 at 12:14
  • No but there extension created for this so it might not give namespace error. –  Jun 14 '18 at 12:23
  • extension URL { NSManaged var baseURLArray: NSObject? NSManaged var selectedbaseURL: String? NSManaged var backendURLArray: NSObject? NSManaged var selectedbackendURL: String? NSManaged var authorizationKey: NSObject? NSManaged var selectedAuthorizationKey: String? } –  Jun 14 '18 at 12:24