How to create an extension for SequenceOf in Swift? - ios

Generic arguments are not allowed in Swift extensions, something like
public extension SequenceOf<T>{
...
}
is not possible. Using something like
public extension SequenceOf{
public func Something<T>(){
....
}
}
leads to
Extension of generic type 'SequenceOf' from a different module cannot provide public declarations
So, how does one extend SequenceOf (or similar types) with extra methods?

The problem here is not that your are extending SequenceOf but that you are doing it in a different module (i.e. a library or a framework).
Add the source file containing the extension to the project that is using it.

Related

Extension method on a class without a constructor in dart? [duplicate]

I'm trying to create a static extension method on one of my classes (which is autogenerated, so I can't easily modify it). According to the docs, this should be possible:
Extensions can also have static fields and static helper methods.
Yet even this small example does not compile:
extension Foo on String {
static String foo() => 'foo!';
}
void main() {
print(String.foo());
}
Error: Method not found: 'String.foo'.
print(String.foo());
^^^
What am I doing wrong?
The docs mean that the extension classes themselves can have static fields and helper methods. These won't be extensions on the extended class. That is, in your example, Foo.foo() is legal but String.foo() is not.
You currently cannot create extension methods that are static. See https://github.com/dart-lang/language/issues/723.
Note that you also might see Dart extension methods referred to as "static extension methods", but "static" there means that the extensions are applied statically (i.e., based on the object's type known at compilation-time, not its runtime type).
As James mentioned, you can't use the static method directly on the extended class as of today, the current solution to your problem would be:
extension Foo on String {
String foo() => 'foo!';
}
void main() {
print('Hi'.foo());
}

How to create a static extension method in Dart?

I'm trying to create a static extension method on one of my classes (which is autogenerated, so I can't easily modify it). According to the docs, this should be possible:
Extensions can also have static fields and static helper methods.
Yet even this small example does not compile:
extension Foo on String {
static String foo() => 'foo!';
}
void main() {
print(String.foo());
}
Error: Method not found: 'String.foo'.
print(String.foo());
^^^
What am I doing wrong?
The docs mean that the extension classes themselves can have static fields and helper methods. These won't be extensions on the extended class. That is, in your example, Foo.foo() is legal but String.foo() is not.
You currently cannot create extension methods that are static. See https://github.com/dart-lang/language/issues/723.
Note that you also might see Dart extension methods referred to as "static extension methods", but "static" there means that the extensions are applied statically (i.e., based on the object's type known at compilation-time, not its runtime type).
As James mentioned, you can't use the static method directly on the extended class as of today, the current solution to your problem would be:
extension Foo on String {
String foo() => 'foo!';
}
void main() {
print('Hi'.foo());
}

How to access private fields without the 'part of' directive?

Set the case there are multiple libraries of a package which need access to a commonly used class which is in its own library. This class gets exported and contains private fields which should only be accessible internally by libraries of the package. Because the part of directive is discouraged I am avoiding it. So this is the challenge: How can I access private fields of a public class from another library of the same package without using part of?
This is what a came up with:
class PublicClass {
Object _shouldNotBePublic;
}
class InternalClass extends PublicClass {
Object get publicInternally => _shouldNotBePublic;
}
And it partially solves the problem.
But now there is an exported function
void someFunction(PublicClass param) {
param._shouldNotBePublic;
}
which takes an argument of PublicClass and it needs to access the private _shouldNotBePublic field. That is exactly what C++ friend does. Are there any good solutions for Dart that don't involve part of?
Edit: The workaround I use for the time being is a simple not exported function in the same library like PublicClass:
Object getShouldNotBePublic(PublicClass obj) {
return obj._shouldNotBePublic
}

How to create a common property in swift4

I need a common property in my project so that I can use or share it thought out the application.
I have tried many solutions but didn't work.
I think it would make many things simple and reusable.
You can use Extension
as documented in
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/doc/uid/TP40014097-CH32-ID383
you can change previously defined classes properties
example
extension UIColor {
class var customGreen: UIColor {
let darkGreen = 0x008110
return UIColor.rgb(fromHex: darkGreen)
}
}
Make use of the iOS Design pattern "Singleton".
Its uses only one reference for a class and the same property will get reflected where ever you are using it throughout the app.
Reference : https://thatthinginswift.com/singletons/
You can create a lightweight "namespace" by declaring a public struct or enum. Then, simply add your static vars and you can safely share it.
As an added benefit, you get thread-safety for free. (Static stored property initializers are thread-safe.)
public enum SharedConstants {
public static var id = "MyID"
public static var hashCode = 12345678
}
By the way, extensions are great if you want to enhance an existing type. Type extensions let you add new functionality to a type without modifying its original code.
Yet, if all you need is a common shared property, extensions may not be the best choice.

The use of private keyword in Swift and their use in protocols?

I'm building an iOS app where i want to make a protocol (Which by my understanding is the equivalent of java interfaces) for my model, to use for Unit Testing purposes.
In Java you typically want to encapsulate your values in a model and make them accessible through getters and setters only.
How can i ensure this encapsulation in Swift with protocols, where i can't use the private keyword for properties.
My model is setup something like this:
class model {
private var property: Int = 5
func getProperty() -> Int {
return property
}
func setProperty(newValue: Int) {
self.property = newValue
}
}
And i want my protocol to look something like this:
protocol modelProtocol {
private var property: Int { get set }
}
My problem is that i can't declare my properties private, is this just a thing in Swifts access control (I've read they have private, internal and public) that you don't use private properties that much or is there an equivalent to Java's way of handling interfaces and private variables?
(Note: I'm using Xcode 7 and swift 2.0 if that matters)
I don't think that you can have private properties and public Getters and Setters with Swift code, as the getters and setters cannot have a higher access control than the property. For unit testing purposes you can access any internal entity using the #testable attribute as shown in the documentation below.
Access Levels for Unit Test Targets
When you write an app with a unit test target, the code in your app needs to be made available to that module in order to be tested. By default, only entities marked as public are accessible to other modules. However, a unit test target can access any internal entity, if you mark the import declaration for a product module with the #testable attribute and compile that product module with testing enabled.
I do not think you would be able to declare private properties within a protocol, you may need to use a base class instead with internal properties and extend that class. I am still fairly new to protocols myself, but I believe they are mainly used to ensure code conforms to that protocol as in... it provides that functionality or those methods.
References Used:
Swift Access Control
Swift Properties
Swift unit testing access

Resources