Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
818 views
in Technique[技术] by (71.8m points)

swift - Benefits of using class func vs func vs no class declaration

Ok so I have a a bunch of helper functions in my project that I originally had in a class called Animate. I was wonder what are the benefits of declaring func vc class func.

Lets use this as an example class:

class Animate{
    func moveView(...){
        ...
    }
}

So I believe if I have a class func I don't have to instantiate the class as so.

Animate.moveView(...)

And if I just declare the function with func it would be:

Animate().moveView(...)

However if I don't declare the file as a class at all as so:

func moveView(...){
    ...
}

When I call the function it is just:

moveView(...)

With no indication where the code came from and it can be just used like this anywhere in the project.

What are the pros and cons of these three ways? Is not declaring a class bad practice? Or, is there some edge case that this is very useful? For example in my situation I have no need for a class since I am just creating helper functions and not an object.

Thanks in advance for any insight on this!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok. Instance methods vs class methods vs global methods.

(The term method and function are interchangeable. Method implies a function implemented by an object, so I tend to prefer the term method to the term function.)

An instance method is a method that is performed by instances of a class. You must have an instance of that class to talk to in order to invoke an instance method.

Instance methods have access to the instance variables of the object they belong to, so the object can save state information between calls. (In a networking class you could create multiple download objects, each of which manages an individual file download of a different file from a different URL, and each might have a different delegate it notifies when it's download is complete)

Class methods are invoked by the class itself, not by an instance. This can make it simple to invoke helper functions without having to manage an object to do that work for you. Since class methods don't talk to an instance of the class, they can't preserve different state information for each object. You might have a utilities class that performs localization functions on strings for example. The localization process is self-contained. You call a class function and pass in a string and the language you want it localized to, and it hands you back a result. No need to keep state between calls. Such a call might look like

let frenchString = 
  LocalizationUtils.localizeString("English String", 
    toLanguage: "French")

Global functions do not belong to any particular class. They are global to the entire module in which they are defined. They are similar to class functions, except that they are not specific to a particular class.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...