PDA

View Full Version : Creating new data type!




saleh.hi.62
Dec 22, 2011, 09:35 PM
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 (https://github.com/topfunky/hpple/blob/master/TFHppleElement.h)

@interface TFHppleElement : NSObject {
@private

NSDictionary * node;
TFHppleElement *parent;
}


.m file
Complete Code (https://github.com/topfunky/hpple/blob/master/TFHppleElement.m)
@interface TFHppleElement ()
@property (nonatomic, retain, readwrite) TFHppleElement *parent;
@end

@implementation TFHppleElement
@synthesize parent;


and for using it's like this:

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?



lee1210
Dec 22, 2011, 11:13 PM
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/mac/documentation/Cocoa/Conceptual/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

jared_kipe
Dec 22, 2011, 11:41 PM
...
i wanna know how i can make a data type like this and what it is called?

Sometimes I wonder if your posts are trolling.

Umm... a linked list that is optimized for being traversed in reverse direction?

saleh.hi.62
Dec 23, 2011, 12:19 AM
Sometimes I wonder if your posts are trolling.

Umm... a linked list that is optimized for being traversed in reverse direction?

No, that was all an example, i just want to make 1 like this
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?

admanimal
Dec 23, 2011, 12:34 AM
I don't even think you understand what this 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.

subsonix
Dec 23, 2011, 01:16 AM
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.

saleh.hi.62
Dec 23, 2011, 03:17 AM
ok i just wrote what i had in mind

.h file

@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

@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:@"saleh" forKey:@"name"];
[ns setObject:@"student" forKey:@"occupation"];

ICItem *item=ns;

[ns allfields];
this is the operation that i would like to implement.

gnasher729
Dec 23, 2011, 03:42 AM
[CODE]-(id)init{
if (self=[super init]) {
item=[NSMutableDictionary dictionary];

}
return self;
}

I noticed that either you set the warning levels in your compiler very low, or you are ignoring warnings. Both are very bad. The compiler can give you valuable information. Almost every single time that the compiler gives me a warning it turns out to be a bug that I fix within five seconds instead of wasting hours finding it.

At previous times, people told you how to find problems and you wouldn't believe that they knew what the problem was. Look carefully at the code that I quoted and try to figure out how I know that you don't get warnings or ignore them. Then you should really spend an hour to find out how to get warnings from the compiler, and then remove all warnings from your code.

Oh my god. I had seen the thing that should give a warning. But there is also a really big bummer in your code that will crash it very soon. Learn about memory management. Learn how to use that static analyzer. There is a huge bug in your code, and if you spent the time setting up XCode properly it would tell you about it as soon as you build your code. You are too ignorant to learn your tools, and waste ten times more time finding problems than it would cost you to learn your tools.

By the way: My company is outsourcing some projects. From your posts I conclude that you are not involved in one of those projects, and believe me, I would know. And believe me, if you were involved in one of those projects, my manager's manager would be talking to your manager's manager and would be asking what the hell is going on.

saleh.hi.62
Dec 23, 2011, 04:27 AM
I noticed that either you set the warning levels in your compiler very low, or you are ignoring warnings. Both are very bad. The compiler can give you valuable information. Almost every single time that the compiler gives me a warning it turns out to be a bug that I fix within five seconds instead of wasting hours finding it.

At previous times, people told you how to find problems and you wouldn't believe that they knew what the problem was. Look carefully at the code that I quoted and try to figure out how I know that you don't get warnings or ignore them. Then you should really spend an hour to find out how to get warnings from the compiler, and then remove all warnings from your code.

Oh my god. I had seen the thing that should give a warning. But there is also a really big bummer in your code that will crash it very soon. Learn about memory management. Learn how to use that static analyzer. There is a huge bug in your code, and if you spent the time setting up XCode properly it would tell you about it as soon as you build your code. You are too ignorant to learn your tools, and waste ten times more time finding problems than it would cost you to learn your tools.

By the way: My company is outsourcing some projects. From your posts I conclude that you are not involved in one of those projects, and believe me, I would know. And believe me, if you were involved in one of those projects, my manager's manager would be talking to your manager's manager and would be asking what the hell is going on.

:eek:

admanimal
Dec 23, 2011, 10:23 AM
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?

jared_kipe
Dec 23, 2011, 11:36 AM
:eek:

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.

mobilehaathi
Dec 23, 2011, 10:44 PM
I've come to the conclusion that OP has no idea what he wants to accomplish, let alone any idea how to accomplish it.