Saturday, October 25, 2014

Difference between objectForKey and valueForKey in iOS

You may get this doubt when you work with NSDictionary in iOS. When you use these methods on Dictionaries, there wont be any much difference between objectForKey and valueForKey in iOS. Both methods behaves almost same on dictionaries. But valueForKey used on Key Value Coding (KVC) as its a key value method.

Where to use: objectForKey will work on NSDictionaries and valueForKey will work on NSDictionaries and KVC. On NSDictionaries both returns null if key not found. And valueForKey throws valueForUndefinedKey: exception on KVC if property not found.  Check the examples below for more clarity.

Usage on NSDictionary: We can use objectForKey: and valueForKey:  on dictionaries. Below is the sample code.

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"king",@"name",@"9985396338",@"phone", nil];
    // using objectForKey method
    NSLog(@"name is %@", [dict objectForKey:@"name"]);
    // displays null as name1 not found in NSDictionary
    NSLog(@"name1 is %@", [dict objectForKey:@"name1"]);

    // using valueForKey method    
    NSLog(@"phone is %@", [dict valueForKey:@"phone"]);
    // displays null as phone1 not found in NSDictionary
    NSLog(@"phone is %@", [dict valueForKey:@"phone1"]);

Usage on KVC: we can't use objectForKey: method on key-value coding. We can use valueForKey: method. Below is the example

// .h file
@interface valueForKeyTest : NSObject
@property NSString *name,*phone;

// .m file
@implementation valueForKeyTest
@synthesize name, phone;

// main.m file
#import "valueForKeyTest.h"
int main(int argc, const char * argv[]) {
    valueForKeyTest *obj = [[valueForKeyTest alloc] init]; = @"king"; = @"9985396338";
    // valueForKey on accessor methods
    NSLog(@"name is %@", [obj valueForKey:@"name"]);
    NSLog(@"phone is %@", [obj valueForKey:@"phone"]);

    // this will throw valueForUndefinedKey: as name1 is not a property
    NSLog(@"name is %@", [obj valueForKey:@"name1"]);

Key Value Coding: A key is unique value, which identifies the property value of the object. A key generally accessor method or instance variable in the object, Check KVC for more info. KVC has below key value methods.

  • setValue:forKey:
  • valueForKey:
  • setValue:forKeyPath:
  • valueForKeyPath: 
  • setValuesForKeysWithDictionary:
  • dictionaryWithValuesForKeys:

Wednesday, October 15, 2014

tree load error - jasperserver

When you are working with Jasper reporting server, you often face tree load error for the jasper server. I faced this error lot of times and I could not find the root cause for this. But we can fix this problem by clearing the cookies in the browser. 
Below are the steps to clear the cookies for the jasper server in the firefox browser.

  • Go to setttings/Preferences in the firefox browser
  • Go to Privacy tab
  • Click on 'remove individual cookies'
  • Search for localhost or jasper server ip address. See sample image below (for me its local host)
  • Click on 'Remove cookie' button (remove until all localhost cookies are removed)

I have specified for FireFox browser, if you are using other browsers like Chrome, IE you need to follow similar process to clear the individual cookies.

Unable to load metadata for VDB name - Jasper 5.6

When we use Virtual Data Source (VDS) in Jasper Reporting Server 5.6, we will face 'Unable to load metadata for VDB name' error. This is due to unable to handle the relation/mapping between the databases with Foreign keys. We need to specify it manually. Below are the sequence of steps for doing that.
  • Open the file applicationContext-VirtualDatasource.xml 
    • Path jasperreports-server-5.6/apache-tomcat/webapps/jasperserver-pro/WEB-INF
  • Search for translatorConfigList property in the file
  • Add property importPropertyMap after translatorConfigList (check below for property code)
  • Restart the server and it will work.

Below is the property code you need to add in the file:

<property name="importPropertyMap">
    <entry key="dw">
        <entry key="importer.importKeys" value="false"/>
        <entry key="importer.importForeignKeys" value="false"/>
        <entry key="importer.importIndexes" value="false"/>
        <entry key="importer.importStatistics" value="false"/>

In the above code, key value is 'dw' is a name of the database which we set while creating the virtual data source

P.S: In Jasper server 5.5 , you need to restart the server for the most of the cases if you face this error.

Popular Posts