The error message tells us to use the default Swift initializer rather than the Objective-C factory method. The one notable suggestion might be a singleton, but if we have a singleton factory method, we should follow the pattern Apple has set, and name our factory method with the "shared" prefix: + (instancetype)sharedMessage Īnd the following Swift code would be perfectly valid: let message = WXMediaMessage.sharedMessage()īut if message isn't a singleton, then it should be no more than return init], which is what we'd get with the following Swift code: let message = WXMediaMessage()Īnd this is what the error message is telling us to do: 'message()' is unavailable: use object construction 'WXMediaMessage()' But best practice would suggest that our factory method should be nothing more than init]. This will go straight to init without calling the factory wrapper. It's important to note that the actual factory method doesn't get called when we use the Swift default initializer. In Objective-C, this is the equivalent to calling: init] In Swift, we should instead be simply calling WXMediaMessage(). So while we'd have to call the method like this in Objective-C: The answer is because Swift actually remaps convenience methods to constructors. Matt's answer is correct in that renaming the method will fix the problem, but we're still left asking why.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |