0

I am new to programming so can anyone here please let me know if the below is the valid in Objective-C. Thanks.

@interface MainViewController : UIViewController
{
  id iTempStore;
}

@property (nonatomic, assign) id iTempStore;

// FirstViewController

@interface FirstViewController : UIViewController
{
  MainViewController* pParent;
}

-(void) SomeFunction
{
  m_pParent = [[[MainViewController]alloc]init];

  NSString* pTest = [[[NSString alloc] initWithString:@"Test"]autorelease];
  // Is this valid way to store an object ???
  [m_pParent setITempStore: pTest];

  // Check Value
  NSString* pValue = [m_pParent iTempStore];
  NSLog(@"Value is: %@", pValue);// Value is: Test

  [m_pParent release];
}
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
newdev1
  • 279
  • 6
  • 15

2 Answers2

0

id can hold a reference to any object, so storing a string there is fine, but since you are backing with an iVar you probably want to use copy or retain rather than assign as your propery storage class... you would use copy if it were alway NSString or any class that has a mutable subclass... but since id can't specify that to be type safe you would either do :

@property(copy) id <NSCopying> iTempStore;

or

@propery(retain)  id iTempStore;
Grady Player
  • 14,399
  • 2
  • 48
  • 76
0

it's technically ok...but not very...good

  1. If you're declaring a property you don't necessary need it as instance variable too.
  2. Do not use non-ARC things ...ARC is supported on all ios devices ( not sure about first gen) but on all that really matter at least.
  3. If you know the object type you don't need to use id. id is used for when you are not sure of the returning type.

your code should look something like this :

@interface MainViewController : UIViewController

@property (nonatomic, assign) NSString* iTempStore;

// FirstViewController

@interface FirstViewController : UIViewController
{
  MainViewController* pParent;
}

-(void) SomeFunction
{
  m_pParent = [[MainViewController alloc]init];

  NSString* pTest = [[[NSString stringWithString:@"Test"];
  [m_pParent setITempStore: pTest];

  NSString* pValue = [m_pParent iTempStore];
  NSLog(@"Value is: %@", pValue);


}
skytz
  • 2,201
  • 2
  • 18
  • 23
  • Thanks for the reply skytz. Considering i am using first gen ios3 (have to) and want to store different type of objects at different stage of the application, is it okay to use id then? – newdev1 Apr 21 '13 at 16:14
  • @newdev1 again...if you really don't know what the type is...yea..it's ok..but if you do there is no point in using id (i know it's convenient..using id for all and covers every type possible but in the long run takes more memory, harder to maintain code...etc.). – skytz Apr 21 '13 at 16:18
  • skytz. Thanks...i need then @property (retain) id iTempStore; like Grady suggested? – newdev1 Apr 21 '13 at 16:39
  • yes ...for objects you want to keep...you should look that those mean anyway [here](http://stackoverflow.com/a/4511004/1270586) – skytz Apr 21 '13 at 16:47