Copy fields between similar classes swift - ios

I have two classes that are same but are present in different places. One inside framework and one in Normal App classes. I want to copy all the fields between them.
class Account {
var prodCode;
var subProdCode;
}
Same class inside the framework.
class Account {
var prodCode;
var subProdCode;
}
I need all fields to be copied from Object of 1st class to Object second class.
What is the best and easy way to do in Swift?

Add a method to MyAccout to update values of properties in your class, which accepts FrameworkAccount as parameter.
class MyAccout {
//...
func updateValues(account: FrameworkAccount) {
prodCode = account.prodCode
subProdCode = account.subProdCode
}
}

Related

ios swift models structure with realm database

I am developing my iOs App and I am using Realm database. As I am totally new to ios developing (also swift and xcode) I have question about structuring data (I've already read some general project structure guidelines but couldn't find the answer). My thinking is connected with Java structures
For Realm databases (RealmSiwft) I created a model like this:
#objcMembers class Patient: Object {
dynamic var patientId:Int = 0
dynamic var refNumber:String = ""
convenience init(id:Int, refNumber:String){
self.init()
self.patinetID = id
self.refNumber = refNumber
}
}
Now, it looks just like a POJO class in Java. But as I learned, this model structure is made that way so it can be able to use Realm.
So the question is, if I need somewhere else in my project to use Patient objects, is this Realm-POJO-model good to use? I mean, should I use it just like a normal Model even when I dont need to make database operations on it? Or should I make this Realm model alike DAO class for databases operations and make another model class like Patient.swift for whenever I want to play with Patient without using databases (I hope not, cause it's so much code duplicating)
And what if I need variables in that Patient Model that won't be stored in database? Can I make it without dynamic? What about init than? That blows my mind, as far as I learn swift it seems so ugly and unstructured, or I just can't switch to it yet...
if I need somewhere else in my project to use Patient objects, is this
Realm-POJO-model good to use?
even when I dont need to make database operations on it?
You can use your Patient object without savings to the DB, move them to different controllers and so on.
what if I need variables in that Patient Model that won't be stored
in database?
Look to ignoredProperties() method.
Can I make it without dynamic?
No you can't because of Realm based on Objective-C object, so this is necessary type.
What about init than?
You can create different Constructors methods, look to the Initialization doc. In case with Realm you should setup values to noticed variables (if you don't give them Default Property Values)
Your class should look like this:
class Patient: Object {
// MARK: - Properties
#objc dynamic var patientId: Int = 0
#objc dynamic var refNumber: String = ""
// MARK: - Meta
// to set the model’s primary key
override class func primaryKey() -> String? {
return "patientId"
}
//Ignoring properties
override static func ignoredProperties() -> [String] {
return ["tmpID"]
}
//It's ok
convenience init(id:Int, refNumber:String){
self.init()
self.patientId = id
self.refNumber = refNumber
}
}
All other detail information you can find in: realm docs
Also you can extend you base code with swift extension:
extension Patient {
var info: String {
return "\(patientId) " + refNumber
}
func isAvailableRefNumber() -> Bool {
return refNumber.length > 6
}
}

Proper way to pass multiple values with protocols in iOS

So I have two ViewControllers. First (MapVC) with map and second (SettingsVC) with many settings that need to be applied to this map.
I thought it would be nice idea to create protocol like
protocol MapSettingsDelegate: class {}
I know that I can specify function inside this protocol. But how I should do it when I have many settings - how should I pass them from SettingsVC to MapVC.
Example:
struct MySettings {
var value1: String
var value2: String
// and so on...
}
protocol MapSettingsDelegate: class {
func settingsUpdated(newSettings: MySettings)
}
and implement it inside your controller
class MapVC : MapSettingsDelegate {
...
func settingsUpdated(newSettings: MySettings) {
// Update everything you need
}
...
}
Feel free to ask for details

Save EVObjects with CoreData

I need to save some data with CoreData. Generally thats not a problem at all. The problem is, that the data is created with EVReflection an therefore inherits the class EVObject. To save the gathered data to CoreData they have to also inherit NSManagedObject. The problem is that swift does not allow you to inherit multiple classes. Would appreciate any tips.
class Device : EVObject
{
var channel : [Channel] = [Channel]()
var name : String = ""
var ise_id : Int = 0
var unreach : Bool = false
var sticky_unreach : Bool = false
var config_pending : Bool = false
override internal func propertyMapping() -> [(String?, String?)] {
return [("name", "_name"), ("ise_id", "_ise_id"), ("unreach", "_unreach"), ("sticky_unreach", "_sticky_unreach"), ("config_pending", "_config_pending")]
}
}
You don't have to inherit. You can extend them. Example:
class User : NSManagedObject{
#NSManaged .....
}
//Extension
import EVReflection
extension User : EVReflectable { }
https://github.com/evermeer/EVReflection#extending-existing-objects
Note I'm not aware of EVReflection but I think this answer can generally apply.
Don't use multiple inheritance. Have two separate classes and a mechanism for creating/loading/updating one object from the other. Protocols may allow it to be done in a way that minimises translation boilerplate (possibly using valueForKey(_:) and setValue(_:forKey) if you can know the key names in a safe manner.
It may not even be even be necessary to have an NSManagedObject subclass but just have an instance of NSManagedObject in all your classes that is loaded/created/saved as necessary.
It depends on what functionality you want to use from EVReflection. Since NSManagedObject also has NSObject as it's base class you could use most functions by just setting NSManagedObject as your base class instead of EVObject.
The only thing you have to do is instead of calling EVObject functions directly, you have to implement the code snippets that are in that EVObject method. Almost any function there is just a convenience method that will call the corresponding EVReflection function.
If you have any questions in the future, then please also report this as an issue on GitHub.

Hiding access to a parent's property in the subclass

I feel like this should be relatively simple but I can't find a way to accomplish it.
Let say I have
class Parent {
public var file: PFFile?
}
and a subclass
class Child : Parent {
// some functionality that hides access to super.file
}
Problem is I can't mess with the Parent class, but I don't want anyone using the Child class to have access to 'file'. How can I accomplish this in Swift?
Perhaps this one fix it:
class Parent {
public var file: PFFile?
}
class RestrictedParent : Parent {
private override var file: PFFile?
}
class Child : RestrictedParent {
// some functionality that hides access to super.file
}
Here in RestrictedParent, we can hide any functionality that should not be visible to any of the child class inheriting to it.
EDIT:
A part from doc:
class Car: Vehicle {
var gear = 1
override var description: String {
return super.description + " in gear \(gear)"
}
}
As per Swift documentation
You can prevent a method, property, or subscript from being overridden
by marking it as final. Do this by writing the final modifier before
the method, property, or subscript’s introducer keyword (such as final
var, final func, final class func, and final subscript).
so you can declare parent class as
class Parent {
final var file: PFFile?
}
Or if you cannot modify parent class then maybe this would work
class Child : Parent {
override private final var file: PFFile?
}
I don't think this is directly possible in Swift 3. Here is the best workaround I could come up with:
class Child : Parent {
override public var file: PFFile? {
get { return nil }
set { }
}
func exampleFunction() {
print(super.PFFile?.description) // Use like this
print(PFFile?.description) // Don't do this
//This ^^^ will ALWAYS return nil for PFFile
}
}
This is sort of a hack, but it does almost exactly what you want. It doesn't completely hide the variable from the outside world, but it makes it impotent. When you try and access file you always get nil, and when you try and assign a value to file it just does nothing. So the outside world still knows that Child has a property file of type PFFile?, but it just can't do anything about it.
Essentially, file is like an attractive movie star. You can send them letters, post on their facebook wall, comment on their instagram feed, but they will never reply to you. You know who they are, and what they look like, but you just can't do anything about it.
Then, inside Child you MUST use super.file EVERYWHERE you access Parent's file property. If you use file or self.file you will be trying to interact with the impotent version, and you will do nothing and get nothing every time! I have included exampleFunction() as an example of this usage. (I made up the description property on PFFile, just assume it is a String)
You can use final modifier,like this :
class Perent {
final var file: PFFile?
}
class Child: Perent {
// code here ...
}

Multiple Subclasses of PFObject

I have a class called Attendee that inherits from PFObject. Below is its basic definition.
class Attendee: PFObject, PFSubclassing {
override class func initialize() {
var onceToken : dispatch_once_t = 0;
dispatch_once(&onceToken) {
self.registerSubclass()
}
}
class func parseClassName() -> String! {
return "Attendee"
}
}
I want to create a subclass of this object called Speaker such that Speaker inherits from Attendee, which in turn inherits from PFObject.
My question is, will I have to implement the same initialize() and parseClassName() functions for Speaker?
To Parse, Attendee and Speaker are two different class without inheritance. Maybe you should do something like:
You should implement initialize() and parseClassName() for every classes.
Your classes in code should duplicate the classes on the server to avoid confusion both for you and for the Parse SDK code. Any functionality you want to share between the classes should be moved out into another class that you can instantiate / run as required.
The only way it could reasonably work is if your subclass only contained methods, no data. But even then, Parse would either not return the correct class to you, or it would return it at different times than you might expect.
You can absolutely do this on the client-side. If you are trying to store them separately, just return a different value in parseClassName() on each object.

Resources