SwiftMessages
SwiftMessages copied to clipboard
Naming
Hello
Please add Prefix to all files, nibs etc. I got situation when I add xib with name CardView, your bundle also has xib named CardView, so function
fileprivate class func internalViewFromNib<T: UIView>(named name: String, bundle: Bundle? = nil, filesOwner: AnyObject = NSNull.init()) throws -> T {
incorrectly select bundle for getting view CardView and I got crash. I guess adding prefix as recommended by Apple "XYZ" will prevent similar problems
Or better instead of
if Bundle.main.path(forResource: name, ofType: "nib") != nil {
resolvedBundle = Bundle.main
} else {
resolvedBundle = Bundle.sm_frameworkBundle()
}
use just
resolvedBundle = Bundle.sm_frameworkBundle()
Thanks
As far as I know, prefixing is not a recommendation from Apple for Swift frameworks since the framework itself provides a namespace. If I'm mistaken, please point me to the doc.
But you raise a great point. The fact that SwiftMessages tries to load nibs in your main bundle is a feature. The idea is to make it as easy as possible to customize the design of the view by simply drag-and-dropping a copy into your project and making changes. But in doing so, it is taking the risk of namespace clashes, something I hadn't considered.
It seems like there are two options for SwiftMessages:
- Rename the nib files. This is a breaking change.
- Provide an option to stop looking in the main bundle.
I'm leaning toward (2) since this is something I can release immediately (a breaking change will need to wait for a major release).
Until I get a fix in place, there are a couple of options for you:
- Rename your nib file.
- Copy the SwiftMessages nib file into your project and rename it. Then load it using
SwiftMessages.viewFromNib(named: "someNibName")
.
Let me know if you have any other ideas.
This is an old issue, but I'm facing the same problem for class name. My app also has some classes named BaseView
, MessageView
, Animator
... I can directly use my app namespace (MyApp.BaseView
...) to prevent conflict with SwiftMessages, but these classes are used many more often than SwiftMessages so that is inconvinience. The problem is, SwiftMessages
is the framework module name AND also a class of the framework itself, so this is NOT working:
let view = SwiftMessages.BaseView(frame: .zero)
It causes compile error:
Type 'SwiftMessages' has no member 'BaseView'
Is there any way to handle this?
This is a Swift bug, which is why I haven't made the breaking changes required to fix it:
https://bugs.swift.org/browse/SR-898
However, it hasn't been fixed in 4 years, so I've been thinking about changing the SwiftMessages
type to Messenger
or something like that. Still on the fence because this affects very few people and the change would break everyone.