Assignment of dynamic class type - ios

I have a dictionary that contains a Class string name as the key and an array of corresponding class objects as the value.
It could be a number of different Class types that come in, so I wanted to do dynamic assignment at runtime.
Does anyone know why this code gives a compiler error?
// Where obj is an object of type MyClass
Class myClass = NSClassFromString(#"MyClass");
myClass *objectOfTypeMyClass = obj;
Update:
Here's how i ended up implementing it:
Class interestClass = NSClassFromString(classProvidedAsString);
id interest = [interestClass createNewInterestUsingManagedObjectContext:backgroundContext];
[interest setValue:title forKey:#"title"];
[interest addLikedByObject:aFriend];
Where title is a property on all objects that I can accept, and createNewInterest is a method all objects have.
The problem was trying to cast id as interestClass to use the properties and methods of that class.

Why just don't you use id?
id objectOfTypeMyClass = obj;
Another option would be use polymorphism (if the classes were created by you).

You can do this way:
NSObject *object=[NSObject new];//this can hold any kind of object
object=#"my name is lakhan";//as string
NSLog(#"%#",object);
object=#[#(10),#(50)]; //now working as array
NSLog(#"%#",object);
object=#{#"k1": #"10", #"k2":#"20"}; // as dictionary
NSLog(#"%#",object);
So as per your requirement you can use whatever value you need to set on the object.

Related

How to list variables for NSManagedObject

I needs to list variables for NSManagedObject, I know there is a way to do it using "class_copyIvarList" as given in How do I list all fields of an object in Objective-C?
but "class_copyIvarList" isn't working on "NSManagedObject".
here is piece of code Im using, which is working perfectly fine for "NSObject" but not for "NSManagedObject":
unsigned int outCount;
Ivar *vars = class_copyIvarList([self class], &outCount);
for (int i = 0; i < outCount; i++) {
Ivar var = vars[i];
unsigned int idCount;
NSLog(#"%s %s", ivar_getName(var), ivar_getTypeEncoding(var));
}
free(vars);
What is wrong with it?
I'm not sure what you're doing here, but with managed objects it's usually more typical to use Core Data's own introspection rather than ask the Objective-C runtime. In a method on a managed object subclass, you'd use [[self entity] propertiesByName] to get a list of all attributes and relationships defined by the entity type. You could replace that method with attributesByName or relationshipsByName depending on what you need. The objects you get back can be further queried, for example to find out the type of a property or the target entity of a relationship.

Get whole object from NSMutableDictionary for current cellForRowAtIndexPath and assign it to a CustomCell

I have the following type of a NSMutableDictionary
(
{
id = 1;
key = value;
key = value;
},
{
id = 2;
key = value;
key = value;
}
)
It contains multiple data of an own Object. Now, in cellForRowAtIndexPath. I created a CustomCell that has a field CustomCell.customObject that should get this object. What I'm trying to do is the following. I want to assign the current entry of the NSMutableDictionary to this field.
Alternatively I could do this (and am doing it right now)
I'm getting the ID like this
NSString *objectId = [[dict valueForKey:#"id"] objectAtIndex:indexPath.row];
And then I'm loading the object from the database. The problem I'm seeing in this, is the doubled request. I mean, I already have the data in my NSMutableDictionary, so why should I request it again?
I don't want to just assign a certain key-value pair, I want to assign the whole current object entry of the NSMutableDictionary. How would I do this?
If the above code (dictionary) is used to show information on tableview there is a mistake in it dictionary should always starts with "Key" not with index. So better make the dictionary to array and then you will have index to get complete information in indexes write this code
NSLog(#"%#",[[newArray objectAtIndex:0]objectForKey:#"id"]);
Hope this will help..

Save int variable to Parse Object

I have a variable int and i want to save this to a PFObject. Whenever I try I get the error:
incompatible integer to point value from int to id.
I can add a normal int which is not in a variable by doing.
PFObject* object = [PFObject objectwithclassname:#"test"];
object[#"score"] = #30;
This works fine, and also I can add a string and it will work. It is when I try this that it does not work.
int test = 10;
object[#"score"] = test;
Anyone know?
The syntax is:
object[#"score"] = #(test);
Variables of type int are no (Objective-C) objects. You use the keyed subscription protocol for assignment. It only takes objects. Basically this is done:
[object setObject:test forKeyedSubscript:#"score"]; // Error: test is no object
So the solution is to put the non-object typed int into an object (boxing). You can do this with rmaddy's solution (boxed expression) or more explicit for a better understanding:
object[#"score"] = [NSNumber numberWithInt:test];

Is it possible to map JSON fields to differently named properties using JAGPropertyConverter?

I'm using the excellent open source JAGPropertyConverter to deserialize JSON responses into a model object.
It's not explicitly documented, but is there a way I can have a JSON field named, say, "userName" map/deserialize into a property called "name"?
It utilizes Key-Value compliant properties, so the vanilla use case would for my model to have a property called "userName" if the JSON I was deserializing had a field called "userName".
As of 76829b4dca, the answer is no. JAGPropertyConverter invokes JAGPropertyFinder to map dictionary keys to properties, and neither class allows you to change the default mapping.
JAGPropertyConverter.m:
- (void)setPropertiesOf:(id)object fromDictionary:(NSDictionary*)dictionary {
JAGProperty *property;
for (NSString *key in dictionary) {
property = [JAGPropertyFinder propertyForName: key inClass:[object class] ];
...
}
...
}
JAGPropertyFinder.m:
+ (JAGProperty *)propertyForName:(NSString *)name inClass:(__unsafe_unretained Class)aClass {
objc_property_t property = class_getProperty(aClass, [name UTF8String]);
...
}

what is the different between identifier id and class?

id is the type of the object; the type could have been NSArray * or NSString *, for example.
Class can either be NSArray * or NSString * or other object.
I can use them like follow
- (void)wantWriteAction:(id)sender {
}
- (void)wantWriteAction:(Class)sender {
}
I want to know the different between them.In what conditions can't use id?what condition can't use Class?
id represents an instance of any class. Class represents any class-- not an instance, but the class itself.
So id could be a specific instance of NSString or NSArray. You can call instance methods on it-- those which are shown with a - in the documentation, like -length or -stringByAppendingFormat. If the id is an NSString then its value might be #"foo".
Class could be the NSString class or the NSArray class. You can call class methods on it-- those which are shown with a + in the documentation, like +stringWithFormat. If the Class is NSString (note, not an NSString, but the NSString class) then it has no string value, because it's not a string. It's a class.

Resources