Save int variable to Parse Object - ios

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];

Related

Getting an error while adding a integer to array object at index (objective c)

while adding a integer to object at index of an array, I am getting an error of "arithmetic on pointer to interface id which is not a constant size for this architecture and platform", didn't know how to resolve it.
Please help.
my code is -
if (arrayTotalAmount.count>0) {
int sum = 0;
for (int i = 0; i<=arrayTotalAmount.count; i++) {
sum = (sum+[arrayTotalAmount objectAtIndex:i]);
}
In 4th line I am getting that error.
Thanks
Objective C array only accepts NSObject type. That means it is impossible to insert a primitive value into an NSArray. You are getting an error because objectAtIndex method returns a pointer which points to that NSObject, the arithmetic operations are still valid on pointers but the thing is that the size of a pointer as integer (32bit, 64bit) may differ on device. So one of the solution is typecasting the pointer sum+(int)[arrayTotalAmount objectAtIndex:i] which makes no sense in your case.
The solution you are looking for is probably sum+[[arrayTotalAmount objectAtIndex:i] intValue] or similar. Assuming that array contains NSNumber objects. If the object inside an array is not an NSNumber then your app will fail in runtime showing an error indicating that an object X does not have a method called intValue in which case you will need to figure how to convert object X to your int.
You just need to convert your array object to integer and then add it will work for you.
if (arrayTotalAmount.count>0) {
int sum = 0;
for (int i = 0; i<=arrayTotalAmount.count; i++) {
sum = (sum+[[arrayTotalAmount objectAtIndex:i] intValue]);
}

Why is the value changing when I cast an int to an NSInteger?

I have an NSDictionary, and I perform objectForKey for key resource_type_id:
NSInteger resourceTypeID = [self.selectedDictionary objectForKey:#"resource_type_id"];
When I print resourceTypeID in the console, it returns 2 (the correct ID).
Now, I need to convert this NSInteger to an int to feed into my switch case statement.
Here is that code:
self.resourceTypeLabel.text = [ResourceType getResourceTypeFromResourceTypeID:(int)resourceTypeID];
However, when I do this, self.resourceTypeLabel.text returns nil.
Upon further inspection, when printing (int)resourceTypeID in the console, it returns 35.
Why is the number changing when I cast it?
-objectForKey: should return an object of type id, not an int. How are you setting the value?

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.

How do you put a variable in the array brackets in Xcode 5?

When I do an operation like this:
self.slider.value = randomArray[0][0];
I would like to be able to do this:
self.slider.value = randomArray[randomVariable][0];
Basically, how do you put that "randomVariable" in the brackets? When I try to do this on Xcode, I get:
Code: self.detail1.text = detailsForNotesUse[x][0];
Error:Expected Method to read dictionary element not found in object
of type 'NSArray *'
The variable I put in the brackets is NSString, the array is NSArray, and detail1 is a text field.
Declarations:
NSString *x = 0;
NSArray *detailsForNotesUse;
You've defined x as an NSString. You should define your index variable like this:
NSUInteger x = 0;
I'm just reposting the answer I gave in my comment below the question:
x needs to be an int if you're using it as an index. ex. int x = 0;
But I'm also writing to note that many of the answers are misleading. You can in fact access a nested array in this way, i.e. randomArray[x][y];, because if randomArray[x] returns an array (as is syntactically valid in obj-c), the items of that array can then be similarly accessed by appending [y] (though you may have to cast randomArray[x] to an NSArray to prevent a warning).

Assignment of dynamic class type

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.

Resources