Dear all,
I am currently playing with a concept in Swift to store statistics in apps
using generic types. One of the key parts are the ability to register
properties (for example booleans, strings and integers - but also more complex
data types such as dates):
stats.add(description: "iOS Version", property: {
return UIDevice.current.systemVersion as? String
}))
stats.add(description: "HealthKit authorized", property: {
return HealthKit.authorized
}))
I've played with different kind of implementations of this, but feel that Swift
limits me in some ways, because I can not find a way to store different types
in a list.
I have tried to implement it in Java, and this works:
import java.util.ArrayList;
import java.util.List;
public class Property<T> {
private String description;
private T property;
public Property(String description, T property){
this.description = description;
this.property = property;
}
public String getDescription() {
return description;
}
public T getProperty() {
return property;
}
}
public class Analytics {
private List<Property> properties = new ArrayList<>();
public Analytics() {
}
public static void main(String[] args) {
Analytics stats = new Analytics();
stats.add(new Property("HealthKit", false));
stats.add(new Property("iOS Version", "9.3"));
stats.show();
}
public void add(Property property) {
properties.add(property);
}
public void show() {
for (Property p : properties) {
System.out.println(p.getDescription() + ": " + p.getProperty());
}
}
}
How would I be able to achieve this in Swift? My current idea of this approach
does not seem to work, but also the idea to create specific subclasses for
every type (and then type cast the different properties), gives a lot of work,
and there would be a limitation for which types are available.
Thank you for your time.
Hugo Lundin.
_______________________________________________
swift-users mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users