Value passing problem from once class to another

Discussion in 'iOS Programming' started by sagarshivam, Aug 31, 2011.

  1. sagarshivam macrumors member

    Joined:
    May 24, 2011
    #1
    Dear All

    I am able to assign values to NSarray timeZoneNames defined in RootViewController.m from SimpleTableViewAppDelegate.m and then I can use 'timeZoneNames' inside RootViewController.m perfectly.

    But When the same procedure I follow for SubTable.m (where Nsarray timevalue is defined), a null array is assigned in SubTable.m

    Codes are :

    SimpleTableViewAppDelegate.m

    Code:
    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    	
    	RootViewController *rootViewController = [[RootViewController1 alloc] initWithStyle:UITableViewStylePlain];
    	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    	
    SubTable *sub = [[SubTable alloc] initWithStyle:UITableViewStylePlain];
    NSArray *timeZones = [NSTimeZone knownTimeZoneNames];
    
    rootViewController.timeZoneNames = [timeZones sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    	
    	
      NSArray *test_array = [[NSArray alloc] initWithArray:timeZones];
      sub.timevalue = [test_array copy];
    	//NSLog(@"count is %d", [sub.timevalue count]);
    	
    UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    	self.navigationController = aNavigationController;
    	[aNavigationController release];
    	[rootViewController release];
    	[window addSubview:[navigationController view]];
    	[window makeKeyAndVisible];
    }
    
    
    RootViewController.h is like:

    Code:
    @interface RootViewController : UITableViewController {
    	NSArray *timeZoneNames;
    }
    
    @property (nonatomic, retain) NSArray *timeZoneNames;
    
    @end
    
    And inside RootViewController.m ,
    Code:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    	NSLog(@"timezonesname count is %d", [timeZoneNames count]);
    	return [timeZoneNames count];
    }
    
    In above case, correct value is getting displayed.

    While
    SUbTable.h looks like:

    Code:
    @interface SubTable: UITableViewController {
    	
    	NSArray *timevalue;
    
    }
    @property (nonatomic ,retain) NSArray *timevalue;
    @end
    and Inside SUbTable.m
    Code:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    	NSLog(@"timevalue count is  %d", [timevalue count]);
        return [timevalue count];
    }
    And here value displayed is always 0.

    Where exactly I am making the mistake? What should I do so that timevalue can be assigned in the same way timeZoneNames is assigned.
     
  2. Sykte, Aug 31, 2011
    Last edited by a moderator: Aug 31, 2011

    Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #2
    How and where are you initializing SubTable and timevalue;
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    This code contains several bugs.

    First, you're leaking objects.

    Second, you're creating a SubTable instance, and setting its timevalue property, but you're then not doing anything to keep that instance. That means it's leaking, but more important, you're not actually setting the timevalue property of any other instance of SubTable. This suggests a fundamental misunderstanding of the difference between a class and an instance of a class.

    When you execute [SubTable alloc], you are given a new and distinct object, different from every other object. The new object is an instance of the SubTable class, but it is not the SubTable class itself. So when you set the timevalue property of that SubTable instance, it's applied only to that instance. Every other SubTable instance in existence (or created thereafter) has no way of seeing or accessing that timevalue array.

    Consider another class, such as NSMutableArray, for example. If you alloc and init an NSMutableArray, you get a new and distinct object. That object is an instance of NSMutableArray, but it isn't the class itself. If you add an object, say an NSNumber, to that instance, it only affects that array instance. It does not add the NSNumber to every NSMutableArray instance.

    I think you need to go back and study the fundamentals. Focus on the difference between a class and instances of a class.

    I also think you need to restudy memory management, to avoid leaking objects (the SubTable instance isn't the only leak). And remember you can run your program under Instruments and look for leaks. I suggest doing that regularly, not just when you think you have a leak. Preventive action is often easier than corrective action.
     

Share This Page