I have following code working in a playground. The didSet observer is working as expected.
struct itemStruct {
var name : String = "" {
didSet (newName) {
didSetNameTest(name)
}
}
}
func didSetNameTest (name : String) {
println("didSet itemStruct: \(name)")
}
var item = itemStruct()
item.name = "test"
If I move the code within a class I get an compiler error:
class itemClass {
struct classItemStruct{
var name : String = "" {
didSet(newName) {
didSetClassNameTest(name)
}
}
}
func didSetClassNameTest(name:String) {
println("didSet itemClass: \(name)")
}
var structItem = classItemStruct()
}
var cItem = itemClass()
cItem.structItem.name = "Test"
Error: Cannot invoke 'didSelectClassNameTest' with an argument list of type '(String)'.
All code could be copied in playground.
Since instance of an inner class is independent of any instance of the outer class as described in a link of #ABakerSmith comment a possible workaround for this would be by makeing didSetClassNameTest function private and static and then call it statically itemClass.didSetClassNameTest(name) on didSet method
Inner types are independent of their outer types. Unlike in java classItemStruct knows nothing about its outer type itemClass.
Related
How to send information from one class A to another class B using rxswift ? In iOS . for example I want to send my class Account.
Maybe rxswift is not for delegates or sending information at all.
https://www.reddit.com/r/swift/comments/7pxt4h/rxswift_vs_delegation_which_is_better/
class Account
{
var myId = 0
var Name = "name"
init (myId inputMyId:Int , Name inputName: String)
{
myId = inputMyId
Name = inputName
}
}
class B
{
var myAccount : Account
func receive()
{
// here self.myAccount should be replaced with myA.myAccount
// where myA is object of class A
}
}
class A
{
var myAccount : Account
func send()
{
// here myB.myAccount should be replaced with self.myAccount
// where myB is object of class B
}
}
solution 1
create class C that will make observables equal from A and B
class SimpleC
{
func makeAB()
{
var theB : SimpleB = SimpleB()
var theA : SimpleA = SimpleA()
theA.valueAccount = theB.valueAccount
theB.receive()
theA.send()
}
}
class SimpleA
{
var myAccount : Account = Account(myId : 0, Name : "")
var valueAccount = PublishSubject<Account>()
var myB : SimpleB?
func send()
{
// here myB.myAccount should be replaced with self.myAccount
// where myB is object of class B
self.myAccount = Account(myId : 1, Name : "MicrosoftTest5")
self.valueAccount.onNext(self.myAccount)
self.myAccount = Account(myId : 2, Name : "bestWorkTest5")
self.valueAccount.onNext(self.myAccount)
}
}
class SimpleB
{
var myAccount : Account = Account(myId : 0, Name : "")
var valueAccount = PublishSubject<Account>()
let disposer = DisposeBag()
func receive()
{
// here self.myAccount should be replaced with myA.myAccount
// where myA is object of class A
valueAccount.subscribe(onNext: { (newValue) in
print("SimpleB subscribe")
self.myAccount = newValue
var theString = "Account{" + "\(self.myAccount.myId)"
theString = theString + "} {"
theString = theString + "\(self.myAccount.Name)" + "}"
print(theString)
}).addDisposableTo(disposer)
}
}
Answering this question in hope to cover two scinarios -
Case 1: passing data while object creation (I can create and wrap it in variables, and pass along), this is same as vanilla swift
This holds true in cases of pushing/ presenting as well
class Account
{
var myId = Variable(0)
var name = Variable("")
init (myId inputMyId:Int , Name inputName: String)
{
myId.value = inputMyId
name.value = inputName
}
func passDataAnotherAccount() {
let anotherAccount = AnotherAccount(id: myId, name: name)
anotherAccount.makeChangesToVars()
}
}
class AnotherAccount {
var classListenerOfMyId = Variable(0)
var classListenerOfMyName = Variable("name")
init(id: Variable<Int>, name: Variable<String>) {
self.classListenerOfMyId = id
self.classListenerOfMyName = name
}
func makeChangesToVars() {
self.classListenerOfMyId.value = self.classListenerOfMyId.value + 1
self.classListenerOfMyName.value = Date().description
}
}
case 2 (Alternative approach to DELEGATION and callbacks)
lets say you want to know about changes from class AnotherAccount in previous class, you can make use of OBSERVABLES in that case
change the method from class Account to this
func passDataAnotherAccount() {
let anotherAccount = AnotherAccount(id: myId, name: name)
anotherAccount.makeChangesToVars()
// this will observe changes from AnotherAccount class
// we just created above in my current class,
// i.e. Account class
anotherAccount.classListenerOfMyId.asObservable().subscribe(onNext:{[weak self] valueChange in
print(valueChange)
}).disposed(by: DisposeBag())
anotherAccount.classListenerOfMyName.asObservable().subscribe(onNext:{[weak self] valueChange in
print(valueChange)
}).disposed(by: DisposeBag())
}
for handling delegations/callbacks you need to observe the changes by subscribing to the underlying Variables/Subjects/Observable.
Points to Note
Please use single dispose bag for one class, unlike me, i have disposed using DisposeBag() itself.
Read More about replacing delegates in RxSwift with Observables (this will be helpful)
I tried to create a custom iterator which returns wrapper abcContainer over raw data class abc
// raw data class
class abc {
var name : String = "";
init( _ value : String) {
name = value;
}
}
// with container, only "name" is to be visible
class abcContainer {
private var _abc : abc;
init( _ obj : abc) {
_abc = obj;
}
// + extra methods here
func getName() -> String {
return _abc.name
}
}
The point would be that the dictionary would return instances of abcContainer instead of just the plain raw abc class.
I wanted to use the sequence protocol to make the conversion automatic, but I was not able to transform the [String:abc] into [String:abcContainer] automatically like this:
// the iterator is implemented just iterating the inner basic dict
// but wrapping the result value as abcContainer
class abcIterator : Sequence, IteratorProtocol {
private var __source : [String:abc]?;
var index = 0
var myIterator : DictionaryIterator<String, abc>;
init(_ ctxArray: [String:abc]) {
self.__source = ctxArray
index = 0;
myIterator = (__source?.makeIterator())!
}
func next() -> abcContainer? {
let nextItem = myIterator.next();
if(nextItem != nil) {
return abcContainer((nextItem?.value)!);
}
return nil;
}
}
// this was supposed to be the wrapper over the collection
class abcCollection : Sequence {
private var __source : [String:abc]?;
init(_ list: [String:abc]) {
self.__source = list
}
func makeIterator() -> abcIterator {
return abcIterator(self.__source!);
}
}
I'm probably missing something very basic here. When I try to use the collection like this:
var dict : [String:abc] = [String:abc]();
dict["abba"] = abc("John Smith");
for (key,value) in abcCollection(dict) {
print(key, value.getName());
}
I get error: Expression type "abcCollection" is ambiguous without more context
Does anyone have idea how to make it work? What is missing? I have a feeling that this answer has the information I need...
Swift 2 to 3 Migration for Swift Sequence Protocol
The problem in your original code is that abcCollection(dict)
returned a sequence of abcContainer objects, and those cannot
be assigned to a (key, value) tuple.
You can achieve your goal with
class abcCollection : Sequence {
private var __source : [String:abc]
init(_ list: [String:abc]) {
self.__source = list
}
public func makeIterator() -> AnyIterator<(AnyObject,abcContainer)> {
let mapped = self.__source.lazy.map {
($0.key as AnyObject, abcContainer($0.value))
}
return AnyIterator(mapped.makeIterator())
}
}
Making __source non-optional makes all the (optional) unwrappings
redundant, and lazy.map { ... } returns a lazily evaluated
sequence of key/value pairs which is then type-erased.
Ok, perhaps the answer was abcIterator was not necessary, you could have defined the iterator directly just like done in the linked answer like this:
class abcCollection : Sequence {
private var __source : [String:abc]?;
init(_ list: [String:abc]) {
self.__source = list
}
public func makeIterator() -> AnyIterator<(AnyObject,abcContainer)> {
var it = self.__source?.makeIterator();
return AnyIterator {
let n = it?.next();
if n == nil { return nil }
return (n?.key as AnyObject, abcContainer((n?.value)!))
}
}
}
After that, the custom collection returned wrapped objects correctly.
How to access to static protocol method within a instance
I have a list of Contact, the contact can be a FamilyContact that inherit from Contact and the GroupStatus protocol
I want to call the static method from GroupStatus but in vain...
Here is my code
protocol GroupStatus {
static func isPrivate() -> Bool // static method that indicates the status
}
protocol IsBusy {
func wizzIt()
}
class AdresseBook {
private var contacts = [Contact]()
func addOne(c: Contact) {
contacts.append(c)
}
func listNonPrivated() -> [Contact]? {
var nonPrivateContact = [Contact]()
for contact in contacts {
// here is I should call the static method provided by the protocol
if self is GroupStatus {
let isPrivate = contact.dynamicType.isPrivate()
if !isPrivate {
nonPrivateContact.append(contact)
}
}
nonPrivateContact.append(contact)
}
return nonPrivateContact
}
}
class Contact : Printable {
var name: String
init(name: String) {
self.name = name
}
func wizz() -> Bool {
if let obj = self as? IsBusy {
obj.wizzIt()
return true
}
return false
}
var description: String {
return self.name
}
}
class FamilyContact: Contact, GroupStatus {
static func isPrivate() -> Bool {
return true
}
}
I can't compile Contact.Type does not have a member named 'isPrivate'
How can I call it ? It works if I delete the static keyword, but I think is more logical to define it static.
If I replace
let isPrivate = contact.dynamicType.isPrivate()
by
let isPrivate = FamilyContact.isPrivate()
It works, but I can have more than 1 subclasses
If I remove the static keywork I can do it by this way :
if let c = contact as? GroupStatus {
if !c.isPrivate() {
nonPrivateContact.append(contact)
}
}
But I want to keep the static keyword
This looks like a bug or a non-supported feature. I would expect that
the following works:
if let gsType = contact.dynamicType as? GroupStatus.Type {
if gsType.isPrivate() {
// ...
}
}
However, it does not compile:
error: accessing members of protocol type value 'GroupStatus.Type' is unimplemented
It does compile with FamilyContact.Type instead of GroupStatus.Type. A similar problem is reported here:
Swift 1.1 and 1.2: accessing members of protocol type value XXX.Type' is unimplemented
Making isPrivate() an instance method instead of a class method is
the only workaround that I currently can think of, maybe someone comes
with a better solution ...
Update for Swift 2 / Xcode 7: As #Tankista noted below, this has
been fixed. The above code compiles and works as expected in Xcode 7 beta 3.
type(of: contact).isPrivate()
This should work in recent Swift.
I'm trying to do the following.
protocol Vehicle {
}
class Car : Vehicle {
}
class VehicleContainer<V: Vehicle> {
}
let carContainer = VehicleContainer<Car>()
let vehicleContainer = carContainer as VehicleContainer<Vehicle>
But I get the compile error on the last line:
'Car' is not identical to 'Vehicle'
Is there any workaround for this?
Also I believe this type of casting should be possible because I can do it with Arrays which are built on generics. The following works:
let carArray = Array<Car>()
let vehicleArray = carArray as Array<Vehicle>
Expanding your array example quickly in playground works as intended
protocol Vehicle {
}
class Car : Vehicle {
}
class Boat: Vehicle {
}
var carArray = [Car]()
var vehicleArray : [Vehicle] = carArray as [Vehicle]
vehicleArray.append(Car()) // [__lldb_expr_183.Car]
vehicleArray.append(Boat()) // [__lldb_expr_183.Car, __lldb_expr_183.Boat]
Haven't done too much work with swift generics but looking quickly at the swift docs
struct Stack<T: Vehicle> {
var items = [Vehicle]()
mutating func push(item: Vehicle) {
items.append(item)
}
mutating func pop() -> Vehicle {
return items.removeLast()
}
}
and using vehicles instead of the generic type T
var vehicleStack = Stack<Vehicle>()
vehicleStack.push(Car())
vehicleStack.push(Boat())
var aVehicle = vehicleStack.pop()
appears to compile aok in an app (playground has some issues handling it though)
I would like to create objects based on their class passed into a function.
First, I have an interface every generatable object should conform to:
interface Generatable{
init(raw: NSDictionary)
}
and a function that would take the class as a parameter
func generateDynamicObjectFromClass(generatable: Generatable.Type){
var someJSONData : NSDictionary = NSDictionary()
var myGeneratedObject : Generatable = generatable(raw: someJSONData) //custom initialiser of Generatable class
}
and then, call it like that:
generateDynamicObjectFromClass(MyGeneratableObject.Type)
MyGeneratableObject class
class MyGeneratableObject : NSObject, Generatable{
init(raw: NSDictionary){
//some initialisation
}
}
However, MyGeneratableObject does not have a Type property, so the problem is to get the corresponding class of the underlying object during runtime. Is that possible ?
You have to define generateDynamicObjectFromClass as a generic function:
protocol Generatable {
init(raw: NSDictionary)
}
func generateDynamicObjectFromClass<T where T:Generatable>(generatable:T.Type, otherParam: NSString = "") -> T {
var someJSONData : NSDictionary = NSDictionary()
var myGeneratedObject = T(raw: someJSONData)
return myGeneratedObject
}
class MyGeneratableObject : NSObject, Generatable {
init(raw: NSDictionary){
println("MyGeneratableObject init")
}
}
var myObject1 = generateDynamicObjectFromClass(MyGeneratableObject.self, otherParam: "foo")
var myObject2 = generateDynamicObjectFromClass(MyGeneratableObject.self)
Alternatively, you can create the object as
var myObject = MyGeneratableObject(raw: NSDictionary())
without the need for a separate function.