bittu
bittu

Reputation: 713

Swift 3: Cannot call value of non function type 'Bundle'

Currently I am working on one of my project which was in swift2 and I am converting to swift 3. I got below error:

Cannot call value of non function type 'Bundle'

at

let modelURL = Bundle.mainBundle().URLForResource("VerseApp", withExtension: "momd")!

Following is code:

lazy var managedObjectModel: NSManagedObjectModel = {
  let modelURL = Bundle.mainBundle().URLForResource("VerseApp", withExtension: "momd")!
   print(modelURL)
   return NSManagedObjectModel(contentsOfURL: modelURL)!
}()

Upvotes: 1

Views: 2379

Answers (6)

Ved Rauniyar
Ved Rauniyar

Reputation: 1589

Please try this -

let bundleUrl = Bundle.main.url(forResource: "VerseApp", withExtension: "momd")!

Upvotes: 0

Stan H
Stan H

Reputation: 206

In swift 3 the most of api was renamed

Just try this line instead of your

 Bundle.main.url(forResource: "VerseApp", withExtension: "momd")

Also take a look to this articles, it will help you to learn changes since 2 version.

https://www.raywenderlich.com/135655/whats-new-swift-3 https://www.raywenderlich.com/156352/whats-new-in-swift-3-1

Upvotes: 3

Nirav D
Nirav D

Reputation: 72460

Syntax is bit changed in Swift 3, it is main not mainBundle() and URLForResource is changed to url(forResource:withExtension:) also the init of NSManagedObjectModel is changed to init?(contentsOf:) from init?(contentsOfURL:)

lazy var managedObjectModel: NSManagedObjectModel = {
    let modelURL = Bundle.main.url(forResource: "VerseApp", withExtension: "momd")!
    print(modelURL)
    return NSManagedObjectModel(contentsOf: modelURL)!
}()

Upvotes: 3

Jogendar Choudhary
Jogendar Choudhary

Reputation: 3494

Use this code instead your:

var managedObjectModel: NSManagedObjectModel = {
            let modelURL = Bundle.main.url(forResource: "VerseApp", withExtension: "momd")!
            print(modelURL)
            return NSManagedObjectModel(contentsOf: modelURL)!
        }()

Upvotes: 0

Awesome.Apple
Awesome.Apple

Reputation: 1324

let modelURL = Bundle.mainBundle().URLForResource("VerseApp", withExtension: "momd")!

Replace above line with this

let modelURL = Bundle.main.url(forResource: "VerseApp", withExtension: "momd")

Upvotes: 2

Bilal
Bilal

Reputation: 19156

Use this

let modelURL = Bundle.main.url(forResource: "VerseApp", withExtension: "momd")!

Upvotes: 2

Related Questions