Skip to content

Instantly share code, notes, and snippets.

@hamada147
Created July 10, 2018 15:37
Show Gist options
  • Save hamada147/d067e3fb9ec4213c907b2611ff6f7b59 to your computer and use it in GitHub Desktop.
Save hamada147/d067e3fb9ec4213c907b2611ff6f7b59 to your computer and use it in GitHub Desktop.
Error in this code happen inside Infaragistics Framework -[IGGridViewColumnDefinition resolveValueForObject:inDataSource:] + 112. I'm creating an NSObject in runtime and setting the datasource value to the retuned NSArray of getArrayOfRecrodsFrom function. If I commented the lines indicated in the function the code runs fine with no issues but w…
{
"replyCode": "0",
"replyMessage": "Succcess",
"responseDetails": [
{
"unrealizedGainLossPercentage": "15.23",
"marketValueProductCurrency": "131223845.03",
"marketValueSAR": "131223845.03",
"unrealizedGainLoss": "3170616.28",
"totalCost": "128053228.75",
"marketName": "Saudi Market",
"allocationPercentage": "40.96",
"currency": "SAR"
},
{
"unrealizedGainLossPercentage": "6.23",
"marketValueProductCurrency": "27888382.02",
"marketValueSAR": "104581301.86",
"unrealizedGainLoss": "1288382.02",
"totalCost": "26600000",
"marketName": "Dubai Financial Market",
"allocationPercentage": "32.66",
"currency": "SAR"
},
{
"unrealizedGainLossPercentage": "35.01",
"marketValueProductCurrency": "76165664.43",
"marketValueSAR": "76165664.43",
"unrealizedGainLoss": "1165664.43",
"totalCost": "75000000",
"marketName": "Kuwait",
"allocationPercentage": "23.78",
"currency": "SAR"
}
]
}
{
"replyCode": "0",
"replyMessage": "Succcess",
"responseDetails": [
{
"order": "1",
"fieldType": "TEXT",
"columnName": "Market Name",
"fieldName": "marketName"
},
{
"order": "2",
"fieldType": "TEXT",
"columnName": "Currency",
"fieldName": "currency"
},
{
"order": "3",
"fieldType": "PRICE",
"columnName": "Total Cost",
"fieldName": "totalCost"
},
{
"order": "4",
"fieldType": "PRICE",
"columnName": "Market Value Product Currency",
"fieldName": "marketValueProductCurrency"
},
{
"order": "5",
"fieldType": "PRICE",
"columnName": "Market Value SAR",
"fieldName": "marketValueSAR"
},
{
"order": "6",
"fieldType": "PRICE",
"columnName": "Unrealized Gain/Loss Value",
"fieldName": "unrealizedGainLoss"
},
{
"order": "7",
"fieldType": "PRICE",
"columnName": "Unrealized Gain/Loss Value %",
"fieldName": "unrealizedGainLossPercentage"
},
{
"order": "8",
"fieldType": "PRICE",
"columnName": "Allocation %",
"fieldName": "allocationPercentage"
}
]
}
@implementation MainWidgetViewController
- (NSArray *)getArrayOfRecrodsFrom: (NSArray *)response {
NSMutableArray * rows = [[NSMutableArray alloc] init];
if ([response count] != 0) {
// 1. get all NSDictionary keys
NSDictionary * temp = response[0];
NSArray * keys = [temp allKeys];
if (!NSClassFromString(self.ServiceName)) {
// 2. create a class
self.ModelClass = objc_allocateClassPair([NSObject class], [self.ServiceName UTF8String], 0);
// 3. all class variables with the same name as key retrieved from NSDictionary
for (NSString * key in keys) {
const char * name = [key UTF8String];
class_addIvar(self.ModelClass, name, sizeof(id), rint(log2(sizeof(id))), @encode(id));
objc_property_attribute_t type = {"T", "@\"NSString\""}; // type
objc_property_attribute_t ownership1 = {"&", "" }; // stong
objc_property_attribute_t ownership2 = {"N", "" }; //
objc_property_attribute_t backingivar = {"V", name}; // name
objc_property_attribute_t attrs[] = {type, ownership1, ownership2, backingivar};
unsigned int attrCount = sizeof(attrs)/sizeof(attrs[0]);
class_addProperty(self.ModelClass, name, attrs, attrCount);
}
// 4. register a class to be used
@try {
objc_registerClassPair(self.ModelClass);
} @catch (NSException * e) {
NSLog(@"Exception in class registeration: %@", e);
}
} else {
self.ModelClass = NSClassFromString(self.ServiceName);
}
for (NSDictionary * curr in response) {
// create object
id MC = [[self.ModelClass alloc] init];
for (int i = 0, size = (int)[keys count]; i < size; i++) {
NSString * newValue = [[NSString alloc] init];
NSString * type = [self getColumnType: keys[i]];
// If commented the following 5 lines
if ([type isEqualToString:@"PRICE"]) {
newValue = [NSString stringWithFormat:@"%.02f", [[curr objectForKey: keys[i]] doubleValue]];
} else {
newValue = [curr objectForKey: keys[i]];
}
// until here comment
// set values for Ivar
const char * name = [keys[i] UTF8String];
Ivar CurrVar = class_getInstanceVariable(self.ModelClass, name);
object_setIvar(MC, CurrVar, [newValue copy]);
}
// add object to array
[rows addObject:MC];
}
}
return rows;
}
- (NSString *)getColumnType:(NSString *)colName {
for (TableColumn * col in self.TableColumns) {
if ([colName isEqualToString:col.fieldName]) {
return col.fieldType;
}
}
return @"TEXT";
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment