How to Declare a Class Level Function in Swift

How do I declare a class level function in Swift?

Yes, you can create class functions like this:

class func someTypeMethod() {
//body
}

Although in Swift, they are called Type methods.

How can i pass class as a parameter to a function in Swift?

You cannot declare a function in Swift that could accept an input argument of several different types, so you cannot declare a type as A or B. However, you don't actually need this to solve your specific problem.

Since you want to access a common property of the two class instances, you should declare that property in a protocol, make both classes conform to that protocol, then make the function take an input argument of the protocol type.

protocol SomethingProtocol {
var something: String { get }
}

class A: SomethingProtocol {
let something = "Hello"
}

class B: SomethingProtocol {
let something = "World"
}

class C {
func request() {
//Call with class A or B it can contain any class. I can call either class A or B depending on condition
update(something: A())
update(something: B())
}

func update(something: SomethingProtocol) {
print(something.something) //Since both class have same varaible var something so this code should work either i pass class A or B through function
}

}

Swift 4.2 - Returning classes created inside a function

Swift is capable of creating new types at runtime, but I don't think that it's currently possible to use that capability as a language user.

In Swift, not all statements are executable: there is code that is evaluated at compile-time only and results in no executable code. Class statements are one of these. The class scope is not evaluated at runtime to create a new class: the compiler sees a class statement, builds that class, and gives you a static reference to that class going forward. That way, no code is executed at your program's startup (or at any other time, for that matter) when you create a class. This contrasts to other languages, such as Python, where every statement is inherently executable, and executing a class statement actually creates a class.

This behavior is emphasized by errors when you try to use local variables inside of function-local classes:

func foo(int: Int) {
class Bar {
let f = int
// error: class declaration cannot close over value 'int' defined in outer
// scope
}
}

Static vs class functions/variables in Swift classes?

static and class both associate a method with a class, rather than an instance of a class. The difference is that subclasses can override class methods; they cannot override static methods.

class properties will theoretically function in the same way (subclasses can override them), but they're not possible in Swift yet.

Class function in swift extension (category)

Yes, it possible and very similar, the main difference is that Swift extensions are not named.

extension UIColor {
class func colorWithHexString(hexString: String) -> UIColor {
// create color from string
// ... some code
return newColor
}
}

How to use @available with class variables

Well, the answer is no, we can't :(

Only at function or class level. In my case, I duplicated the class and renamed the previous one to MyClass13. And I put a if @available where i need to call it.

Thanks!

How to declare a 'protected' variable in swift

You would have to use internal for that as Swift doesn't offer a protected keyword (unlike many other programming languages). internal is the only access modifier between fileprivate and public:

Internal access enables entities to be used within any source file
from their defining module, but not in any source file outside of that
module. You typically use internal access when defining an app’s or a
framework’s internal structure.

There is a blog post that explains a little bit more about why the language designers chose not to offer a protected keyword (or anything equivalent).

Some of the reasons being that

It doesn’t actually offer any real protection, since a subclass can
always expose “protected” API through a new public method or property.

and also the fact that protected would cause problems when it comes to extensions, as it wouldn't be clear whether extensions should also have access to protected properties or not.

Class variables not yet supported

Swift now has support for static variables in classes. This is not exactly the same as a class variable (because they aren't inherited by subclasses), but it gets you pretty close:

class X {
static let y: Int = 4
static var x: Int = 4
}

println(X.x)
println(X.y)

X.x = 5

println(X.x)


Related Topics



Leave a reply



Submit