Creating new data type!

Discussion in 'Mac Programming' started by saleh.hi.62, Dec 22, 2011.

  1. macrumors member

    Joined:
    Jul 25, 2011
    #1
    Hello guys,

    I need to create new data type with a NSDictionary and then when i declare the object be able to work with that with its functions ,look at this code

    this code is doing what i want

    .h file
    Complete code
    Code:
    @interface TFHppleElement : NSObject {
    @private
      
      NSDictionary * node;
      TFHppleElement *parent;
    }
    
    .m file
    Complete Code
    Code:
    @interface TFHppleElement ()
    @property (nonatomic, retain, readwrite) TFHppleElement *parent;
    @end
    
    @implementation TFHppleElement
    @synthesize parent;
    
    and for using it's like this:

    Code:
    TFHppleElement * element = [elements objectAtIndex:0];
    here it is assigning an NSDictionary which is a the forst element of array.

    i wanna know how i can make a data type like this and what it is called?
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    I really can't tell what you want here. What type is elements? And what does that snippet have to do with your goal? You can put any NSObject in an NSArray.

    Are you wanting to pass messages that are passed to a TFHppleElement to be passed through to its NSDictionary ivar? You can do it with this:
    http://developer.apple.com/library/...ObjCRuntimeGuide/Articles/ocrtForwarding.html

    You can override forwardInvocation:, ask your NSDictionary if it responds to the message with respondsToSelector:... if so pass it on, if not call the super implementation.

    If your intent is different explain it more clearly.

    -Lee
     
  3. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #3
    Sometimes I wonder if your posts are trolling.

    Umm... a linked list that is optimized for being traversed in reverse direction?
     
  4. thread starter macrumors member

    Joined:
    Jul 25, 2011
    #4
    No, that was all an example, i just want to make 1 like this
    Code:
    TFHppleElement * element = [elements objectAtIndex:0];
    TFHppleElement here appeared as a object that receive a NSDictionary, the matter is how can i design like that for my own matter?
     
  5. macrumors 68040

    Joined:
    Apr 22, 2005
    #5
    I don't even think you understand what this code
    Code:
    TFHppleElement * element = [elements objectAtIndex:0];
    
    actually does. All it is doing is getting one instance of TFHppleElement from an array of such elements. It has nothing to do with NSDictionary.

    Why don't you tell us, without mentioning TFHppleElement at all, what you are trying to do.
     
  6. macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    You could make a dictionary of dictionaries, instead of making a wrapper around NSDictionary. If you want to link several of these objects you will need to create methods that take care of the linking, allocation, traversal of the data structure as well as unlinking and releasing of the internal objects.
     
  7. thread starter macrumors member

    Joined:
    Jul 25, 2011
    #7
    ok i just wrote what i had in mind

    .h file

    Code:
    @interface ICItem : NSObject
    {
    	NSMutableDictionary *Item;
    	
    }
    @property (assign)NSMutableDictionary *Item;
    
    -(void)setFieldAndValue:(NSString*)field:(NSString*)value;
    -(NSString*)getValueForField:(NSString*)field;
    -(NSArray*)allfields;
    -(NSArray*)allvalues;
    -(void)delElementFromItem:(NSString*)field;
    
    
    @end
    
    .m file

    Code:
    @implementation ICItem
    @synthesize item;
    
    -(id)init{
    if (self=[super init]) {
    	item=[NSMutableDictionary dictionary];
    	
    	}
    	return self;
    }
    -(void)setFieldAndValue:(NSString*)field:(NSString*)value{
    
    	[item setObject:value forKey:field];
    
    }
    -(NSString*)getValueForField:(NSString*)field{
    	return [item objectForKey:field];
    
    }
    
    -(NSArray*)allfields{
    	
    	return [item allKeys];
    }
    
    -(NSArray*)allvalues{
    	
    	return [item allValues];
    }
    
    -(void)delElementFromItem:(NSString*)field{
    	
    	[item removeObjectForKey:field];
    
    }
    
    
    @end


    what i want to do is to do :
    NSMutableDictionary *ns=[[NSMutableDictionary alloc] init];
    [ns setObject:mad:"saleh" forKey:mad:"name"];
    [ns setObject:mad:"student" forKey:mad:"occupation"];

    ICItem *item=ns;

    [ns allfields];
    this is the operation that i would like to implement.
     
  8. gnasher729, Dec 23, 2011
    Last edited: Dec 23, 2011

    macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
     
  9. thread starter macrumors member

    Joined:
    Jul 25, 2011
    #9
    :eek:
     
  10. macrumors 68040

    Joined:
    Apr 22, 2005
    #10
    Aside from the bug that gnasher pointed out, what is the point of wrapping the NSDictionary in another class? You aren't adding any functionality to it at all. Do you plan to add more data or methods to that class?
     
  11. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #11
    Well are you doing any of it?? You can mock people for telling you to do the same things over and over, but you know what? We just don't see the quality of your programming or questions changing.
     
  12. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #12
    I've come to the conclusion that OP has no idea what he wants to accomplish, let alone any idea how to accomplish it.
     

Share This Page