wvteijlingen / Spine

A Swift library for working with JSON:API APIs. It supports mapping to custom model classes, fetching, advanced querying, linking and persisting.
MIT License
264 stars 109 forks source link

Error: Expressions are not allowed at the top level #174

Open dmoss18 opened 7 years ago

dmoss18 commented 7 years ago

I'm following your example resource class in the README:

class User : Resource {
    [omitted]
}

spine.registerResource(User.self)

but when I build I get the error "Expressions are not allowed at the top level" on the registerResource line.

So where is the best place to register resources?

Also, where is the best place to instantiate Spine? Do you recommend a static sharedInstance property on a service class? Or the AppDelegate?

lovesuper commented 7 years ago

You can register your resources in AppDelegate.swift.


import UIKit
import Spine

let serializer = Serializer()

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    serializer.registerResource(MessageModel.self)
    serializer.registerResource(UserModel.self)

    return true
  }

  func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    return true
  }
}