If a class is on #MainActor:
#MainActor class MyClass : NSObject
{
}
does this put all its extensions on #MainActor as well?
extension MyClass
{
}
Yes. As SE-0316 - Global actors says:
A type declared with a global actor attribute propagates the attribute to all methods, properties, subscripts, and extensions of the type by default.
Related
A/c to Apple Doc,
A function or method with an opaque return type hides its return value’s type information. Instead of providing a concrete type as the function’s return type, the return value is described in terms of the protocols it supports
EDIT: Cutting the whole question short
/* PROTOCOLS */
protocol MyPros {}
/* STRUCT */
struct MyStruct {}
/* CLASSES */
class ClassA : MyPros {}
class ClassB : ClassA {}
class ClassC : ClassB {}
Now I using Opaque return type, 1. with struct 2. with class
/* FUNCTIONS */
func getOpaqueStruct() -> some MyStruct { // ERROR: An 'opaque' type must specify only 'Any', 'AnyObject', protocols, and/or a base class
return MyStruct()
}
func getOpaqueClass() -> some ClassC {
return ClassC()
}
getOpaqueStruct returns error which is understandable, if you check getOpaqueClass , getOpaqueClass also not returning protocols, and/or a base class so why class object able to return any class object in the inheritance chain. It also must be returns the baseclass i.e. classA or protocol i.e. MyPros.
ClassC can be in future be superclass of ClassD but base class of these chain is ClassA.
I think you are misunderstanding what the phrase "base class" in the error message means. It doesn't mean "a class that doesn't inherit from anything, and can have subclasses". It just means "a class that can have subclasses" or in other words, "a class that other classes can inherit from".
Why only limit the feature to classes that doesn't inherit from anything, when anything that could have subclasses can benefit from this feature.
Old answer (to revision 3 of the question):
You can't do some StructType because structs don't have subclasses. They whole point of opaque return types is to allow you to tell the caller that:
I'm returning a definite type, but you don't need to know what type this is. You just need to know it is a subclass of XXXClass/confomer of YYYProtocol.
No other types can inherit from structs, so if you have a method that is said to return some StructType, then it can only return StructType, nothing else. This defeats the purpose of opaque return types.
From the name of your method, writing:
func getPlanet() -> some Planet {
return Earth(name: "Earth", creatures: "Humans")
}
would make more sense in my opinion.
The caller knows that getPlanet will return a definite type, that is a conformer of Planet.
I have an abstract Dart class which contains some abstract methods and a concrete, implemented method that I want to keep consistent across all subclasses.
IntelliJ's Dart Analysis system is reporting in subclasses that no concrete implementation exists for my implemented method — even though I can see that abstract has a concrete implementation.
I have also tried implementing the method in my subclass and calling the super class, but that is also reported as not being implemented. Both abstract class and child class are in the same file.
abstract class Exporter {
List<String> getHeaderRow();
List<dynamic> getEntryAsRow(EntriesData entry);
/// Concrete implementation
dynamic getVal(dynamic value) {
return null;
}
}
// ExpenseReport underlined in red: missing concrete implementation of getVal
class ExpenseReport implements Exporter {
List<String> getHeaderRow() {
return [];
}
List<dynamic> getEntryAsRow(EntriesData entry) {
return [];
}
// dynamic getVal(dynamic value) {
// super.getVal(value); // IntelliJ reports "getval" as abstract in superclass
// }
}
Saying
class ExpenseReport implements Exporter
you mean your ExpenseReport implements interface of Exporter, which in turn means that it has to implement all methods declared in Exporter.
If you want to inherit implemented methods from abstract Exporter you need to extend it like this:
class ExpenseReport extends Exporter
What's the difference between Protocols and class-bound Protocols, and which one we should use in Swift?
protocol A : class { ... }
protocol A { ... }
We get an error when attempting to add a weak delegate when the Protocol is not defined as : class:
protocol A { ... }
weak var delegate: A
Gives the error:
'weak' cannot be applied to non-class type
or
'weak' must not be applied to non-class-bound 'A'; consider adding a protocol conformance that has a class bound
Swift >= 4:
protocol A : AnyObject { ... {
Swift < 4:
protocol A : class { ... }
defines a "class-only protocol": Only class types (and not structures or enumerations) can adopt this protocol.
Weak references are only defined for reference types. Classes
are reference types, structures and enumerations are value types.
(Closures are reference types as well, but closures cannot adopt
a protocol, so they are irrelevant in this context.)
Therefore, if the object conforming to the protocol needs to be stored in a weak property then the protocol must be a class-only protocol.
Here is another example which requires a class-only protocol:
protocol A {
var name : String { get set }
}
func foo(a : A) {
a.name = "bar" // error: cannot assign to property: 'a' is a 'let' constant
}
This does not compile because for instances of structures and enumerations, a.name = "bar" is a mutation of a. If you define
the protocol as
protocol A : class {
var name : String { get set }
}
then the compiler knows that a is an instance of a class type to that
a is a reference to the object storage,
and a.name = "bar" modifies the referenced object, but not a.
So generally, you would define a class-only protocol if you need
the types adopting the protocol to be reference types and not value types.
If you are using Swift 4 or later, use AnyObject:
protocol A : AnyObject { ... }
Using class as before gives the warning and fix-it:
Using 'class' keyword to define a class-constrained protocol is deprecated; use 'AnyObject' instead
Replace 'class' with 'AnyObject'
You can make the protocol derive from any class type like NSObject or AnyObject:
protocol TopNewsTableDelegate : AnyObject {
func topNewsTableDidLoadedStories()
}
Or you can type like this
#objc protocol A { ... }
then you can make a weak delegate reference
protocol CustomProtocolName : NSObjectProtocol {
// ...
}
I want to encapsulate a generic object in another class without setting the generic type argument. I created a base Animal<T> class and defined other subclasses from it. Example:
public class Animal<T: YummyObject> {
// Code
}
public class Dog: Animal<Bark> {
// Code
}
public class Cat: Animal<Meow> {
// Code
}
and defined an Animal property, without the type argument, in the UITableView extension bellow:
extension UITableView {
private static var animal: Animal!
func addAnimal(animal: Animal) {
UITableView.animal = animal
}
}
but I get the following compile error when doing so:
Reference to generic type Animal requires arguments in <...>.
This seems to work fine in Java. How can I accomplish the same thing in Swift as well?
Swift doesn’t yet support wildcard-style generics like Java does (i.e., Animal<?>). As such, a common pattern is to define a type-erased superclass, protocol (or wrapper) to enable such usage instead. For instance:
public class AnyAnimal {
/* non-generic methods */
}
and then use it as your superclass:
public class Animal<T: YummyObject>: AnyAnimal {
...
}
Finally, use AnyAnimal in your non-generic code instead:
private static var animal: AnyAnimal!
Examples in the Swift Standard Library. For a practical example, see the KeyPath, PartialKeyPath, and AnyKeyPath classes hierarchy. They follow the same pattern I outlined above. The Collections framework provides even further type-erasing examples, but using wrappers instead.
In Java I can create a static initializer like:
static { ... }
In Swift I can have:
class MyClass {
class var myVar:Int?
}
Is it possible to create some kind of class/static var initializer in Swift?
If you need a computed property accessible from the class type and you want it to be like a constant value, the best option is static keyword.
Type Property Syntax
“For computed type properties for class types, you can use the class keyword instead to allow subclasses to override the superclass’s implementation.” Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/pt/jEUH0.l
With class keyword a subclass can override the computed value.
Best solution:
class MyClass {
static var myVar: Int {
return 0
}
}