How to transfer a Class to another Class in Objective-C? - ios

ForExample ,I Have a Class named PlayListModel, which have a properties:id . and I have other Classes like PlayModel_A or PlayModel_B ,which have many properties。
Now what I want is there have a method like:
- (PlayListModel*)transferAnClass:(id)anClass ToPlayListModel;
I want transfer other model to the uniform PlayListModel , than pass the PlayListModel to the player.
My problem is , the other model's source name is different. some is 'idField' and some is 'idsource' or something other . so the need to transfer them to the Playlist's property name :'id'(for example).
i tried to use run time , but i can't get the definitely name of class, they could be any modelClasses.
I can use [class Class] to know which class it is . but then I can't use Class.property . I don't know how to explain .

It sounds like what you want to do is subclass People..
#interface Student : People
//properties & method names here
#end
#implementation Student
//code here
#end

You can create a model of People separately and use object of that model in Student or Police
class People {
private var _age : Int = 0
private var _name : String = ""
}
and Student class as
class Student {
private var _student_id : Int = 0
private var _student_rollno : String = ""
let _people : People
}
if you need to send data you can directly pass object of Student or Police class.
You can also opt for inheritance.Inherit your People class in Student or Police class. You can access and modify properties of People class in Student or Police class easily.

Related

How to use generics in own created model class in iOS?

How to use generics in own created model class?
I have one FeatureListModel class and other have FavoriteModel class. Both store the same properties, the only difference is the different class model name.
I need to display model properties value in ProductDetail controller.
How could I manage this stuff using generics?
Here is my code (Swift 4.2):
1st Model: FavoriteListModel
class FavoriteListModel {
var categoryID: Int?
var item_name: String?
var MRP: String?
}
2nd Model: FeatureListModel
class FeatureListModel {
var categoryID: Int?
var item_name: String?
var MRP: String?
}
I have 8-10 more properties, but this is just some stuff in my code.
Controller - ProductDetailTableViewController
class ProductDetailTableViewController : UITableViewController {
var productDetails: FavoriteListModel!
var productFeatureList: FeatureListModel!
fileprivate func displayProduct() {
if productDetails != nil {
title = productDetails.item_name
categoryID = productDetails.categoryID!
}else if productFeatureList != nil {
categoryID = productFeatureList.categoryID!
title = productFeatureList.item_name
}
}
and in my Product Detail Table Controller, I am accessing model objects and display on the screen.
I don't want if-else check.
You are mixing up generics and protocols. In your case a protocol is preferable.
In ProductDetailTableViewController there is an object which responds to the getter of item_name (by the way please conform to the camelCased naming convention itemName) and categoryID. The type of the object as well as the existence of other properties and functions is not significant.
Create a protocol
protocol Listable {
var itemName : String { get }
var categoryID : Int { get }
}
Then adopt the protocol in your classes (do you really need a class?) and declare at least categoryID as non-optional since you are force unwrapping the value later anyway. Don't use optionals as an alibi not to write an initializer.
class FavoriteListModel : Listable { ...
class FeatureListModel : Listable { ...
In ProductDetailTableViewController rather than two properties declare one property as Listable and instead of objective-c-ish nil checking use optional binding:
var details: Listable!
fileprivate func displayProduct() {
if let productDetails = details {
title = productDetails.itemName
categoryID = productDetails.categoryID
}
}
What you have here is not a use case for the Generics. Generics are used when you have for example a function that does exact same thing but can be used with two different parameter types. That's when you use generics.
Another concept is super class (parent class or base class) which is used when you have a class with common properties and then other classes with those properties and then extra and different unique properties which in this case, each class subclasses the parent class.
What you have here is neither of them. A good architecture for this case is just a single model type (class or struct) and using two different collections (array or set) in your view controller.
You can also create a favorite class or featured class which holds an array with your models.

Get data from class with two array class Swift

I have a class with two array, i need this class to work with it in tableview so this is my code
class X {
private var abc: [Demo]!
private var def: [Project]!
init() {
}
init(abc:[Demo], def:[Project]) {
self.abc = abc
self.def = def
}
}
So how can i get access to class Demo and Class Project, i have already the data in class X
class Demo like this
class Demo {
private var nom:String
init(nom:String) {
self.nom = nom
}
and class Project like this
class Project {
private var title:String
init(title:String){
self.title = tile
}
You haven't posted the code where you're trying to access the data, but your problem may be that you have declared nom and title as private which makes them inaccessible to other classes. Try deleting private.
If you just want to prevent other classes from changing them, you can change private to public private(set).
what 's mean of "get access"?
If your property in class Demo and Project is private , so you must have a get function to access to property in it

The use of singleton class in objective c and swift

I have a project implemented in both Objective C and Swift classes and I need to have global variables to be shared among these classes .
I have two variables currentUsername and currentUsernumber , I need to use these two in each view in the project, what is the best way to implement that ?
I have tried to implement singleton class and here is my code :
class curentUserSingleton {
static var instance: curentUserSingleton!
var currentUsername: String = ""
var currentUsernumber: String = ""
// SHARED INSTANCE
class func sharedInstance(Name : String , Number : String) -> curentUserSingleton {
self.instance = (self.instance ?? curentUserSingleton(uName: Name , uNumber: Number))
return self.instance
}
// METHODS
init(uName : String , uNumber : String) {
self.currentUsername = uName
self.currentUsernumber = uNumber
}}
But I don't know how to use this class safely in the OC and Swift and I am a little confused since I get declaration errors when I use the class in my code!
Is this the right way to write a singleton class and how to call it in both languages ?
I'd be inclined to do something like:
class User: NSObject {
static let sharedInstance = User()
var name: String?
var number: String?
}
Then you can set and retrieve name and number like so in Swift:
User.sharedInstance.name = "Foo"
User.sharedInstance.number = "42"
print(User.sharedInstance.name)
print(User.sharedInstance.number)
Obviously, to access this from Objective-C .m file, you have to have to include the system generated header like so:
#import "ModuleName-Swift.h" // obviously, replace `ModuleName` with the name of your module
But then the syntax for setting and retrieving these properties is similar as it was in Swift:
[User sharedInstance].name = #"Foo";
[User sharedInstance].number = #"42";
NSLog(#"%#", [User sharedInstance].name);
NSLog(#"%#", [User sharedInstance].number);
To me it seems you do not need a singleton at all. I suggest you would be best of redesigning the architecture to have a user class that can store the information you are needing (and more if you finds the need in the future).
Then you could either pass that user object around between the view controllers as they need or perhaps easier define a currentUser property for the app delegate class.
That way each view controller can obtain the app delegate from the NSApp global reference to the application object and then get the current user object from there. With this pattern the app delegate acts as the globally accessible singleton you need without any need to manage it yourself.

Criteria that filters by subclass of filed declared by super class in domain class

i have the following domain class
class Session{
static hasMany=[lessons:Lesson]
}
class BasicSession extends Session{
}
class AdvancedSession extends Session{
}
know that Lesson is also a domain class:
class Lesson {
static belongsTo=[session:Session]
}
What's the Criteria that retrieves all lessons that belongs to Session subclass (BasicSession or AdvancedSession)
if i want to explain what i mean , i can write :
// lessons belong only to AdvancedSession
Lesson.createCriteria().list{
session{
eq('class','slm.abdennour.AdvancedSession') // !!!
}
}
After consulting this Issue, the solution is as what i said in question but , instead of String type , use Class type .
That it means :
eq('class',slm.abdennour.AdvancedSession)
and not
eq('class','slm.abdennour.AdvancedSession')

Grails: How to define a domain property rendered on views but not persisted?

I have this domain classes, let's say:
class Person {
String name
Integer age
//car data that needs to be shown and filled in views
//but not persisted in Person class
String model
String color
static afterInsert = {
def car = new Car(model: model, color: color)
car.save()
}
}
class Car {
String model
String color
}
What I need is to show in my Person views (create and edit) the model and color properties that are defined inside Person class but these doesn't have to be persisted with this class. These data, model and color, have to be persisted using the Car domain class maybe using the afterInsert event. In other words, I need to save data from a domain class using the views from another domain class.
Thanks in advance.
You can use transients on properties you want GORM to ignore, for example
class Person {
static transients = ['model', 'color']
String name
Integer age
//car data that needs to be shown and filled in views
//but not persisted in Person class
String model
String color
..
}
Just curious but is there a reason you're not using associations
class Person {
..
static hasMany = [cars: Car]
}
class Car {
..
static belongsTo = [Person]
static hasMany = [drivers: Person]
}
.. or composition
class Person {
Car car
}
or simply data binding with multiple domains
//params passed to controller
/personCarController/save?person.name=John&age=30&car.model=honda&car.color=red
//in your controller
def person = new Person(params.person)
def car = new Car(params.car)

Resources