I have a phone number model which looks like this:
import UIKit
import Foundation
struct PhoneValidation : OptionSet {
let rawValue: Int
static let phoneInValid = PhoneValidation(rawValue: 1 << 0)
static let phoneValid = PhoneValidation(rawValue: 1 << 1)
static let smsValidationAttempted = PhoneValidation(rawValue: 1 << 2)
static let smsValidationFailed = PhoneValidation(rawValue: 1 << 3)
static let smsValidationSuccessful = PhoneValidation(rawValue: 1 << 4) // OTP is successfully validated in backend. The field should be non-editable in this duration
static let smsValidationOTPTriggered = PhoneValidation(rawValue: 1 << 5) // OTP validation triggered. The field should be non-editable in this duration
}
class PhonesViewModel: NSCopying {
public var phoneType: PhoneNumberType = PhoneNumberType.mobile
public var phone: String?
public var code: String?
public var countryCode: String?
public var isValid : PhoneValidation?
func copy(with zone: NSZone? = nil) -> Any {
let copy = PhonesViewModel()
copy.phoneType = phoneType
copy.phone = phone
copy.code = code
copy.countryCode = countryCode
copy.isValid = isValid
return copy
}
}
As you can see above the phone model can transition between different states. The SMS validation is available for few countries and for few it is not applicable. So, I plan on setting smsValidationOTPTriggered state when SMS validation is applicable for a country and while the validation is in progress.
What I need here is, while the states smsValidationOTPTriggered or smsValidationSuccessful are set I would not want any module of the application to modify the values(phoneType, phone, code, countryCode) of the model. In other words, I would like the model to switch to a read-only mode while these 2 states are set in model and would like the module to be informed with an error or exception when a modification is attempted.
Is there a best practice already available for what I am trying to achieve here? I have searched before raising this question but did not find any. How can I achieve this?
Thanks,
Raj Pawan Gumdal
How about something like this, I think its better to use property wrappers for your case! The below is not an exact solution but can modify/change to accommodate your need
import UIKit
enum PhoneNumberType {
case mobile
}
enum PhoneValidation {
case phoneInValid
case phoneValid
case smsValidationAttempted
case smsValidationFailed
case smsValidationSuccessful
case smsValidationOTPTriggered
}
struct PhonesViewModel {
public var phoneType: PhoneNumberType = PhoneNumberType.mobile
public var phone: String?
public var code: String?
public var countryCode: String?
public var phoneValidation : PhoneValidation?
func validate(value: [PhoneValidation]) -> Bool {
//add proper check here
return false
}
}
#propertyWrapper
struct Wrapper {
private(set) var value: PhonesViewModel? = nil
var validators: [PhoneValidation] = []
var wrappedValue: PhonesViewModel? {
get { value }
set {
if let model = newValue, model.validate(value: validators) {
value = newValue
print("Value assigned")
} else {
print("Value not assigned")
}
}
}
}
struct SomeOtherClass {
#Wrapper(validators: [PhoneValidation.phoneInValid])
var model: PhonesViewModel?
}
var a = SomeOtherClass()
a.model = PhonesViewModel()
a.model = PhonesViewModel()
You can use a technique with the name "popsicle immutability". An object is initially mutable, but can be "frozen". Modifications for frozen objects are forbidden. In your case PhonesViewModel become frozen when isValid property have value smsValidationOTPTriggered or smsValidationSuccessful.
Let's add Freezable protocol for requirements to objects that can become immutable and conforming for PhonesViewModel:
protocol Freezable: class {
var isFrozen: Bool { get }
}
extension PhonesViewModel: Freezable {
var isFrozen: Bool {
isValid == .smsValidationOTPTriggered || isValid == .smsValidationSuccessful
}
}
Now we must add validation for isFrozen value when a property is assigned. It can be added in property observers like:
...
public var phone: String? {
didSet {
validate()
}
}
...
private func validate() {
assert(!isFrozen)
}
Or using property wrapper:
#propertyWrapper
struct Guarded<Value> {
private var value: Value
init(wrappedValue: Value) {
value = wrappedValue
}
#available(*, unavailable)
var wrappedValue: Value {
get { fatalError("only works on instance properties of classes that conforms to Freezable protocol") }
set { fatalError("only works on instance properties of classes that conforms to Freezable protocol") }
}
static subscript<EnclosingSelf: Freezable>(
_enclosingInstance object: EnclosingSelf,
wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>,
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self>
) -> Value {
get {
object[keyPath: storageKeyPath].value
}
set {
precondition(!object.isFrozen, "Object \(object) is frozen! Modifications are forbidden")
object[keyPath: storageKeyPath].value = newValue
}
}
}
So your class will look like:
class PhonesViewModel: NSCopying {
#Guarded
public var phoneType: PhoneNumberType = PhoneNumberType.mobile
#Guarded
public var phone: String?
#Guarded
public var code: String?
#Guarded
public var countryCode: String?
#Guarded
public var isValid : PhoneValidation?
func copy(with zone: NSZone? = nil) -> Any {
let copy = PhonesViewModel()
copy.phoneType = phoneType
copy.phone = phone
copy.code = code
copy.countryCode = countryCode
copy.isValid = isValid
return copy
}
}
So I'm learning how to make my Android apps into an iOS app. I've gotten pretty far though I'm having an issue when it comes creating the object. In my android app I read in a csv file and create a FireworkList object that contains info. Here is the code from my Android app:
import java.util.ArrayList;
public class FireworksList{
private ArrayList<Integer> _category = new ArrayList<>();
private ArrayList<String> _name = new ArrayList<>();
private ArrayList<String> _shotCount = new ArrayList<>();
private ArrayList<String> _price = new ArrayList<>();
private ArrayList<String> _description = new ArrayList<>();
private ArrayList<String> _videoUrl = new ArrayList<>();
private ArrayList<Integer> _imageResourceNumber = new ArrayList<>();
private ArrayList<Boolean> _favorite = new ArrayList<>();
private ArrayList<Boolean> _special = new ArrayList<>();
private Integer _specialCategoryNumer =15;
private Integer _nextId;
public FireworksList() {
_nextId=0;
}
public ArrayList<Integer> get_category() {
return _category;
}
public ArrayList<String> get_name() {
return _name;
}
public ArrayList<String> get_shotCount() {
return _shotCount;
}
public ArrayList<String> get_price() {
return _price;
}
public ArrayList<String> get_description() {
return _description;
}
public ArrayList<String> get_videoUrl() {
return _videoUrl;
}
public ArrayList<Integer> get_imageResourceNumber() {
return _imageResourceNumber;
}
public ArrayList<Boolean> get_favorite() {
return _favorite;
}
public void set_favorite(int index, Boolean bool) {
_favorite.set(index,bool);
}
public ArrayList<Boolean> get_special(){return _special;}
public void Add(int cat, String name, String shot, String price, String description, String video, int image, Boolean fav, Boolean special){
_category.add(_nextId,cat);
_name.add(_nextId,name);
_shotCount.add(_nextId,shot);
_price.add(_nextId,price);
_description.add(_nextId,description);
_videoUrl.add(_nextId,video);
_imageResourceNumber.add(_nextId,image);
_favorite.add(_nextId,fav);
_special.add(_nextId,special);
_nextId++;
}
public int Count(){
return _nextId;
}
public FireworksList CategorySort(int position){
FireworksList fireworksListTemp = new FireworksList();
for(int i=0; i<_nextId;i++){
if(position==0){
fireworksListTemp.Add(_category.get(i),_name.get(i),_shotCount.get(i),_price.get(i),_description.get(i),_videoUrl.get(i),_imageResourceNumber.get(i),_favorite.get(i),_special.get(i));
}
else if(position==_category.get(i)){
fireworksListTemp.Add(_category.get(i),_name.get(i),_shotCount.get(i),_price.get(i),_description.get(i),_videoUrl.get(i),_imageResourceNumber.get(i),_favorite.get(i),_special.get(i));
}
else if(position==_specialCategoryNumer&&_special.get(i)==true){
fireworksListTemp.Add(_category.get(i),_name.get(i),_shotCount.get(i),_price.get(i),_description.get(i),_videoUrl.get(i),_imageResourceNumber.get(i),_favorite.get(i),_special.get(i));
}
}
return fireworksListTemp;
}
public FireworksList FavoriteSort(){
FireworksList fireworksListTemp = new FireworksList();
for(int i = 0; i<_nextId;i++){
if(_favorite.get(i)==true){
fireworksListTemp.Add(_category.get(i),_name.get(i),_shotCount.get(i),_price.get(i),_description.get(i),_videoUrl.get(i),_imageResourceNumber.get(i),_favorite.get(i),_special.get(i));
}
}
return fireworksListTemp;
}
public int FindIndex(String name, FireworksList fireworksList){
int found=0;
for(int j=0;j<fireworksList.Count();j++){
if(fireworksList.get_name().get(j).equals(name)){
found=j;
}
}
return found;
}
}
I've started rewriting my code yet I ran into an issue when it came to creating a new Firework list then appending to it with what is needed in the categorySort function. Here is my modified code so far:
class FireworkList {
var _category = [Int]()
var _name = [String]()
var _shotCount = [String]()
var _price = [String]()
var _description = [String]()
var _videioUrl = [String]()
var _imageResourceNumber = [Int]()
var _favorite = [Bool]()
var _special = [Bool]()
private var _specialCategoryNumber : Int = 15
private var _nextId : Int = 0
func FireworksList(){_nextId = 0}
func get_category() -> Array<Int>{return _category}
func get_name() -> Array<String>{return _name}
func get_shotCount() -> Array<String>{return _shotCount}
func get_price() -> Array<String>{return _price}
func get_discription() -> Array<String>{return _description}
func get_videoUrl() -> Array<String>{return _videioUrl}
func get_imageResourceNumber() -> Array<Int>{return _imageResourceNumber}
func get_favorite() -> Array<Bool>{return _favorite}
func get_special() -> Array<Bool>{return _special}
func set_favorite(index : Int, bool : Bool){_favorite[index] = bool}
func add(cat : Int, name : String, shot : String, price : String, description : String, video : String, image : Int, fav : Bool, special : Bool){
_category.insert(cat, at: _nextId)
_name.insert(name, at: _nextId)
_shotCount.insert(shot, at: _nextId)
_price.insert(price, at: _nextId)
_description.insert(description, at: _nextId)
_videioUrl.insert(video, at: _nextId)
_favorite.insert(fav, at: _nextId)
_special.insert(special, at: _nextId)
_nextId+=1
}
func Count()->Int{return _nextId}
func CategorySort(position : Int)-> [FireworkList]{
var fireworkListTemp = [FireworkList]()
for i in 0..._nextId{
if(position == 0){
fireworkListTemp.append(FireworkList())
}
}
return fireworkListTemp
}
}
I'm thankful for any help.
The main issue here is that your java class is poorly designed and you are taking this design with you when writing it in swift. Rather than having a bunch of properties that are collections you should create a struct for those properties and have one array with the struct. (I didn't include all properties to keep the code shorter).
struct Firework {
var category: Int
var name: String
var shotCount: String
var price: String
var special: Bool
}
and then declare it in the main class
class FireworkList {
private var fireworks = [Firework]()
Below is the class with the add, count and categorySort functions to show some examples of how to use this struct. As you can see this means much less code. I also taken the liberty to rename properties and functions to follow recommended swift naming practices.
For the categorySort I have made use of the high order function filter to collect the correct items. If you are going to work with swift I would recommend you learn more about it and the other high order functions like sort, map etc.
Also worth mentioning to someone coming from java is that we don't use get/set methods to the same extent in swift. We most access the property directly like let x = myObject.someValue or myObject.someValue = 10
class FireworkList {
var fireworks = [Firework]()
private let specialCategoryNumber = 15
func add(cat : Int, name : String, shot : String, price : String, special: Bool) {
self.fireworks.append(Firework(category: cat, name: name, shotCount: shot, price: price, special: special))
}
func count() -> Int { return fireworks.count }
func categorySort(position : Int) -> [Firework] {
switch position {
case 0:
return self.fireworks
case specialCategoryNumber:
return self.fireworks.filter { $0.category == specialCategoryNumber && $0.special}
default:
return self.fireworks.filter {$0.category == position}
}
}
}
Here's how I would do it:
public class FireworksList {
private var _category = [Int]()
private var _name = [String]()
private var _shotCount = [String]()
private var _price = [String]()
private var _description = [String]()
private var _videoURL = [String]()
private var _imageResourceNumber = [Int]()
public var favorite = [Bool]()
private var _special = [Bool]()
private let specialCategoryNumber = 15
private var nextId = 0
public var category: [Int] {
get {
return _category
}
}
public var name: [String] {
get {
return _name
}
}
public var shotCount: [String] {
get {
return _shotCount
}
}
public var price: [String] {
get {
return _price
}
}
public var description: [String] {
get {
return _description
}
}
public var videoURL: [String] {
get {
return _videoURL
}
}
public var imageResourceNumber: [Int] {
get {
return _imageResourceNumber
}
}
public var special: [Bool] {
get {
return _special
}
}
public var count: Int {
get {
return nextId
}
}
public func add( cat: Int, name: String, shot: String, price: String, description: String, video: String, image: Int, fav: Bool, special: Bool) {
self._category.append(cat)
self._name.append(name)
self._shotCount.append(shot)
self._price.append(price)
self._description.append(description)
self._videoURL.append(video)
self._imageResourceNumber.append(image)
self.favorite.append(fav)
self._special.append(special)
nextId += 1
}
public func categorySort(at position: Int) -> FireworksList {
let tmp = FireworksList()
for i in 0...nextId {
if position == 0 || position == category[i] ||
(position == specialCategoryNumber && special[i] == true) {
tmp.add(cat: category[i], name: name[i], shot: shotCount[i], price: price[i], description: description[i], video: videoURL[i], image: imageResourceNumber[i], fav: favorite[i], special: special[i])
}
}
return tmp
}
public func favouriteSort() -> FireworksList {
let tmp = FireworksList()
for i in 0...nextId {
if favorite[i] {
tmp.add(cat: category[i], name: name[i], shot: shotCount[i], price: price[i], description: description[i], video: videoURL[i], image: imageResourceNumber[i], fav: favorite[i], special: special[i])
}
}
return tmp
}
// I'm putting this function as static as it isn't directly related to any particular instance of FireworksList at any point of time.
public static func findIndex(name: String, fireworksList: FireworksList) -> Int {
var found = 0
for j in 0...fireworksList.count {
if fireworksList.name[j] == name {
found = j
}
}
return found
}
}
My code simply almost directly follows your Java code.
This is my first time trying to contribute in Stack Overflow, and I hope this can be helpful to you.
I am trying to work with swift generics, but I am stuck...
It might be impossible to do it this way, but I was hoping someone would have a good suggestion.
So I have this protocol and a type that I want to have internal:
internal protocol ATCoreInstrumentProtocol { // SOME STUFF }
internal typealias AT_1G_WaveInstrumentType = //Somethings that conforms to ATCoreInstrumentProtocol
Then I have this InstrumentType that I want to be public.
The problem here is that ASSOCIATED_INSTRUMENT aka ATCoreInstrumentProtocol
needs to be internal and therefore I cannot use it in this way.
There is not an option to make the ATCoreInstrumentProtocol public.
public protocol InstrumentType {
static var instrumentType: SupportedInstrumentTypes { get }
associatedtype ASSOCIATED_INSTRUMENT: ATCoreInstrumentProtocol
}
public final class InstrumentTypes {
private init() {}
public final class AT_1G_Wave : InstrumentType {
public class var instrumentType: SupportedInstrumentTypes { get { return .wave_1G } }
public typealias ASSOCIATED_INSTRUMENT = AT_1G_WaveInstrumentType
}
}
This is how I want to use it.
internal class ATCoreInteractor<IT: InstrumentType> {
internal var instrumentObservable : Observable<IT.ASSOCIATED_INSTRUMENT> {
return self.instrumentSubject.asObservable()
}
private var instrumentSubject = ReplaySubject<IT.ASSOCIATED_INSTRUMENT>.create(bufferSize: 1)
internal init(withSerial serial: String){
self.scanDisposable = manager
.scanFor([IT.instrumentType])
.get(withSerial: serial)
.take(1)
.do(onNext: { (inst) in
self.instrumentSubject.onNext(inst)
})
.subscribe()
}
And then I have the ATSyncInteractor which is public
public final class ATSyncInteractor<IT : InstrumentType> : ATCoreInteractor<IT> {
public override init(withSerial serial: String) {
super.init(withSerial: serial)
}
}
public extension ATSyncInteractor where IT : InstrumentTypes.AT_1G_Wave {
public func sync(opPriority: BLEOperationPriority = .normal, from: Date, to: Date, callback: #escaping (ATCoreReadyData<AT_1G_Wave_CurrentValues>?, [WaveTimeSeriesCoordinator], Error?) -> Void) {
// DO SOMETHING
}
}
let interactor = ATSyncInteractor<InstrumentTypes.AT_1G_Wave>(withSerial: "12345")
This ended up beeing my solution..
The InstrumentType only contains the SupportedInstrumentTypes.
The ATCoreInteractor Is no longer generic, but the observer is of type ATCoreInstrumentProtocol.
The ATSyncInteractor extensions casts the instrument to its associated type.
I am open to improvements and suggestions.
public protocol InstrumentType : class {
static var instrumentType: SupportedInstrumentTypes { get }
}
public final class InstrumentTypes {
private init() {}
public final class AT_1G_Wave : InstrumentType {
private init() {}
public class var instrumentType: SupportedInstrumentTypes { get { return .wave_1G } }
}
}
public class ATCoreInteractor {
internal var instrumentSubject = ReplaySubject<ATCoreInstrumentProtocol>.create(bufferSize: 1)
internal init(for type: SupportedInstrumentTypes, withSerial serial: String){
self.scanDisposable = manager
.scanFor([type])
.get(withSerial: serial)
.take(1)
.do(onNext: { (inst) in
self.instrumentSubject.onNext(inst)
})
.subscribe()
}
}
public final class ATSyncInteractor<IT : InstrumentType> : ATCoreInteractor {
public init(withSerial serial: String) {
super.init(for: IT.instrumentType, withSerial: serial)
}
}
public extension ATSyncInteractor where IT : InstrumentTypes.AT_1G_Wave {
private func instrument() -> Observable<AT_1G_WaveInstrumentType> {
return self.instrumentSubject.map { $0 as! AT_1G_WaveInstrumentType }
}
public func sync(opPriority: BLEOperationPriority = .normal, from: Date, to: Date, callback: #escaping (ATCoreReadyData<AT_1G_Wave_CurrentValues>?, [WaveTimeSeriesCoordinator], Error?) -> Void) {
}
}
I've followed the solution at Make a Swift dictionary where the key is "Type"? to create dictionaries that can use a class type as keys.
What I want to do is: I have one dictionary that should store class types with their class type (aka metatype) as keys, too:
class MyScenario {
static var metatype:Metatype<MyScenario> {
return Metatype(self)
}
}
var scenarioClasses:[Metatype<MyScenario>: MyScenario.Type] = [:]
Then I have methods to register and execute scenarios:
public func registerScenario(scenarioID:MyScenario.Type) {
if (scenarioClasses[scenarioID.metatype] == nil) {
scenarioClasses[scenarioID.metatype] = scenarioID
}
}
public func executeScenario(scenarioID:MyScenario.Type) {
if let scenarioClass = scenarioClasses[scenarioID.metatype] {
let scenario = scenarioClass()
}
}
... Problem is in the last line:
Constructing an object of class type 'MyScenario' with a metatype
value must use a 'required' initializer.
It looks like the compiler is confused at that point since I cannot use 'required' at that assignment. Does anyone have an idea how I would have to instantiate the scenarioClass in executeScenario()?
This must do the job.
import Foundation
struct Metatype<T> : Hashable
{
static func ==(lhs: Metatype, rhs: Metatype) -> Bool
{
return lhs.base == rhs.base
}
let base: T.Type
init(_ base: T.Type)
{
self.base = base
}
var hashValue: Int
{
return ObjectIdentifier(base).hashValue
}
}
public class MyScenario
{
var p: String
public required init()
{
self.p = "any"
}
static var metatype:Metatype<MyScenario>
{
return Metatype(self)
}
}
var scenarioClasses:[Metatype<MyScenario>: MyScenario.Type] = [:]
public func registerScenario(scenarioID:MyScenario.Type)
{
if (scenarioClasses[scenarioID.metatype] == nil)
{
scenarioClasses[scenarioID.metatype] = scenarioID
}
}
public func executeScenario(scenarioID:MyScenario.Type)
{
if let scenarioClass = scenarioClasses[scenarioID.metatype]
{
let scenario = scenarioClass.init()
print("\(scenario.p)")
}
}
// Register a new scenario
registerScenario(scenarioID: MyScenario.self)
// Execute
executeScenario(scenarioID: MyScenario.self)
// Should print "any"
The code below is quite contrived but the bottom line is I have a variable that contains a type (e.g. let someType: AnyObject = SomeClass.self) and I want to get the value of a static property belonging to the type stored in that variable. Is this possible in Swift?
class SomeClass {
var someDict: Dictionary<String, AnyObject> = {
"aKey": SomeOtherClass.self
"anotherKey": SomeOtherOtherClass.self
}
func someFunction() {
let someType: AnyObject? = someDict["aKey"]
// This line is incorrect, but how would get the static value I am after?
let aString: String = someType.someString
println(aString)
}
}
class SomeOtherClass {
static var someString: String = "Hello World"
}
class SomeOtherOtherClass {
static var someString: String = "Foo Bar"
}
The answer is to change AnyObject references to SomeOtherBaseClass.Type where SomeOtherBaseClass is a parent class of both SomeOtherClass and SomeOtherOtherClass.
class SomeClass {
var someDict: Dictionary<String, SomeOtherBaseClass.Type> = {
"aKey": SomeOtherClass.self
"anotherKey": SomeOtherOtherClass.self
}
func someFunction() {
let someType: SomeOtherBaseClass.Type? = someDict["aKey"]
// This line is incorrect, but how would get the static value I am after?
let aString: String = someType!.someString
println(aString)
}
}
class SomeOtherClass {
static var someString: String = "Hello World"
}
class SomeOtherOtherClass {
static var someString: String = "Foo Bar"
}
Thanks to #lukya for nudging me in the right direction.