-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bundle moved to model #26
Conversation
BundleURL.swift
Outdated
@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *) | ||
class var BundleURL: URL{ | ||
/// URL of model assuming it was installed in the same bundle as this class | ||
let documentDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still a !
BundleURL.swift
Outdated
import CoreML | ||
|
||
@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *) | ||
class var BundleURL: URL{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make this a struct and have a static method called bundleUrl(modelName: String) -> URL?
or something like that
BundleURL.swift
Outdated
let modelcFile = documentDirectory.appendingPathComponent("FindFour.mlmodelc") | ||
|
||
if !FileManager.default.fileExists(atPath: modelcFile.path) { | ||
if let bundleUrl = Bundle(for: FindFour.self).url(forResource: "CardScan", withExtension: "bundle"){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest using
guard if let bundleUrl = Bundle(for: ...) else {
print("could not get bundle url")
return nil
}
guard if let bundle = Bundle(url: bundleUrl) else {
return nil
}
and so on
BundleURL.swift
Outdated
if !FileManager.default.fileExists(atPath: modelcFile.path) { | ||
if let bundleUrl = Bundle(for: FindFour.self).url(forResource: "CardScan", withExtension: "bundle"){ | ||
if let bundle = Bundle(url: bundleUrl){ | ||
if let modelUrl = bundle.url(forResource: "FindFour", withExtension: "bin"){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing here with guard, we should try to avoid the if waterfall
BundleURL.swift
Outdated
@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *) | ||
//model name : "FindFour.mlmodelc" | ||
struct BundleURL{ | ||
static func bundleUrl(modelName: String) -> URL?{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space between ?{
BundleURL.swift
Outdated
//model name : "FindFour.mlmodelc" | ||
struct BundleURL{ | ||
static func bundleUrl(modelName: String) -> URL?{ | ||
var modelcFile: URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this a var? Can it be a let
BundleURL.swift
Outdated
struct BundleURL{ | ||
static func bundleUrl(modelName: String) -> URL?{ | ||
var modelcFile: URL | ||
if let documentDirectory = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use a guard
here also since we're always going to need this directory and if we can get it we can't compile
BundleURL.swift
Outdated
|
||
let modelUrl = bundle.url(forResource: "FindFour", withExtension: "bin") | ||
|
||
if let compiledUrl = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use a guard
here or return nil
on the else path because the only way we will return a non null is if everything in this function works
@@ -99,7 +84,7 @@ class FindFour { | |||
|
|||
/// Construct a model that automatically loads the model from the app's bundle | |||
convenience init() { | |||
try! self.init(contentsOf: type(of:self).urlOfModelInThisBundle) | |||
try! self.init(contentsOf: BundleURL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this still? We can just instantiate the class directly using the url we get from your static method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor comment, but #shipit 🚢
try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false) { | ||
modelcFile = documentDirectory.appendingPathComponent(modelName) | ||
} | ||
static func bundleUrl(forResource: String, withExtension: String, modelName: String) -> URL?{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comment, but I'd make forResource
and withExtension
the public variable names with something like resource
and extension
internally
No description provided.